Cod sursa(job #2120035)
| Utilizator | Data | 1 februarie 2018 20:44:51 | |
|---|---|---|---|
| Problema | Subsecventa de suma maxima | Scor | 85 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 1.1 kb |
#include <bits/stdc++.h>
#define int long long
std::ifstream in("ssm.in");
std::ofstream out("ssm.out");
using namespace std;
int n , k;
deque<int>d;
main()
{ int a=1,b=k,x,sum=INT_MIN,actual=0;
int incp=1,sfp;
in >> n;
for(int i =1 ; i<=n ;++i)
{
in >> x ;
if(d.size()<k)
{d.push_back(x);
actual+=x;
}
else{
if( (actual>=0 and actual+x<0) )
{d.clear();
incp=i;
d.push_back(x);
actual=x;
//daca e cazul sa il scot tot
}
else
{
d.push_back(x);
actual+=x;
if(d.size()>k&&d.front()<0) {actual-=d.front();
d.pop_front();
++incp;
}
}
}if(actual>sum&&d.size()>=k)sum=actual,b=i,a=incp;
//cout << actual << endl;
}
out <<sum<<" "<<a<<" "<<b;
return 0;
}
