Cod sursa(job #1475364)
| Utilizator | Data | 23 august 2015 23:23:04 | |
|---|---|---|---|
| Problema | Deque | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.57 kb |
#include <fstream>
#include <deque>
using namespace std;
const int nmax = 5000005;
int a[nmax], n, k;
long long sol;
deque <int> d;
int main()
{
ifstream fin("deque.in");
ofstream fout("deque.out");
int i;
fin >> n >> k;
for(i=1; i<=n; i++)
fin >> a[i];
for(i=1; i<=n; i++)
{
while(!d.empty() && a[i]<=a[d.back()])
d.pop_back();
d.push_back(i);
if(d.front()==i-k)d.pop_front();
if(i>=k)sol+=a[d.front()];
}
fout << sol;
fin.close();
fout.close();
return 0;
}
