[Codeforces 425A] Sereja and Swaps

Posted evenbao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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
",ans);
        
        return 0;
    
}

 

以上是关于[Codeforces 425A] Sereja and Swaps的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 380C Sereja and Brackets(扫描线+树状数组)

Sereja and Swaps CodeForces - 426C

CodeForces - 367E:Sereja and Intervals(组合数&&DP)

Codeforces Round #243 (Div. 2)——Sereja and Table

Codeforces Round #243 (Div. 1)——Sereja and Squares

CodeForces - 367C Sereja and the Arrangement of Numbers