Codeforces Round #540 Tanya and Candies 预处理

http://codeforces.com/contest/1118/problem/B

 

题目大意,给你一个序列,删去一个数值之后,要求剩下序列奇数和偶数的和相同,问有多少种删法。

 

思路:预处理奇数和偶数和即可

 

 

(算法能力康复性训练中......)

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")

using namespace std;
const int maxn = 2e5 + 5;
int a[maxn];
//表示到当前的偶数或奇数和
int odd1[maxn], even1[maxn];//前往后
int n;

int main(){
    cin >> n;
    for (int i = 1; i <= n ;i++){
        scanf("%d", a + i);
    }
    int sum = 0;
    for (int i = 1; i <= n; i++){
        even1[i] = even1[i-1];
        odd1[i] = odd1[i-1];
        sum += a[i];
        if (i % 2 == 1) odd1[i] += a[i];
        else even1[i] += a[i];
    }
    int ans = 0;
    for (int i = 1; i <= n; i++){
        if (i % 2 == 1){
            int odd = even1[n] - even1[i] + odd1[i] - a[i];
            int even = sum - a[i] - odd;
            if (odd == even) ans++;
        }
        else {
            int odd = odd1[i] + even1[n] - even1[i];
            int even = sum - odd - a[i];
            if (odd == even) ans++;
        }
    }
    printf("%d\n", ans);
    return 0;
}
View Code

 

posted @ 2019-02-26 20:47  知る奇迹に  阅读(133)  评论(0编辑  收藏  举报