• code forces 994B


    B. Knights of a Polygonal Table
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Unlike Knights of a Round Table, Knights of a Polygonal Table deprived of nobility and happy to kill each other. But each knight has some power and a knight can kill another knight if and only if his power is greater than the power of victim. However, even such a knight will torment his conscience, so he can kill no more than kk other knights. Also, each knight has some number of coins. After a kill, a knight can pick up all victim's coins.

    Now each knight ponders: how many coins he can have if only he kills other knights?

    You should answer this question for each knight.

    Input

    The first line contains two integers nn and k(1n105,0kmin(n1,10))(1≤n≤105,0≤k≤min(n−1,10)) — the number of knights and the number kk from the statement.

    The second line contains nn integers p1,p2,,pnp1,p2,…,pn (1pi109)(1≤pi≤109) — powers of the knights. All pipi are distinct.

    The third line contains nn integers c1,c2,,cnc1,c2,…,cn (0ci109)(0≤ci≤109) — the number of coins each knight has.

    Output

    Print nn integers — the maximum number of coins each knight can have it only he kills other knights.

    Examples
    input
    Copy
    4 2
    4 5 9 7
    1 2 11 33
    output
    Copy
    1 3 46 36 
    input
    Copy
    5 1
    1 2 3 4 5
    1 2 3 4 5
    output
    Copy
    1 3 5 7 9 
    input
    Copy
    1 0
    2
    3
    output
    Copy
    3 
    Note

    Consider the first example.

    • The first knight is the weakest, so he can't kill anyone. That leaves him with the only coin he initially has.
    • The second knight can kill the first knight and add his coin to his own two.
    • The third knight is the strongest, but he can't kill more than k=2k=2 other knights. It is optimal to kill the second and the fourth knights: 2+11+33=462+11+33=46.
    • The fourth knight should kill the first and the second knights: 33+1+2=3633+1+2=36.

    In the second example the first knight can't kill anyone, while all the others should kill the one with the index less by one than their own.

    In the third example there is only one knight, so he can't kill anyone.

     http://codeforces.com/problemset/problem/994/B

    题意:给你n个人,每个人都有一个力量值和一些金钱

    现在假设一个人可以攻击任意人,如果攻击力量值比他低的人

    就可以得到那个人的金钱,输出每个人攻击最多k个人后最多可以有

    多少金钱.

    这个题是真的坑

    最后FST了,55555,比赛的时候没有注意到一个小知识点

    求k个人可以得到最多的钱的时候如果是用二叉平衡树的set容器的话

    如果容器中有重复的元素的话会自动去重

    multiset集合容器:

    ------ 和set的区别:set容器中所有的元素必须独一无二,而multiset容器中元素可以重复

    但是由于不太会用multiset 所以使用vector,每次都排个序,一样的结果

    思路在代码中

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5+5;
    int n,k;
    struct node {
        int p,id;
    } a[maxn];
    bool cmp(node a,node b) {
        return a.p<b.p;
    }
    vector<long long> v;
    long long c[maxn];
    long long ans[maxn];
    int main() {
        ios::sync_with_stdio(false);
        cin>>n>>k;
        for(int i=0; i<n; i++) {
            cin>>a[i].p;
            a[i].id=i;
        }
        for(int i=0; i<n; i++) {
            cin>>c[i];
        }
        //k=0时特殊处理
        if(k==0) {
            for(int i=0; i<n; i++) {
                printf("%d ",c[i]);
            }
            printf("
    ");
            return 0;
        } else {
            //对力量值排序
            sort(a,a+n,cmp);
            ans[a[0].id]=0;
            long long sumk=0;//前k大和
            for(int i=1; i<n; i++) {
                if(v.size()<k) {
                    /在k个数之前时  加上前面这些数的cost
                    v.push_back(c[a[i-1].id]);
                    sumk+=c[a[i-1].id];//前i个数的和
                } else if(c[a[i-1].id]>v[0]) {
                    //前面数中最大的k个数的和
                    sumk+=c[a[i-1].id]-v[0];//更新最大值
                    v[0]=c[a[i-1].id];//更新第k大值
                }
                sort(v.begin(),v.end());//对于k个数重新排序
                ans[a[i].id]=sumk;
            }
            for(int i=0; i<n; i++) {
                printf("%lld ",ans[i]+c[i]);
            }
            printf("
    ");
    
        }
        return 0;
    }

    补上multiset的做法

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn = 1e5+5;
    int p[maxn],coin[maxn];
    
    bool cmp(int x,int y){
        return p[x]<p[y];
    }
    
    ll ans[maxn];
    int id[maxn];
    
    int main(){
        ios::sync_with_stdio(false);
        cin.tie(0);
        int n,k;
        while(cin>>n>>k){
            for(int i=0;i<n;i++){
                cin>>p[i];
            }
            for(int i=0;i<n;i++){
                cin>>coin[i];
            }
            for(int i=0;i<n;i++){
                ans[i]=coin[i];
                id[i]=i;
            }
            sort(id,id+n,cmp);
            multiset<int> s;
            multiset<int>::iterator it;
            for(int i=0;i<n;i++){
                int j=0;
                it=s.end();
                if(s.size()!=0){
                    for(it--;j<k;j++,it--){
                        ans[id[i]]+=*it;
                        if(it==s.begin()) break;
                    }
                }
                s.insert(coin[id[i]]);
            }
            for(int i=0;i<n;i++){
                cout<<ans[i]<<" ";
            }
            cout<<endl;
        }
    }
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    ADO.Net中DataTable的应用
    EasyDSS录像回看出现了推流计划之外的录像文件产生,如何解决?
    EasyDSS的点播文件分享链接如何设置自动播放?
    【解决方案】政务透明,EasyDSS在公开庭审中的应用
    EasyDSS录像计划清理功能的实现分享
    EasyDSS开发中Go语言在for循环中使用协程的注意点
    如何通过API接口获取EasyDSS单条/多条直播流信息?
    EasyDSS如何修改HLS切片时长?
    EasyDSS 2.0版本上传激活文件后提示NO DSS SERVICE是什么原因?
    EasyDSS用户登录错误失败次数过大后自动锁定功能的使用
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/9191955.html
Copyright © 2020-2023  润新知