csu 1008 Horcrux

换成栈就解决了超时,时间上还可以优化。

 1 # include <stdio.h>
2 # include <memory.h>
3
4 # define MAXN 100005
5
6 unsigned short s[MAXN];
7 int top;
8
9 int main()
10 {
11 int n, x, i, f, t, tot, top;
12
13 while (~scanf("%d", &n))
14 {
15 f = top = tot = 0;
16 memset(s, 0, sizeof(s));
17 if (n) scanf("%d", &f);
18 t = f;
19 s[++top] = 1;
20 for (i = 1; i < n; ++i)
21 {
22 scanf("%d", &x);
23 if (f ^ x)
24 {
25 f = x;
26 if (i%2 && top)
27 {
28 if (top > 1) {s[top-1] += s[top]+1;--top;}
29 else {++s[top]; t = !t;}
30 } else s[++top] = 1;
31 } else ++s[top];
32 }
33 if ((top%2) == t) --top;
34 while (top > 0)
35 {
36 tot += s[top];
37 top -= 2;
38 }
39 printf("%d\n", tot);
40 }
41
42 return 0;
43 }



posted on 2012-03-18 23:39  getgoing  阅读(483)  评论(0编辑  收藏  举报

导航