In BerSoft nn programmers work, the programmer ii is characterized by a skill riri.
A programmer aa can be a mentor of a programmer bb if and only if the skill of the programmer aa is strictly greater than the skill of the programmer bb (ra>rb)(ra>rb) and programmers aa and bb are not in a quarrel.
You are given the skills of each programmers and a list of kk pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer ii, find the number of programmers, for which the programmer ii can be a mentor.
The first line contains two integers nn and kk (2≤n≤2⋅105(2≤n≤2⋅105, 0≤k≤min(2⋅105,n⋅(n−1)2))0≤k≤min(2⋅105,n⋅(n−1)2)) — total number of programmers and number of pairs of programmers which are in a quarrel.
The second line contains a sequence of integers r1,r2,…,rnr1,r2,…,rn (1≤ri≤109)(1≤ri≤109), where riri equals to the skill of the ii-th programmer.
Each of the following kk lines contains two distinct integers xx, yy (1≤x,y≤n(1≤x,y≤n, x≠y)x≠y) — pair of programmers in a quarrel. The pairs are unordered, it means that if xx is in a quarrel with yy then yy is in a quarrel with xx. Guaranteed, that for each pair (x,y)(x,y) there are no other pairs (x,y)(x,y) and (y,x)(y,x) in the input.
Print nn integers, the ii-th number should be equal to the number of programmers, for which the ii-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input.
4 2 10 4 10 15 1 2 4 3
0 0 1 2
10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5
5 4 0 5 3 3 9 0 2 5
In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
题意:给出n个人的能力值,给出m对关系,如果一个人的能力值比另一个人的能力值高,并且他们两没有关系的话,那么,这个人就可以成为那个人的导师。求出每个人最多可以是几个人的导师。
思路:结构体来记录下标,能力值,和结果。然后按能力值升序排序一下,用排序后的下标减去有关系的人的个数就是答案了。在这里,我们只记录从能力值高的到能力值低的人之间的关系。反过来不记录。如果反过来也记录的话,就会多减去一些值!注意,这里如果有能力值相同的,也会被计算,所以用一个lower_bound来找出当前节点之前与之能力值相同的点的个数,并将之减去。(在这里,我不会在结构体里面用lower_bound查找,所以重新开了一个一模一样的数组来查找 真丢人呐!!)最后,按下标排序,输出即可。
#include "iostream" #include "algorithm" #include "vector" using namespace std; const int Max=2e5+10; struct pot { int date,index; int ans; }; pot tmp[Max]; bool cmp1(pot a,pot b) { return a.date<b.date; } bool cmp2(pot a,pot b) { return a.index<b.index; } vector<int> G[Max]; int a[Max]; int main() { int n,m; cin>>n>>m; for(int i=1;i<=n;i++){ cin>>tmp[i].date; a[i]=tmp[i].date; tmp[i].index=i; tmp[i].ans=0; G[i].clear(); } int u,v; for(int i=1;i<=m;i++){ cin>>u>>v; if(tmp[u].date>tmp[v].date)//只记录能力值从高到低的关系 G[u].push_back(v); else if(tmp[u].date<tmp[v].date) G[v].push_back(u); } sort(tmp+1,tmp+1+n,cmp1); sort(a+1,a+1+n); for(int i=2;i<=n;i++){ int x=a[i]; if(x==a[1]) continue; int k=i-(lower_bound(a+1,a+1+n,x)-a);//找出之前,与之能力值相通的点的个数 int y=i-G[tmp[i].index].size()-1-k; tmp[i].ans=y; } sort(tmp+1,tmp+1+n,cmp2); for(int i=1;i<=n;i++){ cout<<tmp[i].ans; if(i!=n) cout<<" "; } cout<<endl; return 0; }