P1195 “非常男女”计划

#2, 类似前缀和的思想,以前OI的确用过这种思想,但昨天一时竟然没反应过来,太笨了。

需要注意的情况是如果0,1相同时要特殊处理,其下标就是长度(即若串是从1算起的话是0)。

评测结果
编译成功
测试数据 #0: Accepted, time = 15 ms, mem = 1252 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 1248 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 1256 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 1256 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 1252 KiB, score = 10
测试数据 #5: Accepted, time = 31 ms, mem = 1248 KiB, score = 10
测试数据 #6: Accepted, time = 93 ms, mem = 1252 KiB, score = 10
测试数据 #7: Accepted, time = 124 ms, mem = 1256 KiB, score = 10
测试数据 #8: Accepted, time = 140 ms, mem = 1252 KiB, score = 10
测试数据 #9: Accepted, time = 140 ms, mem = 1256 KiB, score = 10
Accepted, time = 543 ms, mem = 1256 KiB, score = 100

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int _tbl[2] = {-1, 1};
 5 
 6 int _fs[200001], N;
 7 #define fs(x)    _fs[(x) + 100000]
 8 
 9 int main(void)
10 {
11     int max = 0;
12     int i, s;
13     for(cin >> N, s = 0, i = 1; i<=N; ++i) {
14         int o;
15         cin >> o;
16         s += _tbl[o];
17         if (s) {
18             if (!fs(s)) fs(s) = i;
19             else if (max < i - fs(s)) max = i - fs(s);
20         } else if (max < i) max = i;
21     }
22     cout << max << endl;
23     return 0;
24 }

 

posted @ 2014-04-09 16:58  e0e1e  阅读(182)  评论(0编辑  收藏  举报