Mentors CodeForces - 978F (思维 stl的应用)

F. Mentors
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains two integers nn and kk (2n2105(2≤n≤2⋅1050kmin(2105,n(n1)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 (1ri109)(1≤ri≤109), where riri equals to the skill of the ii-th programmer.

Each of the following kk lines contains two distinct integers xxyy (1x,yn(1≤x,y≤nxy)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.

Output

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.

Examples
input
Copy
4 2
10 4 10 15
1 2
4 3
output
Copy
0 0 1 2 
input
Copy
10 4
5 4 1 5 4 3 7 1 2 5
4 6
2 1
10 8
3 5
output
Copy
5 4 0 5 3 3 9 0 2 5 
Note

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;
}


扫描二维码关注公众号,回复: 1445591 查看本文章


猜你喜欢

转载自blog.csdn.net/qq_41874469/article/details/80559263