Pagini recente » Cod sursa (job #1856914) | Cod sursa (job #3275074) | Rating Tircomnicu Nicoleta (Nicoletatircomnicu) | Cod sursa (job #1007089) | Cod sursa (job #3042335)
#include <iostream>
#include <fstream>
#include <deque>
int main()
{
std::ifstream f1("deque.in");
std::ofstream f2("deque.out");
long long int n, k, x, S = 0, cnt = 0;
std::deque<long long int> poz;
std::deque<long long int> rez;
f1 >> n >> k;
while(f1 >> x)
{
if((!rez.empty())&&(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;
}