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

来源:互联网 发布:哈佛大学 知乎 编辑:程序博客网 时间:2024/06/02 18:09

原题链接

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

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

#include <bits/stdc++.h>#define maxn 205#define MOD 1000000007using 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;}


0 0
原创粉丝点击