P5123 [USACO18DEC]Cowpatibility (容斥 or bitset暴力)

题目描述

研究证明,有一个因素在两头奶牛能否作为朋友和谐共处这方面比其他任何因素都来得重要——她们是不是喜欢同一种口味的冰激凌!

Farmer John 的 NN 头奶牛(2\le N\le 5\times 10^42≤N≤5×104)各自列举了她们最喜欢的五种冰激凌口味的清单。为使这个清单更加精炼,每种可能的口味用一个不超过 10^6106 的正整数 \texttt{ID}ID 表示。如果两头奶牛的清单上有至少一种共同的冰激凌口味,那么她们可以和谐共处。

请求出不能和谐共处的奶牛的对数。

输入输出格式

输入格式:

 

输入的第一行包含 NN。以下 NN 行每行包含 55 个整数(各不相同),表示一头奶牛最喜欢的冰激凌口味。

 

输出格式:

 

输出不能和谐共处的奶牛的对数。

 

输入输出样例

输入样例#1: 复制

4
1 2 3 4 5
1 2 3 10 8
10 9 8 7 6
50 60 70 80 90

输出样例#1: 复制

4

说明

在这里,奶牛 44 不能和奶牛 11、22、33 中的任一头和谐共处,奶牛 11 和奶牛 33 也不能和谐共处。

题解1:首先能想到容斥的思路,总共的对数是n*(n-1)/2,然后减去融洽的就可以了,但是不会写....看了题解才发现可以用string来处理容斥的部分....(好骚),具体实现直接看代码就可以了.

#include<bits/stdc++.h>
using namespace std;
#define Sheryang main
const int maxn=1e6+7;
typedef long long ll;
const int mod=1e9+7;
#define IO cin.tie(0),ios::sync_with_stdio(false);
#define pi acos(-1)
#define PII pair<ll,ll>
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();if(c == '-')Nig = -1,c = getchar();while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();return Nig*x;}
#define read read()
/** keep hungry and keep calm! **/

string a[10];
int Sheryang(){
    int n;scanf("%d",&n);
    ll ans=1LL*n*(n-1)/2;

    map<string,int>mp;
    for(int i=1;i<=n;i++){
        for(int j=0;j<5;j++){
            cin>>a[j];
        }
        sort(a,a+5);
        ll tmp=0;
        for(int j=1;j<(1<<5);j++){
            int cnt=0;string s;
            for(int k=0;k<5;k++){
                if(j&(1<<k)){
                    cnt++;
                    s+=a[k];
                }
            }
            if(cnt&1) tmp+=mp[s]++;
            else tmp-=mp[s]++;
        }
        ans-=tmp;
    }

    printf("%lld\n",ans);
    return 0;
}

 

第二种思路:直接使用bitset进行暴力,mp[i]代表喜欢i这个元素的人的集合,然后直接bitset暴力就可以了。

#include<bits/stdc++.h>
using namespace std;
#define Sheryang main
const int maxn=1e6+7;
typedef long long ll;
const int mod=1e9+7;
#define IO cin.tie(0),ios::sync_with_stdio(false);
#define pi acos(-1)
#define PII pair<ll,ll>
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();if(c == '-')Nig = -1,c = getchar();while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();return Nig*x;}
#define read read()
/** keep hungry and keep calm! **/

map<int,bitset<50005> >mp;
int a[50005][6];
int Sheryang(){
    int n=read;

    for(int i=1;i<=n;i++){
        for(int j=1;j<=5;j++){
            a[i][j]=read;
            mp[a[i][j]].set(i);
        }
    }
    
    bitset<50005>tmp;
    ll ans=0;
    for(int i=1;i<=n;i++){
        tmp.reset();
        for(int j=1;j<=5;j++) tmp|=mp[a[i][j]];
        ans+=n-tmp.count();
    }

    printf("%lld\n",ans/2);
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值