码迷,mamicode.com
首页 > 其他好文 > 详细

Tanya and Candies

时间:2019-02-20 10:07:14      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:rem   code   cat   ini   weight   std   mes   specific   print   

Tanya has nn candies numbered from 11 to nn. The ii-th candy has the weight aiai.

She plans to eat exactly n?1n?1 candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.

Your task is to find the number of such candies ii (let‘s call these candies good) that if dad gets the ii-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.

For example, n=4n=4 and weights are [1,4,3,3][1,4,3,3]. Consider all possible cases to give a candy to dad:

  • Tanya gives the 11-st candy to dad (a1=1a1=1), the remaining candies are [4,3,3][4,3,3]. She will eat a2=4a2=4 in the first day, a3=3a3=3 in the second day, a4=3a4=3 in the third day. So in odd days she will eat 4+3=74+3=7 and in even days she will eat 33. Since 737≠3 this case shouldn‘t be counted to the answer (this candy isn‘t good).
  • Tanya gives the 22-nd candy to dad (a2=4a2=4), the remaining candies are [1,3,3][1,3,3]. She will eat a1=1a1=1 in the first day, a3=3a3=3 in the second day, a4=3a4=3 in the third day. So in odd days she will eat 1+3=41+3=4 and in even days she will eat 33. Since 434≠3 this case shouldn‘t be counted to the answer (this candy isn‘t good).
  • Tanya gives the 33-rd candy to dad (a3=3a3=3), the remaining candies are [1,4,3][1,4,3]. She will eat a1=1a1=1 in the first day, a2=4a2=4 in the second day, a4=3a4=3 in the third day. So in odd days she will eat 1+3=41+3=4 and in even days she will eat 44. Since 4=44=4 this case should be counted to the answer (this candy is good).
  • Tanya gives the 44-th candy to dad (a4=3a4=3), the remaining candies are [1,4,3][1,4,3]. She will eat a1=1a1=1 in the first day, a2=4a2=4 in the second day, a3=3a3=3 in the third day. So in odd days she will eat 1+3=41+3=4 and in even days she will eat 44. Since 4=44=4 this case should be counted to the answer (this candy is good).

In total there 22 cases which should counted (these candies are good), so the answer is 22.

Input

The first line of the input contains one integer nn (1n2?1051≤n≤2?105) — the number of candies.

The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (1ai1041≤ai≤104), where aiai is the weight of the ii-th candy.

Output

Print one integer — the number of such candies ii (good candies) that if dad gets the ii-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days.

超时。。。

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int i,a[10005],ready[10005][5],day=1;
    for(i=0;i<n;i++) {
        cin>>a[i];
    }
    int j;
    ready[0][1]=0;
    ready[0][2]=0;
    for(j=0,day=1;j<n;j++,day++){
        if(day%2==1){
            ready[day][1]=ready[day-1][1]+a[j];
            ready[day][2]=ready[day-1][2];
        }
        else{
            ready[day][1]=ready[day-1][1];
            ready[day][2]=ready[day-1][2]+a[j];
        }
    }
    int count=0,k,sum1=0,sum2=0,all=day-1;
//    cout<<ready[all][1]<<" "<<ready[all][2]<<endl;
    for(k=0;k<n;k++){
//        for(day=1,sum1=0,sum2=0,i=0;i<n;i++){
//            if(i==k) continue;
//            if(day%2==1) sum1+=a[i];
//            else sum2+=a[i];
//            day++;
//        }
//        day=k+1;
        sum1=ready[k][1]+ready[all][2]-ready[k+1][2];
        sum2=ready[k][2]+ready[all][1]-ready[k+1][1];
        if(sum1==sum2)
        {
//            cout<<sum1<<" "<<sum2<<endl;
            count++;
        }
    }
    cout<<count<<endl;

    return 0;
}

 

Tanya and Candies

标签:rem   code   cat   ini   weight   std   mes   specific   print   

原文地址:https://www.cnblogs.com/shengge-777/p/10404351.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!