zoukankan      html  css  js  c++  java
  • [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;
        
    }
  • 相关阅读:
    Oracle适配问题解决
    Oracle12C创建视图权限不足
    Oracle12C配置对外访问
    Oracle12C创建scott账户
    Oracle12C安装配置文档
    Redis适配采坑记
    Redis安装问题解决方案
    Redis Linux 安装部署
    【计网 第四章-2】
    【信息论编码2】测度论
  • 原文地址:https://www.cnblogs.com/evenbao/p/9740781.html
Copyright © 2011-2022 走看看