问题 H: Knot Puzzle
时间限制: 2 Sec 内存限制: 256 MB提交: 221 解决: 75
[ 提交][ 状态][ 讨论版][命题人: admin]
题目描述
We have N pieces of ropes, numbered 1 through N. The length of piece i is ai.
At first, for each i(1≤i≤N−1), piece i and piece i+1 are tied at the ends, forming one long rope with N−1 knots. Snuke will try to untie all of the knots by performing the following operation repeatedly:
Choose a (connected) rope with a total length of at least L, then untie one of its knots.
Is it possible to untie all of the N−1 knots by properly applying this operation? If the answer is positive, find one possible order to untie the knots.
Constraints
2≤N≤105
1≤L≤109
1≤ai≤109
All input values are integers.
At first, for each i(1≤i≤N−1), piece i and piece i+1 are tied at the ends, forming one long rope with N−1 knots. Snuke will try to untie all of the knots by performing the following operation repeatedly:
Choose a (connected) rope with a total length of at least L, then untie one of its knots.
Is it possible to untie all of the N−1 knots by properly applying this operation? If the answer is positive, find one possible order to untie the knots.
Constraints
2≤N≤105
1≤L≤109
1≤ai≤109
All input values are integers.
输入
The input is given from Standard Input in the following format:
N L
a1 a2 … an
N L
a1 a2 … an
输出
If it is not possible to untie all of the N−1 knots, print Impossible.
If it is possible to untie all of the knots, print Possible .
If it is possible to untie all of the knots, print Possible .
样例输入
3 50
30 20 10
样例输出
Possible
提示
If the knot 1 is untied first, the knot 2 will become impossible to untie.
本题题意是:我们有N段绳子,把他们打结起来,形成一个N-1个节的绳子,然后解开绳子,但是必须满足一点解开的那个绳子必须大于等于L,满足条件,我们可以解开其中任意一个。问是否可以拆完?
这道题。。。首先不要从上往下分析,因为数据范围是十分大的是1e9,并且数据量是1e5,而遇到这种数据暴力显然是不合理的,既然解开那一点的绳子必须大于等于L,
----------*---------*---------*-------------------*---------------
画出图像。。。你发现如果有一个节的两段是大于L,那么我们可以拆完其他的节。。。最后拆那个节,这样我们就一定能拆完,反之我们一定会至少有一个节两段绳子长度是小于L,无法拆完。。。
上代码
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
int a[100050];
int main(){
int n,l;
a[0]=0;
scanf("%d%d",&n,&l);
for (int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
int flag=0;
for (int i=1;i<n;i++){
if (a[i]+a[i+1]>=l){
flag=1;
}
}
if (flag)printf("Possible\n");
else printf("Impossible\n");
return 0;
}