# Codeforces between a sereja and swaps

Source: Internet
Author: User

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;}`

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.