Pagini recente » Cod sursa (job #2914853) | Monitorul de evaluare | Cod sursa (job #2161283) | Cod sursa (job #2709765) | Cod sursa (job #3042333)
#include <iostream>
#include <fstream>
#include <deque>
int main()
{
std::ifstream f1("deque.in");
std::ofstream f2("deque.out");
int n, k, x, S = 0, cnt = 0, cntStergeri = 0;
bool turn = false;
std::deque<int> poz;
std::deque<int> rez;
f1 >> n >> k;
while(f1 >> x)
{
if(poz.size() > 0)
if(rez.size() > k || cnt - k >= poz.front())
{
rez.pop_front();
poz.pop_front();
}
if(rez.size() > 0)
while(x < rez.back() && rez.size() > 0)
{
rez.pop_back();
poz.pop_back();
}
rez.push_back(x);
poz.push_back(cnt);
if(cnt >= k-1)
{
S += rez.front();
}
cnt++;
}
f2 << S;
}