Friday, April 20, 2018

ALIEN - Aliens at the train

prerequisite: -  Kadane's algorithm, window sliding technique



Solution:-


#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,b;
cin>>n>>b;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int glsum=0,tempsum=0,glcnt=0,tempcnt=0;
for(int i=0,j=0;i<n&&j<n;){
if(i>j){
j++;
continue;
}
if(tempsum+a[j]<=b){
tempcnt++;
tempsum+=a[j];
j++;
}
else{
if(tempcnt>glcnt){
glcnt=tempcnt;
glsum=tempsum;
}
else if(tempcnt==glcnt&&glsum>tempsum){
glsum=tempsum;
}
tempsum-=a[i];
tempcnt--;
i++;
}
}
if(tempcnt>glcnt){
glcnt=tempcnt;
glsum=tempsum;
}
else if(tempcnt==glcnt&&glsum>tempsum){
glsum=tempsum;
}
cout<<glsum<<" "<<glcnt<<endl;
}
return 0;
}

No comments:

Post a Comment

CMPLS - Complete the Sequence!

Problem Link:-     http://www.spoj.com/problems/CMPLS/ Prerequisite:- Method of differences Solution:-