Description
给你一副麻将,胡牌的条件是一对将和若干顺子和刻子组成。现在给你 \(m\) 张牌,牌共 \(n\) 种,问你听哪一张牌。
\(1\leq n\leq 400,1\leq m\leq 1000\)
Solution
可以考虑枚举听那张牌,再枚举哪一对做将。
对于剩下的牌,首先从小到大来扫一遍,若当前牌的张数 \(>3\) ,可以让其先模 \(3\) ,即组成刻子。这样一定是可行的,因为让大于 \(3\) 张的该牌组成顺子,那么一定后面的牌也要是 \(3\) 张以上。但 \(3\) 张以上又可以直接组成刻子。所以一定是可行的。然后剩下的牌和后面的牌组成顺子。只需要将这张的后两张减去该张还剩的牌的个数。若扫的过程中出现负数就不可行。
Code
//It is made by Awson on 2018.2.26
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 400;
void read(int &x) {
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
x *= 1-2*flag;
}
void print(int x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(int x) {if (x < 0) putchar('-'); print(Abs(x)); }
int cnt[N+5], f[N+5], n, m, d, flag, fla, br, ans[N+5], tot;
void work() {
scanf("%d%d", &n, &m);
for (int i = 0; i <= 3*m; i++) read(d), ++cnt[d];
for (int i = 1; i <= n; i++) {
--cnt[i-1], ++cnt[i]; br = 0;
for (int j = 1; j <= n; j++)
if (cnt[j] >= 2) {
if (br) break; fla = 0;
for (int k = 1; k <= n; k++) f[k] = cnt[k]; f[j] -= 2; f[n+1] = f[n+2] = 0;
for (int k = 1; k <= n; k++) {
if (f[k] < 0) {fla = 1; break; }
f[k] %= 3, f[k+1] -= f[k], f[k+2] -= f[k];
}
if (f[n+1] < 0 || f[n+2] < 0) fla = 1;
if (fla == 0) flag = br = 1, ans[++tot] = i;
}
}
if (flag == 0) puts("NO");
else {for (int i = 1; i < tot; i++) write(ans[i]), putchar(' '); writeln(ans[tot]); }
}
int main() {
work(); return 0;
}