Description

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

一种口味的冰激凌!Farmer John的N头奶牛(2≤N≤50,000)各自列举了她们最喜欢的五种冰激凌口味的清单。为

使这个清单更加精炼,每种可能的口味用一个不超过106的正整数ID表示。如果两头奶牛的清单上有至少一种共同

的冰激凌口味,那么她们可以和谐共处。请求出不能和谐共处的奶牛的对数。

Input

输入的第一行包含N

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

Output

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

Sample Input

4

1 2 3 4 5

1 2 3 10 8

10 9 8 7 6

50 60 70 80 90

Sample Output

4

在这里,奶牛4不能和奶牛1、2、3中的任一头和谐共处,奶牛1和奶牛3也不能和谐共处。

HINT

Source

Gold

Solution

正解是容斥(然而我并不会,只会bitset优化暴力,去找题解学习了一下容斥做法)。

不过bzoj好像调了时限,然后我现在网上找到的所有容斥题解都TLE了(因为网上的题解都用了string,换成hash才能过)。

容斥做法:

显然转化为\(n(n-1)/2-\)和谐对数。

然后和谐对数就5种情况:1个一样的,2个一样的,3个一样的,4个一样的,5个一样的。

用经典的容斥式子:

\[ans=\sum_{i=1}^5f(i)*(-1)^i
\]

\(f(i)\)为一样的对数的个数。

那么\(2^5\)枚举所有取法,容斥一遍即可。

复杂度是相对与用string的小常数\(O(2^5nlogn)\)

用bitset优化一下暴力也跑的飞快,就慢了几百ms。复杂度是小常数的\(O(\frac{n^2logn}{w})\)(要开map不然存不下,或者可以分块求答案就不用开map)。

容斥做法:

