加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

UPC10582: Cowpatibility

发布时间:2020-12-14 04:22:55 所属栏目:大数据 来源:网络整理
导读:时间限制:?1 Sec?? 内存限制:?128 MB 提交:?69?? 解决:?24 [提交] [状态] [讨论版] [命题人: admin] 题目描述 It turns out there is one factor that matters far more than any other when determining whether two cows are compatible as potential frie
时间限制:?1 Sec??内存限制:?128 MB
提交:?69??解决:?24
[提交] [状态] [讨论版] [命题人:admin]

题目描述

It turns out there is one factor that matters far more than any other when determining whether two cows are compatible as potential friends: whether they like similar flavors of ice cream!
Farmer John‘s N cows (2≤N≤50,000) have each listed their five favorite flavors of ice cream. To make this list concise,each possible flavor is represented by a positive integer ID at most 106. Two cows are compatible if their lists contain at least one common flavor of ice cream.

Please determine the number of pairs of cows that are NOT compatible

?

输入

The first line of input contains N. Each of the following N lines contain 5 integers (all different) representing the favorite ice cream flavors of one cow.

?

输出

Please output the number of pairs of cows that are not compatible.

?

样例输入

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

样例输出

4

?

提示

Here,cow 4 is not compatible with any of cows 1,2,or 3,and cows 1 and 3 are also not compatible.

?

来源/分类

USACO?2018?Dec,?Gold?

?
每头牛的非空子集有31种情况,求出每头牛的所有子集,并记录一种子集被多少头牛所拥有
?
奇加偶减(奇数个类的计数和-偶数个类的计数和),记子集size为 1,2,3,4,5
1-2+3-4+5即为最终结果。
?
?
#include "bits/stdc++.h"


using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn = 1e5;

int op[] = {-1,1,-1,1};

struct node {
    int n;
    int v[6];

} e[maxn];

bool operator<(const node &a,const node &b) {
    for (int j = 0; j < 5; j++) {
        if (a.v[j] < b.v[j]) return true;
        if (a.v[j] > b.v[j]) return false;
    }
    return false;
}

map<node,int> mp;

node ss(node &te,int j) {
    node ret = {0,{0,0,0}};
    for (int i = 0; i < 5; i++) {
        if ((1 << i) & j) ret.v[ret.n++] = te.v[i];
    }
    return ret;
}

int main() {
    freopen("input.txt","r",stdin);
    int n;
    scanf("%d",&n);
    for (int i = 0; i < n; i++) {
        e[i].n = 5;
        for (int j = 0; j < 5; j++) {
            scanf("%d",&e[i].v[j]);
        }
        sort(e[i].v,e[i].v + 5);
        for (int j = 1; j < 32; j++) {
            mp[ss(e[i],j)]++;
        }
    }
    ll ans = 1ll * n * (n - 1) / 2;//运算时转为longlong,否则会溢出
    for (auto &p :mp) {
        ans -= 1ll * op[p.first.n] * p.second * (p.second - 1) / 2;
    }
    printf("%lldn",ans);
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读