I was reading the submissions and came along a neat submission by the very own @pieguy (You know him, as he is setter of many problems in the Contests here at Codechef).
I am pasting the code snippets where I have doubt, if anyone or @pieguy himself can explain this, then it will really be helpful.
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int E, R;
long long solve(int* start, int length, int se, int ee) {
if(length == 0)
return 0;
long long pos = max_element(start, start+length) - start;
long long ie = pos*R+se;
if(ie > E)
ie=E;
long long re = ee-(length-pos)*R;
if(re < 0)
re=0;
long long res=(ie-re)*start[pos];
res+=solve(start, pos, se, ie);
res+=solve(start+pos+1, length-pos-1, re+R, ee);
return res;
}
int main(){
int T;
scanf("%d", &T);
for(int t=1; t<=T; t++) {
int N, v[10000];
scanf("%d%d%d", &E, &R, &N);
for(int i=0; i<N; i++)
scanf("%d", v+i);
printf("Case #%d: %lld\n", t, solve(v, N, E, R));
}
}
This was the solve() that he used, the intitial call was made by
solve(v, N, E, R)
For meaning of symbols refer to the [problem.][1]
v is the array to store each vi.
I did not understand the
ie = pos*R + se;
part, why is he multiplying regain amount to the position of the max element and then adding to the initial energy.
Again, while calculating re, he is doing something similar.
Please clarify this.
[1]: https://code.google.com/codejam/contest/2418487/dashboard#s=p1