Files
python/TangDou/CSP-J/AcWingTraining/ShunZi1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

60 lines
1.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 10010;
int a[N]; // 用来装都是什么牌
int st[N]; // 标识是不是已经被用过
int n, w;
/*
9
1 2 3 6 2 3 4 7 8
3
9
1 2 3 6 2 3 4 7 9
3
*/
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
cin >> w;
// 可以分组输出true,不可以分组输出false
// n/w个组
// 特判
if (n % w) {
cout << "false" << endl;
return 0;
}
sort(a + 1, a + 1 + n); // 排序
// 遍历每个牌,看看它是不是没有被用过,如果它还没有被用过的话,那么需要找出一串以它开头的顺子
for (int i = 1; i <= n; i++) {
if (!st[i]) {
st[i] = 1; // i号牌已使用
int start = i + 1; // 从哪位置开始找
int target = a[i] + 1; // 要找啥
for (int j = 1; j < w; j++) {
// 如果idx合法,表示找到了,如果不合法,表示没找着
// 合法了,就需要记录这张牌已经被使用
// 不合法,就表示找不到,意味着整体找不到了
bool flag = false;
for (int k = start; k <= n; k++) {
if (!st[k] && a[k] == target) {
st[k] = 1;
flag = true;
start = k + 1;
target++;
break;
}
}
if (!flag) {
cout << "false" << endl;
return 0;
}
}
}
}
cout << "true" << endl;
return 0;
}