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: ...
随机推荐
- C#遍历枚举(Enum)值
foreach (object o in Enum.GetValues(typeof(EmpType))) { Console.WriteLine("{0}:{1}", o, En ...
- php利用OpenXML规范生成word,excel(pdf其他方法)
这个涉及到的东西比较多 HTTP MIME types $contract_data = '<html xmlns:o="urn:schemas-microsoft-com:offic ...
- Eclispe最常用的几个快捷键
熟练使用快捷键可以在很大程度上提高我们的工作效率,Eclipse的快捷键很多,但是常用的也就那么几个,下面说下Eclispe最常用的几个快捷键: Eclipse的快捷键组合可在Eclipse按下ctr ...
- arcmap发布服务报错:“Faild to publish service”
发布gp服务时,Analyze没有重大错误,但是发布结束时提示"Faild to publish service".让人很懵逼: 解决方法: 打开arcgis server man ...
- 把项目运行到本地环境及mysql配置
1. 添加本地域名C:\Windows\System32\drivers\etc\hosts 127.0.0.1 local.v7.com 2. 添加一个apache虚拟站点D:\wamp64\bin ...
- LeetCode 696 Count Binary Substrings 解题报告
题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...
- 【转载】word2vec原理推导与代码分析
本文的理论部分大量参考<word2vec中的数学原理详解>,按照我这种初学者方便理解的顺序重新编排.重新叙述.题图来自siegfang的博客.我提出的Java方案基于kojisekig,我 ...
- 关于 spring MVC 配置自动扫描中 use-default-filters 属性
1.springMVC 设置扫描 Bean 的两种常见写法 1.1.先看第一种常见的配置:默认 <!-- 配置Controller扫描 --> <context:component- ...
- [转贴] 软件测试职业发展的 A 面和 B 面
[转贴] 软件测试职业发展的 A 面和 B 面 1.所谓的软件测试技术到底包含什么? 梅子:我先来从传统意义上来谈一下测试技术,主要就是测试分析,测试设计,测试管理,测试执行,自动化测试技术,专项测试 ...
- 九、使用多线程——NSThread,GCD和NSOperation
概述 早上起床,你先打开洗衣机,然后用热水把泡面泡上,接着打开电脑开启一天的码农生活.其中“洗衣服”.“泡泡面”和“码代码”3个任务(线程)同时进行,这就是多线程.网上有许多关于多线程的经典解释,此处 ...