Cowpatibility
Cowpatibility
https://ac.nowcoder.com/acm/problem/24191
思路
- 把每一种冰淇淋被牛喜欢用bitset存起来,第i头喜欢用第i-1位为1表示。
- 然后遍历每一头牛的每一种喜欢的冰淇淋,并或起来可得该牛和其他牛能否和谐共处的关系,1表示能和谐共处,0表示不能和谐共处。不能和谐共处的数量即为
n-s.count()
- 因为不能和谐共处的关系被重复计算了两次,所以最后答案除2即可
代码
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp(aa,bb) make_pair(aa,bb) #define _for(i,b) for(int i=(0);i<(b);i++) #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define per(i,b,a) for(int i=(b);i>=(a);i--) #define mst(abc,bca) memset(abc,bca,sizeof abc) #define X first #define Y second #define lowbit(a) (a&(-a)) #define debug(a) cout<<#a<<":"<<a<<"\n" typedef long long ll; typedef pair<int,int> pii; typedef unsigned long long ull; typedef long double ld; const int N=5e4+5; const int INF=0x3f3f3f3f; const int mod=1e9+7; const double eps=1e-6; const double PI=acos(-1.0); int a[N][5],ans; map<int,bitset<N> >mp; void solve(){ int n;cin>>n; rep(i,0,n-1){ rep(j,0,4){ cin>>a[i][j]; mp[a[i][j]].set(i); } } rep(i,0,n-1){ bitset<N> s; rep(j,0,4) s|=mp[a[i][j]]; // debug(s.count()); ans+=n-s.count(); } cout<<ans/2<<"\n"; } int main(){ ios::sync_with_stdio(0);cin.tie(0); // int t;cin>>t;while(t--) solve(); return 0; }
牛客每日一题 文章被收录于专栏
水