zoukankan      html  css  js  c++  java
  • Codeforces Round #243 (Div. 1)A. Sereja and Swaps 暴力

    A. Sereja and Swaps
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation:

    A swap operation is the following sequence of actions:

    • choose two indexes i, j (i ≠ j);
    • perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp.

    What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations?

    Input

    The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≤ a[i] ≤ 1000).

    Output

    In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations.

    Sample test(s)
    Input
    10 2
    10 -1 2 2 2 2 2 2 -1 10
    Output
    32
    Input
    5 10
    -1 -1 -1 -1 -1
    Output
    -1

    题意:给你一串数列,允许换K次位置,然后让你求一个最大的连续和
    题解:暴力枚举区间就是!
    int a[maxn];
    vector<int>kiss;
    vector<int>miss;
    int main()
    {
        int n,m,ans,i,j,k;
        while(cin>>n>>m)
        {
            for(i=1;i<=n;i++)
                scanf("%d",&a[i]);
            ans=-inf;
            for(i=1;i<=n;i++)
            {
                int sum=0;
                for(j=i;j<=n;j++)
                {
                    kiss.clear();
                    miss.clear();
                    sum=0;
                    for(k=i;k<=j;k++)
                    {
                        kiss.push_back(a[k]);
                        sum+=a[k];
                    }
                    for(k=1;k<=n;k++)
                    {
                        if(k<i||k>j)
                            miss.push_back(a[k]);
                    }
                    sort(kiss.begin(),kiss.end());
                    sort(miss.begin(),miss.end());
                    ans=max(ans,sum);
                    for(k=1;k<=m&&k<=kiss.size()&&k<=miss.size();k++)
                    {
                        sum-=kiss[k-1];
                        sum+=miss[miss.size()-k];
                        ans=max(ans,sum);
                    }
                }
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Redis 优缺点
    如何保证接口的幂等性。。。。。
    自动化部署 jenkins 插件简介
    JWT与Session比较和作用
    代码注释鉴赏,喜欢就拿去用!
    python中计时模块timeit的使用方法
    【Java】JavaIO(二)、节点流
    【Java】JavaIO(一)、基础知识
    【Git】四、Git工作
    【Git】三、工作区、暂存区、版本库
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4314164.html
Copyright © 2011-2022 走看看