BZOJ5487: [Usaco2018 Dec]Cowpatibility
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
Solution
正解是容斥(然而我并不会,只会bitset优化暴力,去找题解学习了一下容斥做法)。
不过bzoj好像调了时限,然后我现在网上找到的所有容斥题解都TLE了(因为网上的题解都用了string,换成hash才能过)。
容斥做法:
显然转化为\(n(n-1)/2-\)和谐对数。
然后和谐对数就5种情况:1个一样的,2个一样的,3个一样的,4个一样的,5个一样的。
用经典的容斥式子:
\]
\(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的更多相关文章
- BZOJ5484: [Usaco2018 Dec]Sort It Out
5484: [Usaco2018 Dec]Sort It Out https://www.lydsy.com/JudgeOnline/problem.php?id=5484 Sol. 考虑没有在被喊叫 ...
- 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 ...
- [bzoj5483][Usaco2018 Dec]Balance Beam_凸包_概率期望
bzoj5483 Usaco2018Dec Balance Beam 题目链接:https://lydsy.com/JudgeOnline/problem.php?id=5483 数据范围:略. 题解 ...
- USACO2018 DEC(Platinum) (树上乱搞,期望+凸包)
发现这跟\(Gold\)难度简直天差地别啊.. \(T1\) 传送门 解题思路 这道题还是很可做的,发现题意可以传化成一棵树每次从叶子节点删边,然后有\(m\)条限制,形如\(a\)在\(b\)前面删 ...
- USACO2018 DEC (Gold) (dp,容斥+哈希,最短路)
\(T1\) 传送门 解题思路 傻逼\(dp\)..直接\(ST\)表处理最大值\(O(n^2)\)艹过了. 代码 #include<bits/stdc++.h> using namesp ...
- [Usaco2018 Dec]Teamwork 题解
题目描述 题目描述 在Farmer John最喜欢的节日里,他想要给他的朋友们赠送一些礼物.由于他并不擅长包装礼物,他想要获得他的 奶牛们的帮助.你可能能够想到,奶牛们本身也不是很擅长包装礼物,而Fa ...
- USACO比赛题泛刷
随时可能弃坑. 因为不知道最近要刷啥所以就决定刷下usaco. 优先级排在学习新算法和打比赛之后. 仅有一句话题解.难一点的可能有代码. 优先级是Gold>Silver.Platinum刷不动. ...
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]
1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1017 Solved: ...
随机推荐
- react withClamp装饰器
Clamp.js 等待异步数据加载完后在初始化$clamp withClamp.js import React, { Component } from "react"; expor ...
- PL/SQL控制结构
顺序结构 按先后顺序 分支判断结构 IF语句 IF condition THEN statements; [ELSIF condition THEN statements;] [ELSE statem ...
- TS的一些小东西
首先介绍下泛型的三种方法,函数声明,函数表达式,箭头函数 function Interview2<T>(name:T):T{ return name } let mn=function&l ...
- vue构造器的内容
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- PHP面试大全 基础篇100道问题
2017年3月7日14:23:21 其实计划很久但是移植没时间去收集和处理弄成一个完整的文件 SVN地址: https://git.oschina.net/zxadmin/live_z 目前基础部分更 ...
- 程序中打印当前进程的调用堆栈(backtrace)
为了方便调式程序,产品中需要在程序崩溃或遇到问题时打印出当前的调用堆栈.由于是基于Linux的ARM嵌入式系统,没有足够的空间来存放coredump文件. 实现方法,首先用__builtin_fram ...
- 转 消息中间件:RocketMQ 介绍(特性、术语、原理、优缺点、消息顺序、消息重复)
https://blog.csdn.net/jiangyu1013/article/details/81668671 消息中间件的作用 1. 应用解耦 2. 异步处理 比如用户注册场景,注册主流程完成 ...
- 并行开发-Paraller
并行开发的概念 并行开发要做的事情就是将任务分摊给硬件线程去并行执行来达到负载和加速,传统的代码都是串行的,就一个主线程,当我们为了实现加速而开了很多工作线程,这些工作线程就是软件线程 Paralle ...
- 如何将本地大文件通过终端上传到linux服务器
第一种方式: SecureCRT下 上传文件只需在shell终端仿真器中输入命令“rz”,即可从弹出的对话框中选择本地磁盘上的文件,利用Zmodem上传到服务器当前路径下. 下载文件只需在s ...
- Jmeter使用之-断言
添加响应断言 ,一般下面这样选择断言的设置就可以了 个人习惯是直接从响应数据中直接复制正确的接口返回值到断言的测试模式中,如下 !!!但是这样复制的是有问题的 ,需要在测试模式中去掉 ...