Pagini recente » Cod sursa (job #1017761) | Cod sursa (job #1247507) | Cod sursa (job #925196) | Cod sursa (job #1248144) | Cod sursa (job #2931331)
#include <fstream>
#include <deque>
using namespace std;
ifstream fin("buline.in");
ofstream fout("buline.out");
int n, x, y, v[400005];
int smax, st, l, lmax;
int main() {
fin >> n;
for(int i = 1; i <= n; i++) {
fin >> x >> y;
if(y == 0) {
v[i] -= x;
} else {
v[i] += x;
}
v[i + n] = v[i];
}
deque<int> DQ;
DQ.push_back(1);
for(int i = 2; i <= 2 * n; i++) {
v[i] += v[i - 1];
// eliminim toate elem care se repeta
while(!DQ.empty() && i - DQ.front() > n) {
DQ.pop_front();
}
if(v[i] - v[DQ.front()] > smax) {
smax = v[i] - v[DQ.front()];
st = DQ.front() + 1;
lmax = i - DQ.front();
}
// scot elem. calculate inainte
while(!DQ.empty() && v[DQ.back()] > v[i]) {
DQ.pop_back();
}
DQ.push_back(i);
}
fout << smax << " " << st << " " << lmax;
return 0;
}