zoukankan      html  css  js  c++  java
  • Codeforces 590D Top Secret Task

    D. Top Secret Task
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A top-secret military base under the command of Colonel Zuev is expecting an inspection from the Ministry of Defence. According to the charter, each top-secret military base must include a top-secret troop that should... well, we cannot tell you exactly what it should do, it is a top secret troop at the end. The problem is that Zuev's base is missing this top-secret troop for some reasons.

    The colonel decided to deal with the problem immediately and ordered to line up in a single line all n soldiers of the base entrusted to him. Zuev knows that the loquacity of the i-th soldier from the left is equal to qi. Zuev wants to form the top-secret troop using k leftmost soldiers in the line, thus he wants their total loquacity to be as small as possible (as the troop should remain top-secret). To achieve this, he is going to choose a pair of consecutive soldiers and swap them. He intends to do so no more than s times. Note that any soldier can be a participant of such swaps for any number of times. The problem turned out to be unusual, and colonel Zuev asked you to help.

    Determine, what is the minimum total loquacity of the first k soldiers in the line, that can be achieved by performing no more than s swaps of two consecutive soldiers.

    Input

    The first line of the input contains three positive integers nks (1 ≤ k ≤ n ≤ 150, 1 ≤ s ≤ 109) — the number of soldiers in the line, the size of the top-secret troop to be formed and the maximum possible number of swap operations of the consecutive pair of soldiers, respectively.

    The second line of the input contains n integer qi (1 ≤ qi ≤ 1 000 000) — the values of loquacity of soldiers in order they follow in line from left to right.

    Output

    Print a single integer — the minimum possible total loquacity of the top-secret troop.

    Examples
    input
    3 2 2
    2 4 1
    output
    3
    input
    5 4 2
    10 1 6 2 5
    output
    18
    input
    5 2 3
    3 1 4 2 5
    output
    3
    Note

    In the first sample Colonel has to swap second and third soldiers, he doesn't really need the remaining swap. The resulting soldiers order is: (2, 1, 4). Minimum possible summary loquacity of the secret troop is 3. In the second sample Colonel will perform swaps in the following order:

    1. (10, 1, 6 — 2, 5)
    2. (10, 1, 2, 6 — 5)

    The resulting soldiers order is (10, 1, 2, 5, 6).

    Minimum possible summary loquacity is equal to 18.

    题目链接 

    题意 

    给n个数,至多进行s次相邻元素交换的操作,问前k个元素的最小和是多少?

    参考:http://blog.csdn.net/Z_Mendez/article/details/49512305

    分析 

    设状态dp[i][j]为前i个数交换了j次的最小和。转移方程为dp[i+1][j+l-(i+1)]=min(dp[i+1][j+l-(i+1)],dp[i][j]+a[l]).为了没有后效性,先从小到大枚举l,表示从哪里交换过来,然后从大到小枚举i,避免数被重复添加到前k个里。由冒泡排序可知,n个元素的最多交换次数为n*(n-1),即使s很大,也并不会超时。

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    #include<algorithm>
    #include<cstring>
    #include <queue>
    #include <vector>
    #include<bitset>
    #include<map>
    using namespace std;
    typedef long long LL;
    const int maxn = 5e5+5;
    const int mod = 772002+233;
    typedef pair<int,int> pii;
    #define X first
    #define Y second
    #define pb push_back
    #define mp make_pair
    #define ms(a,b) memset(a,b,sizeof(a))
    const int inf = 0x3f3f3f3f;
    #define lson l,m,2*rt
    #define rson m+1,r,2*rt+1
    
    int a[200],dp[200][200*200];
    
    int main(){
        int n,k,s;
        scanf("%d%d%d",&n,&k,&s);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        ms(dp,inf);
        dp[0][0]=0;
        for(int l=1;l<=n;l++){
            for(int i=l-1;i>=0;i--){
                for(int j=0;j<=i*l;j++){
                    dp[i+1][j+l-(i+1)]=min(dp[i+1][j+l-(i+1)],dp[i][j]+a[l]);
                }
            }
        }
        int ans=inf;
        for(int i=0;i<=min(s,n*n);i++) ans=min(dp[k][i],ans);
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    Docker磁盘垃圾清理
    什么是容器编排?
    Docker 容器连接
    docker入门操作整理
    Docker学习的几个建议和技巧
    支付清结算之电商侧处理
    在Linux 中进入单用户模式的技巧
    教你如何构建异步服务器和客户端的 Kotlin 框架 Ktor
    NetSuite助力各行业企业快速发展
    linux需要你的不懈努力
  • 原文地址:https://www.cnblogs.com/fht-litost/p/8572398.html
Copyright © 2011-2022 走看看