Pagini recente » Cod sursa (job #2504174) | Profil programator Brainfu*k profesionist | Cod sursa (job #662688) | Cod sursa (job #1090561) | Cod sursa (job #2927793)
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
const int Nmax=5e6;
int V[Nmax+3],n,k;
deque < pair<int,int> > Q;
int S=0;
int main()
{
f >> n >> k;
for(int i=1; i<=n; i++)
f >> V[i];
for(int i=1; i<=k; i++)
{
while(Q.size() && Q.back().first>=V[i])
Q.pop_back();
Q.push_back(make_pair(V[i],i));
}
S+=Q.front().first;
for(int i=k+1; i<=n; i++)
{
while(Q.size() && Q.back().first >= V[i])
Q.pop_back();
Q.push_back(make_pair(V[i],i));
if(Q.front().second<i-k+1)
Q.pop_front();
S+=Q.front().first;
cout << Q.front().first << " " << Q.front().second << " " << i << "\n";
}
g << S;
return 0;
}