Codeforces Round #540 Tanya and Candies 预处理

Posted heimao5027

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
")

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
", ans);
    return 0;
}
View Code

 

以上是关于Codeforces Round #540 Tanya and Candies 预处理的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #540 (Div. 3)

Codeforces Round #540 Tanya and Candies 预处理

Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) DFS

「日常训练」School Marks(Codeforces Round 301 Div.2 B)

Codeforces Round #301 (Div. 2) E. Infinite Inversions —— 逆序对 离散化 + 树状数组

「日常训练」Bad Luck Island(Codeforces Round 301 Div.2 D)