codeforces 421D bug in code

题目链接:http://codeforces.com/problemset/problem/421/D

题目大意:每个人说出自己认为的背锅的两个人,最后大BOSS找两个人来背锅,要求至少符合p个人的想法。最终选出的两个人中只有有一个在自己的预测内就算符合想法。

解题思路:统计每个人背锅的次数,排个序。找出相加大于等于p的对数。

    然后去重:需要去重的原因,假如有且只有两个人的预测都是 1和2 (其他人的预测不涉及1,、2),则1、2这对组合在我们的计算中符合度为4,实际符合度为2,因此需 要去重

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

代码如下:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
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;
            }
        }
        // cout<<"now="<<now<<" cnt[now]="<<cnt[now]<<" ans="<<ans<<endl;
    }
    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;
        }
        // cout<<"tmp.x="<<tmp.x<<" tmp.y="<<tmp.y<<" ans="<<ans<<endl;
    }
    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、付费专栏及课程。

余额充值