传送门

A. Ariel
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
Input
3 2100 1 150 1 230 2 1 2121 2 1 21 1 11 1 21 02 02 1 12 1 22 2 2 13 03 2 1 23 1 23 1 1

Output
111121113122

Input
3 2100 010 0100 031 02 03 0

Output
131

Solution:
(1)(看起来)比较暴力的做法:

预处理:将属性相同的选手的score放在一个vector里,排序.这样共得到$2^{k}$个vector.

查询:在每个合法属性对应的vector里二分查找比该score大的score的数目.

Implememtation:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 const int N=1<<10, M=1e4+5;
 5
 6 vector<int> a[N];
 7 int s[M], st[M];
 8
 9 int main(){
10     int n, k, m;
11     cin>>n>>k;
12     for(int i=1, c; i<=n; i++){
13         cin>>s[i]>>c;
14         for(int t; c--; cin>>t, st[i]|=1<<t-1);
15         a[st[i]].push_back(s[i]);
16     }
17     for(int i=0; i<1<<k; i++)
18         sort(a[i].begin(), a[i].end());
19     cin>>m;
20     for(int id, c, t, mask; m--; ){
21         cin>>id>>c;
22         mask=0;
23         for(int i=0, x; i<c; i++)
24             cin>>x, mask|=1<<x-1;
25         int ans=0;
26         for(int i=0; i<1<<k; i++)
27             if((st[id]&mask)==(i&mask))
28                 ans+=a[i].end()-upper_bound(a[i].begin(), a[i].end(), s[id]);
29         cout<<ans+1<<endl;
30     }
31     return 0;
32 }

事实证明,这个做法真的没那么暴力.

(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(1<<10), M(1e4+5);
vector<pair<int,int>> a, b[N];
int res[M][N], st[M];int main(){int n, k;cin>>n>>k;for(int i=1, c, t, sc; i<=n; i++){cin>>sc>>c;for(; c--; cin>>t, st[i]|=1<<t-1);  //error-prone
        a.push_back({sc, i});}sort(a.begin(), a.end(), greater<pair<int,int>>());for(int i=0; i<1<<k; i++){for(int i=0; i<1<<k; i++)b[i].clear();for(auto x: a)b[st[x.second]&i].push_back(x);for(int j=0; j<1<<k; j++)for(int k=1; k<b[j].size(); k++)if(b[j][k].first==b[j][k-1].first)res[b[j][k].second][i]=res[b[j][k-1].second][i];else res[b[j][k].second][i]=k;}int m;cin>>m;for(; m--; ){int mask=0, c, id, t;cin>>id>>c;for(; c--; cin>>t, mask|=1<<t-1);cout<<res[id][mask]+1<<endl;}return 0;
}

转载于:https://www.cnblogs.com/Patt/p/5565551.html

CF Gym 100685A Ariel相关推荐

  1. Gym 100685A Ariel (运算)

    题意: 让你找出满足&运算这个性质的所有人里 这个人的rank 分析: 由于q=105很大,直接把答案表先预处理出来,然后每次二分找到这个位置算出答案就好了由于q = 10^5很大, 直接把答 ...

  2. CF Gym 100227 I题 题解

    这场Gym全名是这个:2013-2014 CT S01E01: Extended 2000 ACM-ICPC East Central North America Regional Contest ( ...

  3. Caravan Robbers CF Gym - 100134C

    https://cn.vjudge.net/problem/Gym-100134C http://codeforces.com/gym/100134/attachments 答案就是最小的min(bi ...

  4. CF Gym 100187E Two Labyrinths (迷宫问题)

    题意:问两个迷宫是否存在公共最短路. 题解:两个反向bfs建立层次图,一遍正向bfs寻找公共最短路 #include<cstdio> #include<cstring> #in ...

  5. CF Gym 101630 B Box

    题目的意思大概就是给一个长方体的长宽高,问他能不能用一个w*h的纸剪出来,就是说展开图的长宽能不能比给定的小. 题目给了11中展开图的拓扑结构,我觉得这个很关键,要是题目没有给这个我可能想不到那么全面 ...

  6. CF GYM 100703G Game of numbers

    题意:给n个数,一开始基数为0,用这n个数依次对基数做加法或减法,使基数不超过k且不小于0,输出最远能运算到的数字个数,输出策略. 解法:dp.dp[i][j]表示做完第i个数字的运算后结果为j的可能 ...

  7. 【CF gym 103260】40th Petrozavodsk Programming Camp, Day 5,2021.2.3 水题2题

    M.Discrete Logarithm is a Joke 题意: 思路: 不难想到an=g^{an+1},因为样例给了 a[1000000]的值,所以反着推就行了. 记得开int128,longl ...

  8. 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 ...

  9. Gym - 100203A Ariel 暴力+位运算

    题意:第i种生物有k[i]个特征,分数是score[i],现在要参加竞赛,报出一种生物a,和一些特征h[i],参加竞赛的所有生物在这些h[i]上面的特征是一样的,a生物有h[i],则所有竞赛的生物都必 ...

最新文章

  1. 网络工程与机房等精华指引贴
  2. 像素位移_1亿像素放大也清晰 OPPO Ace2超清四摄解析
  3. hadoop读取mysql数据_Pyspark连接mysql、hive、hdfs 实例展示
  4. Liunx 重定向,管道符(转)
  5. java中br.readline_Java:java中BufferedReader的read()及readLine()方法的使用心得
  6. 如何用Pygame写游戏(一)
  7. 十进制转化成八进制(一到十六进制)
  8. c语言编程车速里程测量,电子车速里程表设计开题报告.doc
  9. 低压电力线宽带载波通信互联互通技术规-总则
  10. 技术评审之技术文档的规范模板
  11. sketch怎么把psd导出为HTML,如何巧妙将sketch文档完美转换成PSD
  12. 约瑟夫环(数三退一)
  13. AD fanout 各选项说明
  14. python数据结构: 有序表
  15. 手机玩游戏卡顿怎么办。
  16. python整合selenium爬取QQ空间访客记录
  17. 团 队 作 业 ———— 随 堂 小 测
  18. Istio 流量管理 virtualservice (1)
  19. 《离散数学及其应用》读书笔记【三】计数
  20. spyder怎么显示文件目录_投影机画质不好怎么调?手把手教你怎么校准家用4K投影机...

热门文章

  1. 最大正方形面积/数量(单调栈)
  2. 如今,我们已经长大,
  3. MySQL数据库——约束
  4. 【Qt界面个性化】大杀器——qss
  5. CSS进阶-Less
  6. 设计电子计算机,电机设计电子计算机在电机设计计算中的应用(电机设计CAD)...
  7. 用python输入概率分布,计算信源熵/信息熵
  8. firebug “阻挡”
  9. 解决由于找不到amd_ags_x64.dll,无法继续执行代码。重新安装程序可能会解决此问题,地平线(Forza Horizon 5)
  10. oracle 11g新的后台进程