Codeforces Round #243 (Div. 2)-C. Sereja and Swaps(multiset)

原题链接

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

枚举所有区间[l, r]把该区间的数放在集合s1中,剩下的数放在s2集合中,取出s2中最大的数和s1最小的数进行交换,重复最多k次

#include <bits/stdc++.h>
#define maxn 205
#define MOD 1000000007
using namespace std;
typedef long long ll;

int n, k;
int num[maxn];
multiset<int> s1, s2;
int main(){
	
//	freopen("in.txt", "r", stdin);
	scanf("%d%d", &n, &k);
	for(int i = 0; i < n; i++)
	 scanf("%d", num+i);
	
	int ans = -100000000, sum;
	set<int> ::iterator iter1, iter2;
	for(int i = 0; i < n; i++){
	 s1.clear();
	 s2.clear();
	 sum = 0;
	 for(int j = 0; j < n; j++)
	  s2.insert(num[j]);
	 for(int j = i; j < n; j++){
	 	sum += num[j]; 
	 	s1.insert(num[j]);
	 	iter2 = s2.lower_bound(num[j]);
	 	s2.erase(iter2);
	 	int m = k, p = sum;
	 	iter1 = s1.begin();
	 	if(s2.empty()){
	 		ans = max(sum, ans);
	 		break;
	 	}
	 	iter2 = --s2.end();
	 	while(m-- && iter1 != s1.end()){
	 		if(*iter1 < *iter2){
	 			if(iter2 == s2.begin())
	 			 m = 0;
	 			p += *iter2 - *iter1;
	 			iter1++;
	 			iter2--;
	 		}
	 		else break;
	 	}
	 	ans = max(ans, p);
	 }
   }
   printf("%d\n", ans);
   return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值