here is only basic implementation of problems for beginners. If you have any problem with any solution or any basic concept of programming or you want more efficient solution you can mail me.
my suggestion is not to copy and paste codes from here try to understand the logic and think why you were not able to solve it.

Thursday 18 September 2014

Spoj candies and milestones

//http://www.spoj.com/problems/CANDYSTN/

#include<stdio.h>

//fast input
inline int fastRead_int() {
    register int c = getchar_unlocked();
    int x = 0;
    int neg = 0;
    for(; ((c<48 || c>57) && c != '-'); c = getchar_unlocked());
    if(c=='-') {
    neg = 1;
    c = getchar_unlocked();
    }
    for(; c>47 && c<58 ; c = getchar_unlocked()) {
    x = (x<<1) + (x<<3) + c - 48;
    }
    if(neg)
    x = -x;
   
    return x;
}


int main(){
long long int t,n,m,ar[100000],i,j,p,q;
t=fastRead_int();
while(t--){
p=0;q=0;
long long min=0,minb=0;
n=fastRead_int();
m=fastRead_int();
ar[0]=fastRead_int();
for(i=1;i<m;i++){
ar[i]=fastRead_int();
if(ar[i]>ar[i-1]){
p-=(ar[i]-ar[i-1]);
q+=(ar[i]-ar[i-1]);
if(p<minb){
minb=p;
}
}else if(ar[i]<ar[i-1]){
p+=(ar[i-1]-ar[i]);
q-=(ar[i-1]-ar[i]);
if(q<min){
min=q;
}
}else{
q--;
if(q<min){
min=q;
}
}
}
j=(-1)*minb+(-1)*min+1;
if(j>n){
printf("-1\n");
}else{
printf("%lld\n",(-1)*min+1);
}
}
return 0;
}

No comments:

Post a Comment