Codeforces 342
A:
只可能有1 2 4、1 2 6、1 3 6三种情况,先把4分光然后就乱搞了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4
5 using namespace std;
6
7 int n,cnt[10];
8
9 int main()
10 {
11 scanf("%d",&n);
12 for (int a=1;a<=n;a++)
13 {
14 int v;
15 scanf("%d",&v);
16 if (v==5 || v==7)
17 {
18 printf("-1\n");
19 return 0;
20 }
21 cnt[v]++;
22 }
23 if (cnt[1]!=cnt[2]+cnt[3] || cnt[1]!=cnt[4]+cnt[6] || cnt[4]>cnt[2])
24 {
25 printf("-1\n");
26 return 0;
27 }
28 while (cnt[6])
29 {
30 if (cnt[3])
31 {
32 printf("1 3 6\n");
33 cnt[1]--;cnt[3]--;cnt[6]--;
34 }
35 else
36 {
37 printf("1 2 6\n");
38 cnt[1]--;cnt[2]--;cnt[6]--;
39 }
40 }
41 for (;cnt[1];cnt[1]--)
42 printf("1 2 4\n");
43
44 return 0;
45 }
B:
贪心走。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4
5 using namespace std;
6
7 const int maxm=100010;
8
9 int n,m,s,f,t[maxm],l[maxm],r[maxm];
10
11 int main()
12 {
13 scanf("%d%d%d%d",&n,&m,&s,&f);
14 for (int a=1;a<=m;a++)
15 scanf("%d%d%d",&t[a],&l[a],&r[a]);
16 for (int a=1,b=1;s!=f;b++)
17 {
18 while (a<=m && t[a]<=b)
19 a++;
20 a--;
21 if (s>f)
22 {
23 if (t[a]==b && s>=l[a] && s<=r[a]+1) printf("X");
24 else printf("L"),s--;
25 }
26 else
27 {
28 if (t[a]==b && s>=l[a]-1 && s<=r[a]) printf("X");
29 else printf("R"),s++;
30 }
31 }
32 printf("\n");
33
34 return 0;
35 }
C:
谁告诉我为啥错了……
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 #include<cmath>
5
6 using namespace std;
7
8 int main()
9 {
10 int r,h;
11 scanf("%d%d",&r,&h);
12 int ans=h/r*2;
13 double remain=h%r;
14 remain+=sqrt(3)*r/2.0;
15 if (remain>=(double)r) ans+=2;
16 else ans++;
17 printf("%d\n",ans);
18
19 return 0;
20 }
D:
要求某些格子不能放然后要保证至少有一个骨牌能够滑进某个指定位置,读懂题就是简单DP了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4
5 using namespace std;
6
7 #define inc(a,b) {a+=b;if (a>=mo) a-=mo;}
8
9 const int maxn=10010;
10 const int mo=1000000007;
11
12 int n,f[maxn][8][2];
13
14 char s[4][maxn];
15
16 int main()
17 {
18 scanf("%d",&n);
19 for (int a=1;a<=3;a++)
20 scanf("%s",s[a]+1);
21 f[0][0][0]=1;
22 for (int a=0;a<n;a++)
23 for (int b=0;b<8;b++)
24 for (int c=0;c<2;c++)
25 if (f[a][b][c])
26 {
27 int x,y,z;
28 x=b&1;
29 y=b&2;
30 z=b&4;
31 if (x && s[1][a+1]!='.') continue;
32 if (y && s[2][a+1]!='.') continue;
33 if (z && s[3][a+1]!='.') continue;
34 if (!x && !y && s[1][a+1]=='.' && s[2][a+1]=='.')
35 {
36 if (s[3][a+1]=='O') inc(f[a+1][0][1],f[a][b][c])
37 else
38 {
39 if (z) inc(f[a+1][0][c],f[a][b][c])
40 else
41 {
42 if (s[3][a+1]!='.') inc(f[a+1][0][c],f[a][b][c])
43 else inc(f[a+1][4][c|(s[3][a+2]=='O')],f[a][b][c]);
44 }
45 }
46 }
47 if (!y && !z && s[2][a+1]=='.' && s[3][a+1]=='.')
48 {
49 if (s[1][a+1]=='O') inc(f[a+1][0][1],f[a][b][c])
50 else
51 {
52 if (x) inc(f[a+1][0][c],f[a][b][c])
53 else
54 {
55 if (s[1][a+1]!='.') inc(f[a+1][0][c],f[a][b][c])
56 else inc(f[a+1][1][c|(s[1][a+2]=='O')],f[a][b][c]);
57 }
58 }
59 }
60 int ss=0;
61 if (x && s[1][a+2]=='O') ss=1;
62 if (y && s[2][a+2]=='O') ss=1;
63 if (z && s[3][a+2]=='O') ss=1;
64 if (!x && s[1][a+1]=='.' && s[1][a]=='O') ss=1;
65 if (!y && s[2][a+1]=='.' && s[2][a]=='O') ss=1;
66 if (!z && s[3][a+1]=='.' && s[3][a]=='O') ss=1;
67 int sx=0;
68 if (!z && s[3][a+1]=='.') sx|=1;
69 sx<<=1;
70 if (!y && s[2][a+1]=='.') sx|=1;
71 sx<<=1;
72 if (!x && s[1][a+1]=='.') sx|=1;
73 inc(f[a+1][sx][ss|c],f[a][b][c]);
74 }
75 printf("%d\n",f[n][0][1]);
76
77 return 0;
78 }
E:
尼玛一坨暴力过的……每当未处理的结点数超过阈值就暴力重做,复杂度O(n^1.5)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 #include<cmath>
5 #include<algorithm>
6
7 using namespace std;
8
9 const int maxn=100010;
10
11 int n,m,en,q[maxn],depth[maxn],f[maxn][20],dist[maxn];
12
13 struct edge
14 {
15 int e;
16 edge *next;
17 }*v[maxn],ed[maxn<<1];
18
19 void add_edge(int s,int e)
20 {
21 en++;
22 ed[en].next=v[s];v[s]=ed+en;v[s]->e=e;
23 }
24
25 void bfs(int root)
26 {
27 int front=1,tail=1;
28 q[1]=root;
29 depth[1]=1;
30 for (;front<=tail;)
31 {
32 int now=q[front++];
33 for (edge *e=v[now];e;e=e->next)
34 if (!depth[e->e])
35 {
36 depth[e->e]=depth[now]+1;
37 f[e->e][0]=now;
38 int p=now,x=0;
39 while (f[p][x])
40 {
41 f[e->e][x+1]=f[p][x];
42 p=f[p][x];
43 x++;
44 }
45 q[++tail]=e->e;
46 }
47 }
48 }
49
50 int get_lca(int p1,int p2)
51 {
52 if (depth[p1]<depth[p2]) swap(p1,p2);
53 int delta=depth[p1]-depth[p2],x=0;
54 while (delta)
55 {
56 if (delta&1) p1=f[p1][x];
57 delta>>=1;
58 x++;
59 }
60 x=0;
61 while (p1!=p2)
62 {
63 if (!x || f[p1][x]!=f[p2][x])
64 {
65 p1=f[p1][x];
66 p2=f[p2][x];
67 x++;
68 }
69 else x--;
70 }
71 return p1;
72 }
73
74 int main()
75 {
76 scanf("%d%d",&n,&m);
77 for (int a=1;a<n;a++)
78 {
79 int s,e;
80 scanf("%d%d",&s,&e);
81 add_edge(s,e);
82 add_edge(e,s);
83 }
84 int size=(int)sqrt(n);
85 bfs(1);
86 int cnt=1;
87 q[1]=1;
88 memset(dist,0x3f,sizeof(dist));
89 dist[1]=0;
90 int opt,p;
91 for (int a=1;a<=m;a++)
92 {
93 scanf("%d%d",&opt,&p);
94 if (opt==1)
95 {
96 q[++cnt]=p;
97 dist[p]=0;
98 }
99 else
100 {
101 int ans=dist[p];
102 for (int b=1;b<=cnt;b++)
103 ans=min(ans,depth[p]+depth[q[b]]-2*depth[get_lca(p,q[b])]);
104 printf("%d\n",ans);
105 }
106 if (cnt>=size)
107 {
108 for (int front=1,tail=cnt+1;front!=tail;)
109 {
110 int now=q[front++];
111 if (front==maxn) front=0;
112 for (edge *e=v[now];e;e=e->next)
113 if (dist[e->e]>dist[now]+1)
114 {
115 dist[e->e]=dist[now]+1;
116 q[tail++]=e->e;
117 if (tail==maxn) tail=0;
118 }
119 }
120 cnt=0;
121 }
122 }
123
124 return 0;
125 }