#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define il inline
#define ull unsigned long long namespace io { #define in(a) a = read()
#define out(a) write(a)
#define outn(a) out(a), putchar('\n') #define I_int ll
inline I_int read() {
I_int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
char F[200];
inline void write(I_int x) {
if (x == 0) return (void) (putchar('0'));
I_int tmp = x > 0 ? x : -x;
if (x < 0) putchar('-');
int cnt = 0;
while (tmp > 0) {
F[cnt++] = tmp % 10 + '0';
tmp /= 10;
}
while (cnt > 0) putchar(F[--cnt]);
}
#undef I_int }
using namespace io; using namespace std; #define N 50010
#define base 23333333 int n = read();
int a[10];
map<ull,ll>mp; int main() {
ll ans = 1ll*n*(n-1ll)/2ll;
for(int i = 1; i <= n; ++i) {
for(int j = 0; j < 5; ++j) in(a[j]);
sort(a,a+5); ll sum = 0;
for(int k = 1; k < (1 << 5); ++k) {
int tot = 0; ull s = 0;
for(int j = 0; j < 5; ++j)
if(k&(1<<j)) s = s * base + a[j], ++tot;
if(tot&1) sum += mp[s]++;
else sum -= mp[s]++;
}
ans -= sum;
}
outn(ans);
}

bitset做法

#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define il inline
#define ull unsigned long long namespace io { #define in(a) a = read()
#define out(a) write(a)
#define outn(a) out(a), putchar('\n') #define I_int ll
inline I_int read() {
I_int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
char F[200];
inline void write(I_int x) {
if (x == 0) return (void) (putchar('0'));
I_int tmp = x > 0 ? x : -x;
if (x < 0) putchar('-');
int cnt = 0;
while (tmp > 0) {
F[cnt++] = tmp % 10 + '0';
tmp /= 10;
}
while (cnt > 0) putchar(F[--cnt]);
}
#undef I_int }
using namespace io; using namespace std; #define N 50010 int n = read();
int a[N][5];
map<int, bitset<50005> > t; int main() {
for(int i = 1; i <= n; ++i) {
for(int j = 0; j < 5; ++j) {
in(a[i][j]);
t[a[i][j]][i] = 1;
}
}
ll ans = 0;
for(int i = 1; i <= n; ++i) {
bitset<50005>tmp; tmp.reset();
for(int j = 0; j < 5; ++j) {
tmp |= t[a[i][j]];
}
ans+=n - tmp.count();
}
outn(ans/2ll);
}

BZOJ5487: [Usaco2018 Dec]Cowpatibility的更多相关文章

  1. BZOJ5484: [Usaco2018 Dec]Sort It Out

    5484: [Usaco2018 Dec]Sort It Out https://www.lydsy.com/JudgeOnline/problem.php?id=5484 Sol. 考虑没有在被喊叫 ...

  2. bzoj5483: [Usaco2018 Dec]Balance Beam

    又又又又又又又被踩爆了 首先容易写出这样的期望方程:f(1)=max(d(1),f(2)/2),f(n)=max(d(n),f(n-1)/2), f(i)=max(d(i),(f(i-1)+f(i+1 ...

  3. [bzoj5483][Usaco2018 Dec]Balance Beam_凸包_概率期望

    bzoj5483 Usaco2018Dec Balance Beam 题目链接:https://lydsy.com/JudgeOnline/problem.php?id=5483 数据范围:略. 题解 ...

  4. USACO2018 DEC(Platinum) (树上乱搞,期望+凸包)

    发现这跟\(Gold\)难度简直天差地别啊.. \(T1\) 传送门 解题思路 这道题还是很可做的,发现题意可以传化成一棵树每次从叶子节点删边,然后有\(m\)条限制,形如\(a\)在\(b\)前面删 ...

  5. USACO2018 DEC (Gold) (dp,容斥+哈希,最短路)

    \(T1\) 传送门 解题思路 傻逼\(dp\)..直接\(ST\)表处理最大值\(O(n^2)\)艹过了. 代码 #include<bits/stdc++.h> using namesp ...

  6. [Usaco2018 Dec]Teamwork 题解

    题目描述 题目描述 在Farmer John最喜欢的节日里,他想要给他的朋友们赠送一些礼物.由于他并不擅长包装礼物,他想要获得他的 奶牛们的帮助.你可能能够想到,奶牛们本身也不是很擅长包装礼物,而Fa ...

  7. USACO比赛题泛刷

    随时可能弃坑. 因为不知道最近要刷啥所以就决定刷下usaco. 优先级排在学习新算法和打比赛之后. 仅有一句话题解.难一点的可能有代码. 优先级是Gold>Silver.Platinum刷不动. ...

  8. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  9. BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]

    1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1017  Solved: ...

随机推荐

  1. php数组函数大全

    一.数组操作的基本函数 数组的键名和值 array_values($arr);获得数组的值 array_keys($arr);获得数组的键名 array_flip($arr);数组中的值与键名互换(如 ...

  2. 教你如何用笔记本设置超快WIFI

    以win7为例 1.在主菜单运行框输入  cmd------->以管理员的身份运行 2.命令提示符中输入:netsh wlan set hostednetwork mode=allow ssid ...

  3. 微信小程序开发笔记02

    今天学习了微信小程序开发用到的语言,wxml与wxss语言基本语法与html和css基本语法相似,学习起来相对简单.在小程序主要的语言是js(javascript,跟准确的说是jqery) ,由于这种 ...

  4. 个人小爱好:Operating System:three easy pieces---第6章第4节_担心并发问题?

    担心并发问题? 微妙,上下文切换大约6微妙.而,现在的系统有着级数级别的提升,在2-3GHz的处理起中消耗只有亚微妙级. 但应该注意到,不是所有的系统性能都跟着CPU性能的提升而提升,根据Ouster ...

  5. linux下查看网卡速率

    转自:http://blog.csdn.net/liugongfeng/article/details/50263733 我们都知道ifconfig -a 可以列出所有网卡,但是怎么判读是千兆网卡还是 ...

  6. qemu对虚拟机的内存管理(一)

    在分析了KVM中对虚拟机各级地址(gva->gpa->hva->hpa)的转换之后,想要知道qemu中又是如何完成各级地址转换的,因此对qemu中对虚拟机内存管理的相关数据结构与源码 ...

  7. qt opencv编译错误 /usr/local/lib/libopencv_imgcodecs.so.3.1:-1: error: error adding symbols: DSO missing from command line

    转载自:http://tbfungeek.github.io/2016/03/05/Opencv-%E5%AE%89%E8%A3%85%E8%BF%87%E7%A8%8B%E4%B8%AD%E5%87 ...

  8. vue-cli脚手架

    cnpm i vue-cli -g   //npm 安装报错,原因不明,可能是我改过东西的原因,但是cnpm可以安装 命令行进入要新建的vue的目录执行 C:\Users\76912\Videos\v ...

  9. mybatis BindingException: Invalid bound statement (not found)

    错误截图 解决措施 此异常的原因是由于mapper接口编译后在同一个目录下没有找到mapper映射文件而出现的. 通常我们在配置SqlSessionFactory时会有如配置 <!-- 配置Sq ...

  10. mac安装gcc

    xcode-select --install安装命令行工具 安装完毕后,可能报错 xcrun: error: active developer path ("/Applications/Xc ...