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. [No0000198]swagger api一键导入postman

    在用postman进行接口测试时,对于参数较多的接口时第一次添加接口参数是比较繁琐的,可利用swagger一键导入api接口,事例如下: 1.获取swagger地址 2.打开postman,点击imp ...

  2. How to enable Linux-PAM on uClinux

    By default the uClinux  uses the tools provided by busybox firstly. So the init login and passwd are ...

  3. UI自动化框架——构建思维

    目的:从Excel中获取列的值,传输到页面 技巧:尽可能的提高方法的重用率 Java包: 1.java.core包 3个类:1)日志(LogEventListener)扩展web driver自带的事 ...

  4. js--单选按钮赋值

    var sex='${userInfo.sex}'; if(sex=="女"){ $("input[name=sex][value='女']").attr(&q ...

  5. datagridview的一些设置

    1.自动调整列宽 this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMo ...

  6. python基础(4)-元组&字典&字符串&队列

    元组(tuple) #元组相对列表来说不同之处是只读不可写 读操作和列表一致 letter_tuple = ('a','b','c','d'); print(letter_tuple[0]);#res ...

  7. PowerBI更新 - 解决方案架构 - PowerBI Solution Architecture(一图胜万字!)

    2019/04更新 参见这里 今天发福利啦!发福利啦!发福利啦! 企业的各种数据整合到PowerBI显示,浏览器,移动端显示关键指标. 一个很好的PowerBI解决方案的图!一图胜万字!你所需要知道的 ...

  8. cocos2dx 实现文字的一键复制功能(IOS、Android)

    1.IOS篇(用OC和C++混编) 头文件声明: public static void copy(std::string str); 实现如下: void copy(std::string str) ...

  9. SystemParametersInfo调置壁纸、屏幕保护程序

    应用SystemParametersInfo函数可以获取和设置数量众多的windows系统参数.这个小程序就是运用了SystemParametersInfo函数来设置桌面的墙纸,而且程序可以让我们选择 ...

  10. mysql order by 多个字段排序

    工作中需用到order by 后两个字段排序,但结果却产生了一个Bug,以此备录. [1]复现问题场景 为了说明问题,模拟示例数据库表students,效果同实例. 如下语句Sql_1: SELECT ...