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.

Saturday, 4 October 2014

Interesting Selection

problem statement is here


here we have two possibilities
        (1). we take cold drink from 1st position
        (2). we take poison from 1st position
rest can be easily calculated using dp


#include<stdio.h>
long long ar[100006],br[100007];
long long max(long long a,long long b){
if(a>b){
return a;
}else{
return b;
}
}
long long check(long long a,long long b){

if(a>b){
return 0;
}
long long cr[b+1],i;
cr[a]=ar[a];
cr[a-1]=0;
for(i=a+1;i<=b;i++){
cr[i]=max((cr[i-2]+ar[i]-br[i-1]),cr[i-1]-br[i]);
}
return cr[b];
}
int main(){
long long a,b,c,i,j,n,x,y,z;
int t;
scanf("%d",&t);
while(t--){
scanf("%lld",&n);
for(i=0;i<n;i++){
scanf("%lld",&ar[i]);
}
for(i=0;i<n;i++){
scanf("%lld",&br[i]);
}
if(n==1){
x=ar[0];
}else if(n==2){
x=max(ar[0]-br[1],ar[1]-br[0]);
}else{
y=ar[0]-br[n-1]-br[1]+check(2,n-2);
z=check(1,n-1)-br[0];
x=max(y,z);
}
printf("%lld\n",x);
}
return 0;
}

No comments:

Post a Comment