Mai intai trebuie sa te autentifici.
Cod sursa(job #2657232)
| Utilizator | Data | 10 octombrie 2020 09:59:54 | |
|---|---|---|---|
| Problema | Deque | Scor | 60 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 1.32 kb |
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;
deque<pair<int,long long>>dq;
ifstream f("deque.in");
ofstream g("deque.out");
long long n, k,suma;
void rezolvare(){
f>>n>>k;
int x;
f>>x;
dq.emplace_back(0,x);
for(int i=1; i<k; i++){
f>>x;
if(i-dq.front().first>=k)
dq.pop_front();
if(x<dq.front().second){
while (!dq.empty() && x<dq.front().second)
dq.pop_front();
dq.emplace_front(i,x);
}
else{
if(x<dq.back().second){
while (!dq.empty() &&x<dq.back().second)
dq.pop_back();
}
dq.emplace_back(i,x);
}
}
suma+=dq.front().second;
for(int i=k; i<n; i++){
f>>x;
if(i-dq.front().first>=k)
dq.pop_front();
if(x<dq.front().second){
while (!dq.empty() &&x<dq.front().second)
dq.pop_front();
dq.emplace_front(i,x);
}
else{
if(x<dq.back().second){
while (!dq.empty() &&x<dq.back().second)
dq.pop_back();
}
dq.emplace_back(i,x);
}
suma+=dq.front().second;
}
g<<endl<<suma<<endl;
}
int main() {
rezolvare();
return 0;
}
