跳到正文

题解

【题解】CF978F

SqrtSecond约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
获赞
4
题目
CF978F

版本

  1. v1当前

先假设没有人发生口角。

那么每个人的答案就是原数组中严格小于自己能力值的人数。

对于这部分,另开一个数组,sort+lower_bound可以处理。

注意 sort+lower_bound 是求原数组中第一个大于等于某个数的数的下标,如果直接用二分查找,就可能多算答案。

那么有人发生口角呢?

想象一下,如果有个人原来可以当对方的导师,但是由于发生了口角,损失的就只有他一个人。

那么,我们只需要将能力大的那个人找出来,将其答案减11即可。

那么如果两个人能力值相等呢?

显然,本来双方都不能做对方的导师,发生口角也没什么关系

那么这种情况就跳过就行啦~

code:

C++
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
	char ch=getchar();int x=0,r=1;
	while(ch<'0'||ch>'9'){if(ch=='-')r=0;ch=getchar();}
	while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return r?x:-x;
}//快读
int n,m,a[200100],b[200100],ans[200100],x,y;
signed main()
{
	n=read();m=read();
	for(int i=1;i<=n;++i)a[i]=b[i]=read();
	sort(b+1,b+n+1);
	for(int i=1;i<=n;++i)
	{
		int l=lower_bound(b+1,b+n+1,a[i])-b;//第一个等于a[i]的数的位置,即前面都小于a[i]
		ans[i]=l-1;//即有多少人比他弱
	}
	while(m--)
	{
		x=read();y=read();
		if(a[x]<a[y])ans[y]--;
		if(a[y]<a[x])ans[x]--;//将发生口角的两人关系排除
	}
	for(int i=1;i<=n;++i)printf("%d ",ans[i]);
	return 0;//养成好习惯
}
原文