5583: Knot Puzzle
时间限制: 2 Sec 内存限制: 256 MB提交: 232 解决: 80
[ 提交][ 状态][ 讨论版][命题人: 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.
来源
//思路:思考一下什么时候是一定能解开的
//1.有一段绳子>l的时候,将这段绳子留在最后,一定能全解开
//2.根据1,可类比得出当存在两段相邻绳子的长度和>=l,也一定能解开
//3.所以反过来可以推出,如果所有两段相邻绳子的长度和<l,一定解不开
#include<bits/stdc++.h>
using namespace std;
int s[100005];
int main()
{
int n;
while(~scanf("%d",&n))
{
int l;
scanf("%d",&l);
for(int i=0; i<n; i++)
{
scanf("%d",&s[i]);
}
int num = 0;
for(int i=0; i<n-1; i++)
{
if(s[i]+s[i+1]<l)
{
num++;
}
}
if(num == n-1) printf("Impossible\n");
else printf("Possible\n");
}
}