1499C - Minimum Grid Path - CodeForces Solution


brute force data structures greedy math *1500

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
const int maxn=5e5+45,inf=0x3f3f3f3f;
ll idx1,idx2,a[maxn],MIN1[maxn];
ll MIN2[maxn];
ll b[maxn],c[maxn],pre1[maxn],pre2[maxn];
void work(){ 
    int n;
    idx1=idx2=0;
    ll ans=MIN1[0]=MIN2[0]=1e18;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
    	scanf("%lld",&c[i]);
//    	MIN[i]=min(MIN[i-1],c[i]);
    	if(i%2)a[++idx1]=c[i];
    	else b[++idx2]=c[i];
	}
	for(int i=1;i<=idx1;i++)pre1[i]=pre1[i-1]+a[i],MIN1[i]=min(MIN1[i-1],a[i]);
	for(int i=1;i<=idx2;i++)pre2[i]=pre2[i-1]+b[i],MIN2[i]=min(MIN2[i-1],b[i]);
	for(int i=2;i<=n;i++){
		int i1,i2;
		if(i%2)i2=i/2,i1=i2+1;
		else i1=i2=i/2;
		ans=min(ans,(n-i1)*MIN1[i1]+pre1[i1]+(n-i2)*MIN2[i2]+pre2[i2]);
//		else ans=min(ans,)
	}
	printf("%lld\n",ans);
}
int main()
{
	int t=1;
	scanf("%d",&t);
	while(t--)
	work();
	return 0;
 } 
		   		   		 		    	   	    		


Comments

Submit
0 Comments
More Questions

236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD