概述
题目链接:http://codeforces.com/problemset/problem/425/A
A. 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
题意:
求一个序列交换K次后的最大连续子序列和!
PS:
暴力枚举每一个区间!
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1010;
#define INF 0x3f3f3f3f
int MAX(int a, int b)
{
if(a > b)
return a;
return b;
}
int n, k;
int a[maxn];
int b[maxn], c[maxn];
int l1 = 0, l2 = 0;
int findd(int l, int r)
{
for(int i = 1; i <= n; i++)
{
if(i >= l && i <= r)
{
b[++l1] = a[i];
}
else
{
c[++l2] = a[i];
}
}
if(l1 < l2)
return l1;
return l2;
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
int maxans = -INF;
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++)
{
l1 = 0, l2 = 0;
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
int num = findd(i, j);
sort(b+1, b+l1+1);
sort(c+1, c+l2+1);
if(num > k)
{
num = k;
}
for(int h = 1; h <= num; h++)
{
if(c[l2-h+1] > b[h])
{
int tt = b[h];
b[h] = c[l2-h+1];
c[l2-h+1] = tt;
}
}
int tsum = 0;
for(int l = 1; l <= l1; l++)
{
tsum += b[l];
}
maxans = MAX(maxans,tsum);
}
}
printf("%dn",maxans);
}
return 0;
}
/*
10 2
10 -1 2 2 2 2 2 2 -1 10
5 10
-1 -1 -1 -1 -1
1 1
-1
3 2
1 -1 2
3 1
1 -1 2
5 2
-1 -4 -5 -2 -3
*/
最后
以上就是紧张大象为你收集整理的CodeForces A. Sereja and Swaps(暴力+贪心)的全部内容,希望文章能够帮你解决CodeForces A. Sereja and Swaps(暴力+贪心)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复