AtCoder Beginner Contest 128 D - equeue
#include<stdio.h> #include<math.h> #include<string.h> #include<ctype.h> #include<iostream> #include<algorithm> #include<vector> #include<queue> typedef long long ll; using namespace std; ll maxn=0; int main() { int sum,k;cin>>sum>>k; deque<ll> num; for(int i=1;i<=sum;i++) { ll temp;cin>>temp; num.push_back(temp); } for(int i=0;i<=k;i++){ for(int j=0;j<=k;j++){ if(i+j>k) continue; priority_queue<ll,vector<ll>,greater<ll>> op;//重点 deque<ll> copy=num; for(int m=0;m<i&&!copy.empty();m++) { op.push(copy.front());copy.pop_front(); } for(int m=0;m<j&&!copy.empty();m++) { op.push(copy.back());copy.pop_back(); } ll res=k-i-j; op.push(0); while(op.top()<0&&res>0&&!op.empty()){ res--;op.pop(); } ll sam=0; while(!op.empty()){ sam+=op.top();op.pop(); } maxn=max(sam,maxn); } } cout<<maxn<<"\n"; }
思路:重点是只枚举一端的长度,好让res有操作空间的trick以及双端队列stl的使用
浙公网安备 33010602011771号