题解:AT_agc002_c [AGC002C] Knot Puzzle

· · 题解

[AGC002C] Knot Puzzle

每次选择一段长度至少为 L 的绳子。 所以如果找不到长度为 L 的一段(或两段)绳子,则无解。

输出解开绳结的可能顺序。这些 N−1 行中的 j -th 应该包含在 j -th 操作中被解开的绳结的索引。这里,连接 i 和 i+1 的结的索引为 i。

如图,从 1 拆到 3,再从 8 拆到 6,最后拆 4,5。

code:

#include <bits/stdc++.h>
using namespace std;
int n, l, a[100050], cnt = -1;
int main()
{
    cin >> n >> l;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    for (int i = 0; i < n - 1; i++) {
        if (a[i] + a[i + 1] >= l) { //有可以解开的
            cnt = i;
            break;
        }
    }
    if (cnt == -1) {
        cout << "Impossible" << endl;
        return 0;
    }
    cout << "Possible" << endl;
    for (int i = 0; i < cnt; i++) //前部分
        cout << i + 1 << endl;
    for (int i = n - 2; i > cnt; i--) //后部分
        cout << i + 1 << endl;

    cout << cnt + 1 << endl; //中间保留下来的
    return 0;
}