【C++贪心】Knot Puzzle

问题 N: Knot Puzzle

时间限制: 2.000 Sec  内存限制: 256 MB
提交 状态

题目描述

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.

输入

The input is given from Standard Input in the following format:

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.

样例输入 Copy

3 50
30 20 10

样例输出 Copy

Possible

提示

If the knot 1 is untied first, the knot 2 will become impossible to untie.
下面是代码。
解释:只要找到相邻的两段绳子总长大于L即可。
因为只要每次切割的区间都包含这两段绳子,
就可以先把这两段绳子之外的Knot全部Untie掉,
最后把两段绳子端点及内部的Knot全部Untie即可。
#include <iostream>
using
namespace std; const int N = 1e5 + 10; long long a[N]; int main() { int n, L; scanf("%d%d", &n, &L); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 2; i <= n; i++) { if (a[i - 1] + a[i] >= L) { printf("Possible\n"); return 0; } } printf("Impossible\n"); return 0; }

 

 
posted on 2024-02-10 16:52  CuberW  阅读(20)  评论(0)    收藏  举报