标签:优先队列 contain isp thml pow out eve escape size
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 (1≤n,m≤101≤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 (0≤xi≤90≤xi≤9) representing the sequence.
The next line contains mm distinct space-separated integers y1,y2,…,ymy1,y2,…,ym (0≤yi≤90≤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
7 3
3 5 7 1 6 2 8
1 2 7
7 1 2
4 4
3 4 1 0
0 1 7 9
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; }
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 kk (1≤n≤105,0≤k≤min(n?1,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 (1≤pi≤109)(1≤pi≤109) — powers of the knights. All pipi are distinct.
The third line contains nn integers c1,c2,…,cnc1,c2,…,cn (0≤ci≤109)(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
4 2
4 5 9 7
1 2 11 33
1 3 46 36
5 1
1 2 3 4 5
1 2 3 4 5
1 3 5 7 9
1 0
2
3
3
Note
Consider the first example.
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; }
标签:优先队列 contain isp thml pow out eve escape size
原文地址:https://www.cnblogs.com/nanan/p/14382829.html