Codeforces Round #207 (Div. 2) A. Group of Students

OpenSoucre發表於2013-10-19
 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 int main(){
 7     int m,sum = 0;
 8     cin >> m ;
 9     vector<int> c(m+1,0);
10     for(int i = 1; i <= m ; ++ i) {cin >> c[i];sum+=c[i];}
11     int x,y,firstPart = 0, secondPart = 0,firstIndex = 0, secondIndex =0;
12     cin >>  x >>  y;
13     for(firstIndex = 0,secondIndex = m+1;firstIndex < secondIndex;){
14         if (firstPart < x) firstPart+=c[++firstIndex];
15         if (secondPart < x) secondPart += c[--secondIndex];
16         if( firstPart >= x && secondPart >= x ) break;
17     }
18     if(firstPart > y || secondPart > y) cout<<0<<endl;
19     else{
20         if(firstIndex >= secondIndex) cout<<0<<endl;
21         else{
22             if(sum - firstPart - secondPart > 2*(y-x)) cout<<0<<endl;
23             else{
24                 while(secondPart + c[secondIndex-1]<= y && (secondIndex-1) > firstIndex){
25                     secondIndex--;
26                     secondPart+=c[secondIndex];
27                 }
28                 if (secondIndex-1 <= firstIndex) cout<<secondIndex<<endl;
29                 else{
30                     if(sum - secondPart > y) cout<<0<<endl;
31                     else cout<<secondIndex<<endl;
32                 }
33             }
34         }
35     }
36 }

相關文章