tanasaradu

Untitled

Oct 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. ifstream fin("deque.in");
  4. ofstream fout("deque.out");
  5. const int NMAX=5000005;
  6. deque<int>Q;
  7. int n,k,a[NMAX];
  8. long long sol;
  9. int main()
  10. {
  11. fin>>n>>k;
  12. for(int i=1;i<=k;i++)
  13. {
  14. int x;
  15. fin>>a[i];
  16. x=a[i];
  17. while(!Q.empty() && a[Q.back()]>=x)
  18. Q.pop_back();
  19. Q.push_back(i);
  20. }
  21. sol+=(a[Q.front()]);
  22. for(int i=k+1;i<=n;i++)
  23. {
  24. int x;
  25. fin>>a[i];
  26. x=a[i];
  27. while(!Q.empty() && a[Q.back()]>=x)
  28. Q.pop_back();
  29. Q.push_back(i);
  30. if(i-Q.front()>=k)
  31. Q.pop_front();
  32. sol+=a[Q.front()];
  33. }
  34. fout<<sol<<"\n";
  35. fin.close();
  36. fout.close();
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment