2021.01.28 Rating赛

A - A

 CodeForces - 994A 

You are locked in a room with a door that has a keypad with 10 keys corresponding to digits from 0 to 9. To escape from the room, you need to enter a correct code. You also have a sequence of digits.

Some keys on the keypad have fingerprints. You believe the correct code is the longest not necessarily contiguous subsequence of the sequence you have that only contains digits with fingerprints on the corresponding keys. Find such code.

Input

The first line contains two integers nn and mm (1n,m101≤n,m≤10) representing the number of digits in the sequence you have and the number of keys on the keypad that have fingerprints.

The next line contains nn distinct space-separated integers x1,x2,,xnx1,x2,…,xn (0xi90≤xi≤9) representing the sequence.

The next line contains mm distinct space-separated integers y1,y2,,ymy1,y2,…,ym (0yi90≤yi≤9) — the keys with fingerprints.

Output

In a single line print a space-separated sequence of integers representing the code. If the resulting sequence is empty, both printing nothing and printing a single line break is acceptable.

Examples

Input
7 3
3 5 7 1 6 2 8
1 2 7
Output
7 1 2
Input
4 4
3 4 1 0
0 1 7 9
Output
1 0

Note

In the first example, the only digits with fingerprints are 11, 22 and 77. All three of them appear in the sequence you know, 77 first, then 11 and then 22. Therefore the output is 7 1 2. Note that the order is important, and shall be the same as the order in the original sequence.

In the second example digits 00, 11, 77 and 99 have fingerprints, however only 00 and 11 appear in the original sequence. 11 appears earlier, so the output is 1 0. Again, the order is important.

解题思路:给两组数,第一组数是键盘上的数字,第二组数是带指纹的数字,要按第一组数出现的顺序输出第二组数。

使用map记录第二组数,按原顺序遍历第一组数,若map值不为0,则输出该数即可。

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
int main(){
    int x,y,i,j,a[100],b;
    cin>>x>>y;
    map<int,int> mp;
    mp.clear();
    for(i=0;i<x;i++){
        cin>>a[i];
    }
    for(j=0;j<y;j++){
        cin>>b;
        mp[b]=1;
    }
    for(i=0;i<x;i++){
        if(mp[a[i]]==1){
            cout<<a[i]<<" ";
        }
    }
    cout<<endl;
    return 0;
}
View Code

 

B - B

 CodeForces - 994B 

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
4 2
4 5 9 7
1 2 11 33
Output
1 3 46 36 
Input
5 1
1 2 3 4 5
1 2 3 4 5
Output
1 3 5 7 9 
Input
1 0
2
3
Output
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.

解题思路:给出n和k,有n个骑士,每个骑士的战力为ai,有bi的钱,如果一个骑士的战力比另一个骑士的战力高,就可以夺取这个骑士的钱,但是每个骑士最多夺取k个人,求这些骑士最多可以获得多少钱。

每个骑士都要从比他战力低的人中选出k个,且要选取前k个金币数最多的,一开始自己只有思路却具体不知道怎么做,赛后学习其他博客得知用优先队列来做,从战力最小的开始入队,每次取出前k个可掠夺的骑士,这样就保证了队列中的骑士都是可以被掠夺的,并且,取出的k个骑士的钱一定是前k大的。(要注意的是,每次取出队列中的数,还要重新加回去!!因为是求最大可能!)

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Max=1e5+10;
typedef pair<int,int> P;
bool cmp(P a,P b){ return a.second<b.second;}
P p[Max],tmp[15];
int a[Max];
map<int,ll> mp;
int main()
{
    ios::sync_with_stdio(false);
    priority_queue<P> que;//对于pair 优先队列自动按firs从大到小取!
    int n,k,t,x,cnt;
    mp.clear();
    cin>>n>>k;
    for(int i=0;i<n;i++) {cin>>p[i].second;a[i]=p[i].second;}//a数组是用来保证输出和输入对应的
    for(int i=0;i<n;i++) {cin>>p[i].first;mp[p[i].second]=p[i].first;}
    sort(p,p+n,cmp);//按战力升序排序
    for(int i=0;i<n;i++){
        t=k,cnt=0;
        while(que.size()&&t--){//选k个战力比自己小的骑士掠夺
            P x=que.top();
            que.pop();
            mp[p[i].second]+=x.first;
            tmp[cnt++]=x;//记录被掠夺的骑士,后面需要重新入队
        }
        que.push(p[i]);//自己入队
        for(int j=0;j<cnt;j++) que.push(tmp[j]);//重新入队
    }
    for(int i=0;i<n;i++) cout<<mp[a[i]]<<" "; cout<<endl;
    return 0;
}
View Code

 

posted @ 2021-02-07 13:47  nanmoon  阅读(63)  评论(0编辑  收藏  举报