Question:
A string of numbers can be exchanged for at most K times to find the maximum continuous number and the maximum number
Ideas:
N ^ 2 brute-force enumeration of all intervals. If you want to change the number, you must take the large number out of the interval and change the decimal priority queue within the interval to complete the operation.
Code:
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;#define N 201int n,m,ans;int a[N];priority_queue<int> q2;priority_queue< int , vector<int> , greater<int> > q1;int main(){ int i,j,k,sum,f1,f2; scanf("%d%d",&n,&m); for(i=1,ans=-10000;i<=n;i++) { scanf("%d",&a[i]); ans=max(ans,a[i]); } for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { while(!q1.empty()) q1.pop(); while(!q2.empty()) q2.pop(); for(k=1,sum=0;k<=n;k++) { if(k<i||k>j) q2.push(a[k]); else { q1.push(a[k]); sum+=a[k]; } } for(k=m;k&&!q1.empty()&&!q2.empty();k--) { f1=q1.top(); q1.pop(); f2=q2.top(); q2.pop(); if(f1<f2) { sum-=f1; sum+=f2; } else break; } ans=max(ans,sum); //printf("%d %d %d %d\n",i,j,sum,ans); } } printf("%d\n",ans); return 0;}