题意:
有 n n n个人,每个人都投票给了两个人,求有多少对 ( x , y ) (x,y) (x,y) 满足其投票数相加 ≥ p \geq p ≥p 。
题解:
按投票数排序,二分求出 c n t [ x ] + c n t [ y ] ≥ p cnt[x]+cnt[y] \geq p cnt[x]+cnt[y]≥p 的个数。
但是难点在于去重,比如对于 ( 1 , 2 ) (1,2) (1,2) ,假如只有一个人投票给了 ( 1 , 2 ) (1,2) (1,2) ,其他人都没有投给1,2 ,那么计算出来的 c n t [ 1 ] + c n t [ 2 ] = 2 cnt[1]+cnt[2] = 2 cnt[1]+cnt[2]=2 ,但实际上为 1 1 1. 所以要求出 c n t [ x ] + c n t [ y ] − m p [ x , y ] ≥ p cnt[x]+cnt[y] -mp[x,y] \geq p cnt[x]+cnt[y]−mp[x,y]≥p的对数
所以考虑容斥,先求出所有 c n t [ x ] + c n t [ y ] ≥ p cnt[x]+cnt[y] \geq p cnt[x]+cnt[y]≥p 的对数,再去求出满足 c n t [ x ] + c n t [ y ] ≥ p cnt[x]+cnt[y] \geq p cnt[x]+cnt[y]≥p的条件下, c n t [ x ] + c n t [ y ] − m p [ x , y ] < p cnt[x]+cnt[y]-mp[x,y] < p cnt[x]+cnt[y]−mp[x,y]<p 的对数,然后减去即可。因为最多只有 n n n 对 m p [ x , y ] mp[x,y] mp[x,y] ,所以遍历一遍即可。
代码:
#pragma GCC diagnostic error "-std=c++11"
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<ctime>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
const int mod=1e9+7;
const int MAXN=3e5+5;
const int inf=0x3f3f3f3f;
int vis[MAXN];
map<pii,int>mp;
int a[MAXN];
int main()
{
int n,p;
cin>>n>>p;
for(int i=1;i<=n;i++)
{
int u,v;
cin>>u>>v;
vis[u]++;
vis[v]++;
if(u>v) swap(u,v);
mp[{u,v}]++;
}
for(int i=1;i<=n;i++)
{
a[i]=vis[i];
}
sort(a+1,a+1+n);
ll ans=0;
for(int i=1;i<=n;i++)
{
int res=max(0,p-a[i]);
int l=1,r=i-1;
int pos=i;
while(l<=r)
{
int mid=(l+r)>>1;
if(a[mid]>=res)
{
pos=mid;
r=mid-1;
}
else l=mid+1;
}
ans+=i-pos;
}
for(auto i:mp)
{
int u=i.first.first;
int v=i.first.second;
if(vis[u]+vis[v]>=p&&vis[u]+vis[v]-i.second<p){
ans--;
}
}
cout<<ans<<endl;
}