Codeforces Round #243 (Div. 2) C. Sereja and Swaps 解题报告

11 篇文章 0 订阅

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.

Sample test(s)
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

    解题报告: 昨晚一直没想出来怎么做……一大憾事。写了个随机化一直WA,直接暴力也是一直超时……

    今天看了队友的代码,发现思想还是很简单的:选定一个区间[i, j],则剩下的区间为[1, i-1]和[j+1, n],将选定区间的最小值与剩下区间的最大值交换,使选定区间的和越来越大。非常好的暴力方法啊。复杂度(n^2)*(nlogn + k*logn)。

    清晰易懂的思路,简单易实现的代码,真是个非常好的“暴力”算法~

    好吧,今天写的代码贴一个,用优先队列存储区间的最大值和最小值,最小值用一个数的负数获得,用的时候再反转就好了。

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

int num[222];
int sum[222];

void work(int n, int m)
{
    for(int i=1;i<=n;i++)
    {
        scanf("%d", num+i);
        sum[i]=sum[i-1]+num[i];
    }

    int ans=-1e8;
    for(int i=1;i<=n;i++) for(int j=i;j<=n;j++)
    {
        int tmp=sum[j]-sum[i-1];

        priority_queue<int> Max, Min;
        for(int k=i;k<=j;k++) Min.push(-num[k]);
        for(int k=1;k<i;k++) Max.push(num[k]);
        for(int k=j+1;k<=n;k++) Max.push(num[k]);

        for(int i=0;i<m && !Max.empty() && !Min.empty() && Max.top()+Min.top()>0;i++)
        {
            tmp+=Max.top()+Min.top();
            Max.pop();
            Min.pop();
        }

        ans=max(ans, tmp);
    }
    printf("%d\n", ans);
}

int main()
{
    int n, k;
    while(~scanf("%d%d", &n, &k))
        work(n, k);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值