zoukankan      html  css  js  c++  java
  • Codeforces 994B. Knights of a Polygonal Table

    解题思路

    1. 将骑士按力量从小到大排序,到第i个骑士的时候,前面的i-1个骑士他都可以击败,找出金币最多的k个。
    2. 用multiset存金币最多的k个骑士的金币数,如果多余k个,则删除金币数最小的,直到只有k个数字。

    我就是因为没有用multiset在最后5分钟被hack了。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    int n,k;
    //输入,p表示力量,c表示金币,idx表示输入时的位置 
    struct node{
    	ll p;ll c;int idx;	
    }a[500050];
    
    bool cmp(node x, node y){
    	return x.p < y.p;
    }
    ll ans[500050];
    int main(){
    	ios::sync_with_stdio(false); 
    	cin >> n >> k;
    	//输入并按力量从小到大排序
    	for(int i = 1;i <= n; ++i) cin >> a[i].p, a[i].idx = i;
    	for(int i = 1;i <= n; ++i) cin >> a[i].c;
    	sort(a+1,a+1+n,cmp);
    	
    	multiset <int> s; //存第i个骑士可以击败的不超过k个骑士的金币数 
    	for(int i = 1;i <= n; ++i){
    		ans[a[i].idx] = a[i].c; 
    		for(auto t:s){
    			ans[a[i].idx] += t;
    		}
    		s.insert(a[i].c);
    		//如果将第i个骑士的金币数插入之后大于k个数字,就删除到只有k个 
    		while(s.size() > k){
    			s.erase(s.begin());
    		}
    	}
    	for(int i = 1;i <= n; ++i) cout << ans[i] << " ";
    	cout << endl;
        return 0;
    }
    
  • 相关阅读:
    hdu1507
    zoj1654
    hdu2444
    poj3692
    hdu1150
    hdu1151
    poj2771
    hdu3829
    hdu4619
    hdu4715
  • 原文地址:https://www.cnblogs.com/zhangjiuding/p/9192555.html
Copyright © 2011-2022 走看看