In the case:
4
1
-1
1
my program return "from 3 to 4 "...and the solution is "from 1 to 4"...my problem is "If more than one segment is maximally nice, choose the one with the longest cycle ride (largest j-i)."
Code: Select all
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
int n,casos,i; scanf("%d",&casos); int test=1;
while(casos--){
scanf("%d",&n); int suma=0,dp=-20001,var,ini,fin,inif,finf; ini=fin=0; inif=finf=-1;
for(i=0;i<n-1;i++){
scanf("%d",&var);
if(suma>0){ suma+=var; fin=i; }
else{ suma=var; ini=i; }
if(suma==dp){ if( fabs(fin-ini)>(finf-inif) ){ inif=ini; finf=i; }
}
if(suma>dp){ dp=suma;
inif=ini;
finf=i;
}
}
if(dp<0) printf("Route %d has no nice parts\n",test);
else
printf("The nicest part of route %d is between stops %d and %d\n",test,inif+1,finf+2);
test++;
}
return 0;
}