CF Gym 100685A Ariel
2 seconds
256 megabytes
standard input
standard output
King Triton really likes watching sport competitions on TV. But much more Triton likes watching live competitions. So Triton decides to set up a swimming competition in the kingdom Merfolk. Thousands of creatures come to take part in competition, that's why it is too difficult to take the first place.
For the King's beloved daughter Ariel this competition is the first in her life. Ariel is very kind, so she wants to give a lot of gold medals. Ariel says, that it is unfair to make a single ranking list for creatures that are so different. It is really a good result to be the fastest small fish without tail in Merfolk!
Ariel chooses k important traits (such as size, tailness, rapacity and so on). A creature can either possess a trait or not (there are no intermediate options).
A score is given for each creature (it doesn't matter how it was calculated) and the list of possessed traits f1, ..., fy is also given.
Ariel wants to know the place occupied by creature a in a competition among creatures, who have the same traits h1, ..., ht. So if creature a doesn't have a trait hi, then all creatures in the competition are without this trait. If creature a has a trait hi, then all creatures in the competition have this trait. Other traits doesn't matter. The winner of the competition is a creature with the maximum score.
The first line contains n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 10). The next n lines contain information about creatures: score (1 ≤ score ≤ 109), y (0 ≤ y ≤ k) — the number of possessed traits, and y numbers fi (1 ≤ fi ≤ k) — ids of possessed traits. All fi in one line are different.
The next line contains m (1 ≤ m ≤ 105) — the number of queries from Ariel. The next m lines describe queries: a (1 ≤ a ≤ n) — the id of a creature, then t — the number of traits, then t numbers hi. All hi in one line are different.
For each query output the place of a creature a in ranking list amount the corresponded creatures. If several creatures have the same score all of them take the same place.
3 2
100 1 1
50 1 2
30 2 1 2
12
1 2 1 2
1 1 1
1 1 2
1 0
2 0
2 1 1
2 1 2
2 2 2 1
3 0
3 2 1 2
3 1 2
3 1 1
1
1
1
1
2
1
1
1
3
1
2
2
3 2
100 0
10 0
100 0
3
1 0
2 0
3 0
1
3
1
Solution:
预处理:将属性相同的选手的score放在一个vector里,排序.这样共得到$2^{k}$个vector.
查询:在每个合法属性对应的vector里二分查找比该score大的score的数目.
Implememtation:
#include <bits/stdc++.h>
using namespace std; const int N=<<, M=1e4+; vector<int> a[N];
int s[M], st[M]; int main(){
int n, k, m;
cin>>n>>k;
for(int i=, c; i<=n; i++){
cin>>s[i]>>c;
for(int t; c--; cin>>t, st[i]|=<<t-);
a[st[i]].push_back(s[i]);
}
for(int i=; i<<<k; i++)
sort(a[i].begin(), a[i].end());
cin>>m;
for(int id, c, t, mask; m--; ){
cin>>id>>c;
mask=;
for(int i=, x; i<c; i++)
cin>>x, mask|=<<x-;
int ans=;
for(int i=; i<<<k; i++)
if((st[id]&mask)==(i&mask))
ans+=a[i].end()-upper_bound(a[i].begin(), a[i].end(), s[id]);
cout<<ans+<<endl;
}
return ;
}
事实证明,这个做法真的没那么暴力.
(2)将所有可能的查询的结果算出来(打表),这样查询的复杂度是$O(1)$的.
把查询查询中要求和id相同的那些属性压缩到一个$k$位整数$s$中(将$s$的对应位置1),存在$res[id][s]$中.
用$st[i]$表示第i个选手的属性,不难看出$res[id][s]$对应的比赛的选手集合为:
\[C_{id, s} =\{i: st[i] \& s = st[id]\& s\}\]
由此,我们将$C_{id, s}$,写成$C_{st[id]\&s}$
考虑在s固定的情况下,如何计算res[1..n][s].
我们用(id, score)表示一个选手, 先将各选手按score从大到小排序。然后逐个放到$C_{st[id]\&s}$中,其实这样是将选手照其$st[id]\&s$分成了若干等价类.这样便可在某个等价类内O(1)地计算res[i][s]。总复杂度是$O(N\log(N)+2^{k}N)$。
Implementation:
#include <bits/stdc++.h>
using namespace std; const int N(<<), M(1e4+);
vector<pair<int,int>> a, b[N];
int res[M][N], st[M]; int main(){
int n, k;
cin>>n>>k;
for(int i=, c, t, sc; i<=n; i++){
cin>>sc>>c;
for(; c--; cin>>t, st[i]|=<<t-); //error-prone
a.push_back({sc, i});
} sort(a.begin(), a.end(), greater<pair<int,int>>()); for(int i=; i<<<k; i++){
for(int i=; i<<<k; i++)
b[i].clear();
for(auto x: a)
b[st[x.second]&i].push_back(x);
for(int j=; j<<<k; j++)
for(int k=; k<b[j].size(); k++)
if(b[j][k].first==b[j][k-].first)
res[b[j][k].second][i]=res[b[j][k-].second][i];
else
res[b[j][k].second][i]=k;
}
int m;
cin>>m;
for(; m--; ){
int mask=, c, id, t;
cin>>id>>c;
for(; c--; cin>>t, mask|=<<t-);
cout<<res[id][mask]+<<endl;
}
return ;
}
CF Gym 100685A Ariel的更多相关文章
- CF Gym 102028G Shortest Paths on Random Forests
CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...
- CF gym 101933 K King's Colors —— 二项式反演
题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...
- cf Gym 101086M ACPC Headquarters : AASTMT (Stairway to Heaven)
题目: Description standard input/output As most of you know, the Arab Academy for Science and Technolo ...
- CF Gym 100685E Epic Fail of a Genie
传送门 E. Epic Fail of a Genie time limit per test 0.5 seconds memory limit per test 64 megabytes input ...
- CF GYM 100703A Tea-drinking
题意:龙要制作n个茶,每个茶的配方是一个字符串,两个字符串之间有一个差值,这个差值为两个字符串每个对应字母之间差的绝对值的最大值,求制作所有茶时获得的所有差值中的最大值. 解法:克鲁斯卡尔.将茶的配方 ...
- CF GYM 100703B Energy Saving
题意:王子每月买m个灯泡给n个房间换灯泡,如果当前有的灯泡数够列表的第一个房间换的就全换,直到灯泡不够为止,给出q个查询,查询x月已经换好几个房子,手里还剩多少灯泡. 解法:水题……小模拟. 代码: ...
- CF GYM 100703F Game of words
题意:两个人玩n个游戏,给出每人玩每个游戏的时间,两个人需要在n个游戏中挑m个轮流玩,求最短时间. 解法:dp.(这场dp真多啊……话说也可以用最小费用最大流做……然而并不会XD)dp[i][j][k ...
- CF GYM 100703G Game of numbers
题意:给n个数,一开始基数为0,用这n个数依次对基数做加法或减法,使基数不超过k且不小于0,输出最远能运算到的数字个数,输出策略. 解法:dp.dp[i][j]表示做完第i个数字的运算后结果为j的可能 ...
- CF GYM 100703I Endeavor for perfection
题意:有n个学习领域,每个领域有m个课程,学习第i个领域的第j个课程可以获得sij个技能点,在每个领域中选择一个课程,要求获得的n个技能点的最大值减最小值最小,输出符合要求的策略. 解法:尺取法.将课 ...
随机推荐
- android strings.xml 报 is not translated in af,
57 down vote In your ADT go to window->Preferences->Android->Lint Error Checking Find there ...
- Eclipse发布地址不同引发的问题
eclipse发布到workspace metadata时,进不去http://localhost:8888页面.但是,它可以启动JAZZ和“公司的一个工程”. 而 eclipse发布到tomcat ...
- C# 将透明图片的非透明区域转换成Region
以下代码实现将一张带透明度的png图片的非透明部分转换成Region输出 /// <summary> /// 根据图片得到一个图片非透明部分的区域 /// </summary> ...
- Redmine 项目管理工具----完全攻略
摘要: 此篇博客涉及 安装,插件修改,插件安装,代码显示,中文乱码,SVN配置等内容,几乎覆盖所有redmine基本功能. 本机环境: Redmine 版本: 3.2.0 本机环境: win7 64位 ...
- gethostbyname 亲测可用
建立Socket链接的时候需要IP地址,但是只有域名怎么办,gethostbyname就是一个将域名转换为IP的函数: #include <netdb.h> struct hostent ...
- mysql-5.7.14-winx64免安装版在win10下的详细配置过程
1.配置文件 如果使用mysql的默认配置,在使用的过程中会出现很多问题,如汉字是乱码等. 在mysql的根目录(如:D:\mysql\mysql-5.7.14-winx64\)下,新建配置文件my. ...
- 转 一篇关于sql server 三种恢复模式的文章,从sql server 的机制上来写的,感觉很不错,转了
简介 SQL Server中的事务日志无疑是SQL Server中最重要的部分之一.因为SQL SERVER利用事务日志来确保持久性(Durability)和事务回滚(Rollback).从而还部分确 ...
- C#基础之内存分配
1.创建一个对象 一个对象的创建过程主要分为内存分配和初始化两个环节.在.NET中CLR管理的内存区域主要有三部分:栈.GC堆.LOH堆,栈主要用来分配值类型数据.它的管理是有系统控制的,而不是像GC ...
- 使用spring cloud实现分布式配置管理
<7天学会spring cloud系列>之创建配置管理服务器及实现分布式配置管理应用. 本文涉及到的项目: 开源项目:http://git.oschina.net/zhou666/spri ...
- DELL VENUE 11 PRO系统损坏之后的解决办法
首页说明下我的平板是dell venue 11 pro atom版+win8.1版. 前两天测试玩win10,结果后来几天这货直接开不了机了,每次提示自动修复,但是却说找不到某一文件,然后蓝屏,win ...