bzoj 1028: [JSOI2007]麻将

暴力枚举+贪心

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<map>
 8 #include<cmath>
 9 #define M 100009
10 #define ll long long
11 #define Mo 998244353
12 #define pa pair<int,int>
13 using namespace std;
14 ll read()
15 {
16     char ch=getchar();
17     ll x=0,f=1;
18     for(;ch<'0'||ch>'9';ch=getchar())
19       if(ch=='-')
20         f=-1;
21     for(;ch>='0'&&ch<='9';ch=getchar())
22       x=x*10+ch-'0';
23     return x*f;
24 }
25 int a[M],shu[M],n,m,ans[M];
26 bool pan()
27 {
28     for(int i=1;i<=n;i++)
29       a[i]=shu[i];
30     for(int i=1;i<=n-2;i++)
31       {
32         if(a[i]<0)
33           return 0;
34         a[i]=a[i]%3;
35         a[i+1]-=a[i];
36         a[i+2]-=a[i];
37       }
38     if(a[n-1]%3||a[n]%3)
39       return 0;
40     return 1;
41 }
42 int main()
43 {
44     n=read();
45     m=read();
46     for(int i=1;i<=3*m+1;i++)
47       {
48         int a1=read();
49         shu[a1]++;
50       }
51     for(int i=1;i<=n;i++)
52       {
53         shu[i]++;
54         for(int j=1;j<=n;j++)
55           {
56             if(shu[j]<2)
57               continue;
58             shu[j]-=2;
59             if(pan())
60               {
61                 ans[++ans[0]]=i;
62                 shu[j]+=2;
63                 break;
64               }
65             shu[j]+=2;
66           }
67         shu[i]--;
68       }
69     for(int i=1;i<ans[0];i++)
70       printf("%d ",ans[i]);
71     if(!ans[0])
72       printf("NO\n");
73     else
74       printf("%d\n",ans[ans[0]]);
75     return 0;
76 }

 

posted @ 2016-07-09 21:13  xiw5  阅读(126)  评论(0编辑  收藏  举报