您的位置:首页 > 其它

csu 1008 Horcrux

2012-03-18 23:39 239 查看
换成栈就解决了超时,时间上还可以优化。

# include <stdio.h>
# include <memory.h>

# define MAXN 100005

unsigned short s[MAXN];
int top;

int main()
{
int n, x, i, f, t, tot, top;

while (~scanf("%d", &n))
{
f = top = tot = 0;
memset(s, 0, sizeof(s));
if (n) scanf("%d", &f);
t = f;
s[++top] = 1;
for (i = 1; i < n; ++i)
{
scanf("%d", &x);
if (f ^ x)
{
f = x;
if (i%2 && top)
{
if (top > 1) {s[top-1] += s[top]+1;--top;}
else {++s[top]; t = !t;}
} else s[++top] = 1;
} else ++s[top];
}
if ((top%2) == t) --top;
while (top > 0)
{
tot += s[top];
top -= 2;
}
printf("%d\n", tot);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: