Codeforces 426C Sereja and Swaps【思维】

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 2
10 -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);
    }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值