codeforces C. Sereja and Swaps

http://codeforces.com/contest/426/problem/C

 题意:找出连续序列的和的最大值,可以允许交换k次任意位置的两个数。

思路:枚举区间,依次把区间内的比较小的数换成区间外的比较大的数。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <queue>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 int a[300],b[300],c[300];
 9 int n,kk;
10 bool cmp(int aa,int bb)
11 {
12     return aa>bb;
13 }
14 
15 int main()
16 {
17     cin>>n>>kk;
18     int ans=-1000;
19     for(int i=1; i<=n; i++)
20     {
21        cin>>a[i];
22        ans=max(ans,a[i]);
23     }
24     for(int i=1; i<=n; i++)
25     {
26         for(int j=i+1; j<=n; j++)
27         {
28             int t=0;
29             memset(b,0,sizeof(b));
30             memset(c,0,sizeof(c));
31             for(int k=i; k<=j; k++)
32             {
33                 b[t++]=a[k];
34             }
35             int t1=0;
36             for(int x=1; x<i; x++)
37             {
38                 c[t1++]=a[x];
39             }
40             for(int x=j+1; x<=n; x++)
41             {
42                 c[t1++]=a[x];
43             }
44             sort(b,b+t);
45             sort(c,c+t1,cmp);
46             int sum=0;
47             for(int x=1; x<=min(kk,t); x++)
48             {
49                 if(b[x-1]<c[x-1]&&x<=t1)
50                 {
51                     sum+=c[x-1];
52                 }
53                 else
54                     sum+=b[x-1];
55             }
56             for(int x=min(kk,t); x<t; x++)
57             {
58                 sum+=b[x];
59             }
60             ans=max(ans,sum);
61         }
62     }
63     printf("%d\n",ans);
64     return 0;
65 }
View Code

 

posted @ 2015-03-02 21:15  null1019  阅读(177)  评论(0编辑  收藏  举报