【CF421D】Bug in Code

题目

题目描述
Recently a serious bug has been found in the FOS code. The head of the F company wants to find the culprit and punish him. For that, he set up an organizational meeting, the issue is: who’s bugged the code? Each of the nn coders on the meeting said: ‘I know for sure that either xx or yy did it!’

The head of the company decided to choose two suspects and invite them to his office. Naturally, he should consider the coders’ opinions. That’s why the head wants to make such a choice that at least pp of nn coders agreed with it. A coder agrees with the choice of two suspects if at least one of the two people that he named at the meeting was chosen as a suspect. In how many ways can the head of F choose two suspects?

Note that even if some coder was chosen as a suspect, he can agree with the head’s choice if he named the other chosen coder at the meeting.

输入格式
The first line contains integers nn and pp (3<=n<=3·10^{5}; 0<=p<=n)(3<=n<=3⋅10
5
; 0<=p<=n) — the number of coders in the F company and the minimum number of agreed people.

Each of the next nn lines contains two integers x_{i}x
i

, y_{i}y
i

(1<=x_{i},y_{i}<=n)(1<=x
i

,y
i

<=n) — the numbers of coders named by the ii -th coder. It is guaranteed that x_{i}≠i, y_{i}≠i, x_{i}≠y_{i}x
i



=i, y
i



=i, x
i



=y
i

.

输出格式
Print a single integer — the number of possible two-suspect sets. Note that the order of the suspects doesn’t matter, that is, sets (1,2)(1,2) and (2,1)(2,1) are considered identical.

题意翻译
题目描述

XX公司的代码出问题了!!老板十分生气,为了有个说法,他决定选出两个背黑锅的人。

现在有n个人,每个人都会选出两个背黑锅的人,老板想要让决定背黑锅的两个人的支持数(就是有几个人想让这个人背黑锅)之和大于等于P。请问有多少种选择方案(顺序无关)

输入格式

第一行两个数n和p,后n行每行两个数xi,yi,表示想让xi和yi背锅。

输出格式

一行,一个数,表示共有多少种方案数

输入输出样例
输入 #1复制
4 2
2 3
1 4
1 4
2 1
输出 #1复制
6
输入 #2复制
8 6
5 6
5 7
5 8
6 2
2 1
7 3
1 3
1 4
输出 #2复制
1

思路

这题重点是去重
去重过程 x和y分别表示每个人的预测,(x<y),同样的一组预测x和y出现了c次,假如cnt[x]+cnt[y]>=y&&cnt[x]+cnt[y]-c<p,则ans–。

代码

#include<bits/stdc++.h>
using namespace std;
#define FFF 300005
int cnt[FFF],to[FFF];
struct node {
    int x,y;
}f[FFF];
bool cmp(node a,node b){
    if(a.x==b.x)
        return a.y<b.y;
    else
        return a.x<b.x;
}
int main()
{
    int n,p;
    scanf("%d%d",&n,&p);
    memset(cnt,0,sizeof(cnt));
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&f[i].x,&f[i].y);
        if(f[i].x>f[i].y)
        {
            int t=f[i].x;
            f[i].x=f[i].y;
            f[i].y=t;
        }
        cnt[f[i].x]++;
        cnt[f[i].y]++;
    }
    for(int i=1;i<=n;i++)to[i]=cnt[i];
    long long ans=0,now=n;
    sort(cnt+1,cnt+n+1);
    for(int i=1;i<=n;i++) {
        if(cnt[i]>=p)
            ans+=n-1;
        else{
            while(cnt[now]>=p-cnt[i])
                now--;
            if(cnt[now+1]+cnt[i]>=p){
                if(cnt[now+1]>cnt[i])
                    ans+=n-now;
                else
                    ans+=n-now-1;
            }
        }
    }
    ans/=2;
    sort(f,f+n,cmp);
    node tmp;
    tmp.x=tmp.y=0;
    memset(cnt,0,sizeof(cnt));
    int c=0;
    for(int i=0;i<n;i++)
    {
        if(tmp.x==f[i].x&&tmp.y==f[i].y)
            c++;
        else
        {
            if(tmp.x&&to[tmp.x]+to[tmp.y]>=p&&to[tmp.x]&&to[tmp.x]+to[tmp.y]-c<p)
                ans--;
            tmp.x=f[i].x;
            tmp.y=f[i].y;
            c=1;
        }
    }
    if(to[tmp.x]+to[tmp.y]>=p&&to[tmp.x]&&to[tmp.x]+to[tmp.y]-c<p)
        ans--;
    cout<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值