[Codeforces 425A] Sereja and Swaps

[题目链接]

        https://codeforces.com/contest/425/problem/A

[算法]

         枚举最终序列的左端点和右端点 , 尝试用这段区间中小的数与区间外大的数交换

         时间复杂度 : O(N^3logN)

[代码]

        

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 210;
const int inf = 2e9;
 
int n , k;
int a[MAXN],b[MAXN],value[MAXN];

template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}

int main()
{
        
        read(n); read(k);
        for (int i = 1; i <= n; i++) read(value[i]);
        int ans = -inf;
        for (int i = 1; i <= n; i++)
        {
                for (int j = i; j <= n; j++)
                {
                        int la = 0 , lb = 0 , cnt = 0;
                        for (int x = 1; x <= n; x++)
                        {
                                if (x >= i && x <= j)
                                {
                                        a[++la] = value[x];
                                        cnt += value[x];
                                } else
                                         b[++lb] = value[x];
                        }
                        sort(a + 1,a + la + 1);
                        sort(b + 1,b + lb + 1,greater<int>());
                        for (int x = 1; x <= min(la,min(lb,k)); x++)
                        {
                                if (b[x] - a[x] > 0) 
                                        cnt += b[x] - a[x];
                                else break;
                        }
                        ans = max(ans,cnt);
                }
        }
        printf("%d\n",ans);
        
        return 0;
    
}

 

posted @ 2018-10-03 20:30  evenbao  阅读(217)  评论(0编辑  收藏  举报