题解:P8467 [Aya Round 1 B] 甲(one)

· · 题解

P8467 题解

思路

通过我的观察题目的意思就是让我们满足条件的 P 使得:

a_{P_1}-1=a_{P_2}=a_{P_3}+1 a_{P_4}=a_{P_5}

然后再枚举 a_5:对于每组给定的 a_1,a_2,a_3,a_4,枚举 a_5 的可能取值(0 到 9),并检查是否存在满足条件的排列 P。

排列检查:对于每个可能的 a_5,检查是否存在满足条件的排列 P。

AC 代码

#include <bits/stdc++.h>
using namespace std;
bool kkksc03(int a[5]) {
    int temp[5];
    for (int i = 0; i < 5; i++) temp[i] = a[i];
    sort(temp, temp + 5);
    do {
        if (temp[0] - 1 == temp[1] && temp[1] == temp[2] + 1 && temp[3] == temp[4]) return true;
        int k = -1;
        for (int i = 4; i > 0; --i) {
            if (temp[i - 1] < temp[i]) {
                k = i - 1;
                break;
            }
        }
        if (k == -1) break;
        int l = -1;
        for (int i = 4; i > k; i--) {
            if (temp[i] > temp[k]) {
                l = i;
                break;
            }
        }
        swap(temp[k], temp[l]);
        reverse(temp + k + 1, temp + 5);
    } while (1);
    return 0;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int a[5];
        for (int i = 0; i < 4; i++) cin >> a[i];
        bool found = 0;
        for (int a5 = 0; a5 <= 9; a5++) {
            a[4] = a5;
            if (kkksc03(a)) {
                found = true;
                break;
            }
        }
        cout << (found ? 1 : 0) << '\n';
    }
    return 0;
}