Codeforces 426C Sereja and Swaps【思维】

来源:互联网 发布:三星手机天气预报软件 编辑:程序博客网 时间:2024/06/05 04:43

C. Sereja and Swaps
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation:

A swap operation is the following sequence of actions:

  • choose two indexes i, j (i ≠ j);
  • perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp.

What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations?

Input

The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1]a[2]...a[n]( - 1000 ≤ a[i] ≤ 1000).

Output

In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations.

Examples
input
10 210 -1 2 2 2 2 2 2 -1 10
output
32
input
5 10-1 -1 -1 -1 -1
output
-1

题目大意:


给出长度为N的一个序列,我们现在可以交换一对数字最多K次,问交换之后,最大的连续子序列的和。


思路:


观察到n和k都不大 ,我们可以O(n^2)枚举出连续子序列和的区间【L,R】,然后将区间内最小的值,和区间外最大的值不断进行交换,维护最大值即可。


Ac代码:

#include<stdio.h>#include<string.h>#include<queue>using namespace std;int a[500];int main(){    int n,T;    while(~scanf("%d%d",&n,&T))    {        int output=-0x3f3f3f3f;        for(int i=1;i<=n;i++)scanf("%d",&a[i]);        for(int i=1;i<=n;i++)        {            for(int j=i;j<=n;j++)            {                priority_queue<int,vector<int>,less<int> >out;                priority_queue<int,vector<int>,greater<int> >in;                for(int k=1;k<=n;k++)                {                    if(k>=i&&k<=j)                    {                        in.push(a[k]);                    }                    else out.push(a[k]);                 }                 int temp=T;                 while(temp--)                 {                     if(out.size()==0||in.size()==0)continue;                     int u=out.top();                     int v=in.top();                     if(u>v)                     {                        in.pop();out.pop();                        out.push(v);in.push(u);                     }                 }                 int sum=0;                 while(!in.empty())                 {                     int u=in.top();                     sum+=u;                     in.pop();                 }                 output=max(output,sum);            }        }        printf("%d\n",output);    }}






原创粉丝点击