Sereja and Swaps(贪心+暴力枚举区间)

Sereja and Swaps

 

 AC_Code:

 

 1 //枚举区间,o(n^2),然后将区间内最小的数逐个和区间外面最大的数交换
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 typedef long long ll;
 5 const int maxn = 205;
 6 const int inf=0x3f3f3f3f;
 7 #define rep(i,first,last) for(int i=first;i<=last;i++)
 8 #define dep(i,first,last) for(int i=first;i>=last;i--)
 9 int n,k,a[maxn],b[maxn];
10 
11 int solve(int l,int r){
12     memcpy(b,a,sizeof(a));
13     int ans=0;
14     rep(i,l,r) ans+=a[i];
15     int p,q;
16     rep(i,1,k){
17         int bMax=-inf,bMin=inf;
18         rep(j,1,n){
19             if((j<l||j>r)&&b[j]>bMax ){
20                 bMax=b[j];
21                 p=j;
22             }
23             else if( j>=l && j<=r && b[j]<bMin ){
24                 bMin=b[j];
25                 q=j;
26             }
27         }
28         int t=bMax-bMin;
29         if( t<=0 ) break;
30         ans+=t;
31         swap(b[p],b[q]);
32     }
33     return ans;
34 }
35 int main()
36 {
37     scanf("%d%d",&n,&k);
38     int ans=-inf;
39     rep(i,1,n) scanf("%d",&a[i]);
40     rep(i,1,n){
41         rep(j,i,n){
42             ans=max(ans,solve(i,j));
43         }
44     }
45     printf("%d\n",ans);
46     return 0;
47 }

 

posted @ 2020-02-25 10:33  swsyya  阅读(169)  评论(0编辑  收藏  举报

回到顶部