939C - Convenient For Everybody - CodeForces Solution


binary search two pointers *1600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define MSET(x,y) memset(x,y,sizeof(x))
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define SZ size()
#define M 100005
#define ll long long
#define ld long double
#define INF 1e18
#define NINF -1e18
#define endl "\n"
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
ll MOD = 1000000007;
string yes = "YES\n";
string no = "NO\n";

void solve(ll t) {
	ll n;
	cin >> n;
	vector<ll> a(n);
	for(auto &it : a) cin >> it;
	ll s, f;
	cin >> s >> f;
	ll sum = 0,mx=0,timeat1=s-1;
	for(ll i=0;i<(f-s);i++) sum += a[i];
	mx=sum;
	for(ll i=1;i<n;i++) {
		sum = sum - a[i-1] + a[(i + (f-s) - 1)%n];
		// cout << i << " " << sum << endl;
		if(sum >= mx) {
			if(sum == mx) timeat1 = min(timeat1, (n + s-i-1)%n);
			else timeat1 = (n+s-i-1)%n;
			mx=sum;
			// cout << i << " " << timeat1 << endl;

		}
 	}

	cout << timeat1+1<< endl;
}

int32_t main() {
	fastio;
	#ifndef ONLINE_JUDGE
		freopen("i.txt", "r", stdin);
		freopen("o.txt", "w", stdout);
	#endif
	ll t=1;
	// cin>>t;
	for(ll i=1;i<=t;i++) {
		solve(i);
	}
}


Comments

Submit
0 Comments
More Questions

101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake