zoukankan      html  css  js  c++  java
  • UPC10582: Cowpatibility

    时间限制: 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.

    来源/分类

     
    每头牛的非空子集有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, -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, 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("%lld
    ", ans);
        return 0;
    }
  • 相关阅读:
    【CYH-02】noip2018数论模拟赛:赛后题解
    C语言 malloc函数
    C 库函数
    C语言strcmp()函数:比较两个字符串
    C语言sprintf函数的深入理解
    C语言strcat()函数:字符串连接(拼接)
    liunx 中设置zookeeper 自启动(service zookeeper does not support chkconfig)
    页面上出现403错误,并解决
    Mac 下安装nginx
    nginx: [emerg] unknown directive "," in /usr/local/etc/nginx/nginx.conf:80
  • 原文地址:https://www.cnblogs.com/albert-biu/p/10452636.html
Copyright © 2011-2022 走看看