Code: Select all
1
11
1
1
1
-100
2
2
-100
1
1
1
Code: Select all
The nicest part of route 1 is between stops 5 and 7
Moderator: Board moderators
Code: Select all
1
11
1
1
1
-100
2
2
-100
1
1
1
Code: Select all
The nicest part of route 1 is between stops 5 and 7
Code: Select all
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<string>
#include<utility>
#include<algorithm>
using namespace std;
int main(){
int tc,stop,nice[20000],maxi,maxj,max;
scanf("%d",&tc);
for(int x=0;x<tc;x++){
nice[0]=0;max=-10000000;
scanf("%d",&stop);
for(int i=1;i<stop;i++){
int dum;
scanf("%d",&dum);
nice[i]=nice[i-1]+dum;
}
for(int i=1;i<stop;i++){
for(int j=i-1;j>=0;j--){
if(max<nice[i]-nice[j] || (max==nice[i]-nice[j]&&maxi-maxj<i-j)){
maxi=i+1;
maxj=j+1;
max=nice[i]-nice[j];
}
}
}
if(max<0)printf("Route %d has no nice parts\n",x+1);
else printf("The nicest part of route %d is between stops %d and %d\n",x+1,maxj,maxi);
}
return 0;
}
Code: Select all
#include<cstdio>
using namespace std;
void find_sum(int n,long long int arr[],int c)
{
int i,j,k,len,a,b;
long long int sum[n+1][n+1],maxlen;
maxlen=-1;
for(i=1;i<n;i++)
{
sum[i][i+1]=arr[i];
if(arr[i]>maxlen)
{
maxlen=arr[i];
a=i;
b=i+1;
}
// printf("%d %d %d\n",i,i+1,sum[i][i+1]);
}
for(len=2;len<n;len++)
{
for(i=1;i<n-len+1;i++)
{
j=i+len;
k=len/2;
sum[i][j]=sum[i][j-k]+sum[j-k][j];
//printf("%d %d %d\n",i,j,sum[i][j]);
if(sum[i][j]>maxlen)
{
maxlen=sum[i][j];
a=i;
b=j;
}
else if(sum[i][j]==maxlen)
{
if((b-a)<(j-i))
{
b=j;
a=i;
}
}
}
}
if(maxlen>=0)
printf("The nicest part of route %d is between stops %d and %d\n",c,a,b);
else
printf("Route %d has no nice parts\n",c);
}
int main()
{
int r,s,i,j;
long long arr[21000];
scanf("%d",&r);
for(i=1;i<=r;i++)
{
scanf("%d",&s);
for(j=1;j<s;j++)
scanf("%d",&arr[j]);
find_sum(s,arr,i);
}
return 0;
}