357A - Group of Students - CodeForces Solution


brute force greedy implementation *1000

Please click on ads to support us..

Python Code:

def serch():
    global n,l,x,y,left
    for i in range(n):
        left += l[i]
        if y >= left >= x:
            break
        elif left > y:
            return 0
    right = sum(l[i+1:])
    for j in range(i+1, n):
        if right < x:
            return 0
        elif right > y:
            if y >= left + l[j] >= x:
                right -= l[j]
                left += l[j]
            else:
                return 0
        else:
            return j+1
    else:
        return 0


n = int(input())
l = list(map(int, input().split()))
x, y = map(int, input().split())
left = 0
print(serch())

		 		     	 	  	 	  	     	 	 	

C++ Code:

#include <bits/stdc++.h>
#include <cmath>
#define ll long long

using namespace std;

int main() {
 ll grp1,grp2,i,n,x,y;
	while(cin>>n){
			ll a[105],sum=0,ans=0;
		for(i=1;i<=n;i++){
			cin>>a[i];
			sum+=a[i];
		}
		cin>>x>>y;
		grp1=0;
		grp2=sum;
		for(i=1;i<=n;i++){
				grp1+=a[i];
				grp2-=a[i];
				if((grp1>=x && grp1<=y) && (grp2>=x &&grp2<=y))
				{
					ans=i+1;
					break;

				}
		}
		cout<<ans<<endl;
	}
}


Comments

Submit
0 Comments
More Questions

1729D - Friends and the Restaurant
1606C - Banknotes
580C - Kefa and Park
342A - Xenia and Divisors
1033A - King Escape
39D - Cubical Planet
1453A - Cancel the Trains
645A - Amity Assessment
1144A - Diverse Strings
1553B - Reverse String
1073A - Diverse Substring
630N - Forecast
312B - Archer
34D - Road Map
630I - Parking Lot
160B - Unlucky Ticket
371B - Fox Dividing Cheese
584B - Kolya and Tanya
137B - Permutation
550C - Divisibility by Eight
5A - Chat Servers Outgoing Traffic
615A - Bulbs
5B - Center Alignment
549A - Face Detection
535B - Tavas and SaDDas
722C - Destroying Array
366A - Dima and Guards
716B - Complete the Word
1461C - Random Events
1627A - Not Shading