Mai intai trebuie sa te autentifici.
Cod sursa(job #2033656)
| Utilizator | Data | 7 octombrie 2017 09:43:01 | |
|---|---|---|---|
| Problema | Deque | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.9 kb |
#include <bits/stdc++.h>
using namespace std;
deque <int> q;
int a[5000005];
int n, k;
long long s;
int main()
{ int i, x;
ifstream fin("deque.in");
ofstream fout("deque.out");
fin >> n >> k;
for(i = 1; i <= n; i++)
fin >> a[i];
/// prelucrez primele k elemente
for(i = 1; i <= k; i++)
{
x = a[i];
/// scot din queue toate elementele mai mari
/// sau egale cu x
while(!q.empty() && x <= a[q.back()])
q.pop_back();
q.push_back(i);
}
s += a[q.front()];
/// restul elementelor
for(i = k+1; i <= n; i++)
{
x = a[i];
while(!q.empty() && x <= a[q.back()])
q.pop_back();
q.push_back(i);
if(i - q.front() >= k)
q.pop_front();
s+=a[q.front()];
}
fout << s << "\n";
return 0;
}
