100: cf 878C set+并查集+链表
$des$
Berland要举行 $n$ 次锦标赛,第一次只有一个人,之后每一次会新
加入一个人。锦标赛中有 $k$ 种运动项目,每个人在这 $k$ 种项目上都有一
个能力值,每次会选择任意两个还未被淘汰的人进行某个项目的比
赛,能力值高的人胜出,输的人被淘汰,直至只剩下一个人成为冠
军。
给出每个人每个项目的能力值,保证它们两两不同,求每次锦标
赛有多少人可能成为冠军。
$sol$
只要选手 $a$ 在某个项目上比选手 $b$ 强, $a$ 就可以淘汰 $b$,我们可以连
一条 $a$ 到 $b$ 的边。
对整个图求强连通分量,缩点后一定会形成一个竞赛图,拓扑序
最靠前的分量中的所有点都可能成为冠军。
每加入一个点时,我们可能需要合并拓扑序在一段区间内强连通
分量。用set按拓扑序维护每个强连通分量,对每个分量记录它的大
小,以及在每个项目上的最大和最小能力值,就可以直接在set上二分
找到需要合并的区间。
最多只会合并 $n - 1$ 次,时间复杂度 $O(nklogn)$
$code$
#include <bits/stdc++.h> using namespace std; #define gc getchar()
inline int read() {
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} #define Rep(i, a, b) for(int i = a; i <= b; i ++)
#define MP(a, b) make_pair(a, b) const int N = 5e4 + ; int A[N][];
int Max[N][];
int n, m;
int fa[N], size[N], nxt[N], last = ;
int Answer[N]; set<pair<int, int> > S[]; int Get(int x) {
return fa[x] == x ? x : fa[x] = Get(fa[x]);
} void Merge(int a, int b) {
Rep(i, , m) Max[b][i] = max(Max[b][i], Max[a][i]);
size[b] += size[a], fa[a] = b;
} int main() {
n = read(), m = read(); Rep(i, , n) Rep(j, , m) Max[i][j] = A[i][j] = read();
Rep(i, , n) fa[i] = i, size[i] = ;
Rep(i, , m) S[i].insert(MP(A[][i], )); Answer[] = ; Rep(i, , n) {
int a = , b = ;
set<pair<int, int> > :: iterator it;
Rep(j, , m) {
it = S[j].upper_bound(MP(A[i][j], i));
if(it != S[j].end()) {
int t = Get((*it).second);
if(!b || Max[t][j] < Max[b][j]) b = t;
}
if(it != S[j].begin()) {
it --;
int t = Get((*it).second);
if(!a || Max[t][j] > Max[a][j]) a = t;
}
S[j].insert(MP(A[i][j], i));
}
if(!a) nxt[i] = b;
else if(!b) last = i, nxt[a] = i;
else if(a == b) Merge(i, a);
else if(Max[a][] < Max[b][]) nxt[a] = i, nxt[i] = b;
else {
for(int t = b; t != a; t = nxt[t], Merge(t, b));
Merge(i, b);
nxt[b] = nxt[a];
if(a == last) last = b;
}
Answer[i] = size[last];
} Rep(i, , n) cout << Answer[i] << "\n"; return ;
}
100: cf 878C set+并查集+链表的更多相关文章
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- 【CF878C】Tournament set+并查集+链表
[CF878C]Tournament 题意:有k个项目,n个运动员,第i个运动员的第j个项目的能力值为aij.一场比赛可以通过如下方式进行: 每次选出2个人和一个项目,该项目能力值高者获胜,败者被淘汰 ...
- CodeForces 566D Restructuring Company (并查集+链表)
题意:给定 3 种操作, 第一种 1 u v 把 u 和 v 合并 第二种 2 l r 把 l - r 这一段区间合并 第三种 3 u v 判断 u 和 v 是不是在同一集合中. 析:很容易知道是用并 ...
- Jzoj 初中2249 蒸发学水(并查集)
题目描述 众所周知,TerryHu 是一位大佬,他平时最喜欢做的事就是蒸发学水. 机房的位置一共有n 行m 列,一开始每个位置都有一滴学水,TerryHu 决定在每一个时刻选择 一滴学水进行蒸发,直到 ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 828C) - 链表 - 并查集
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...
- CF思维联系--CodeForces - 218C E - Ice Skating (并查集)
题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...
- 稀疏图(邻接链表),并查集,最短路径(Dijkstra,spfa),最小生成树(kruskal,prim)
全部函数通过杭电 1142,1162,1198,1213等题目测试. #include<iostream> #include<vector> #include<queue ...
- cf-Round541-Div2-F(并查集+静态链表)
题目链接:http://codeforces.com/contest/1131/problem/F 思路: 很容易看出这是一道并查集的题目,因为要输出每个cage中住的鸟的编号,故采用静态链表.用l[ ...
- Codeforces 1131 F. Asya And Kittens-双向链表(模拟或者STL list)+并查集(或者STL list的splice()函数)-对不起,我太菜了。。。 (Codeforces Round #541 (Div. 2))
F. Asya And Kittens time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- localhost-startStop-1启动失败
背景:在IDEA调试程序的时候,应用起不来,看日志是从main线程切换到localhost-startStop-1线程就开始卡住了 方法一 原因 这个问题和jvm上的熵池策略有关 解决 将$JAVA_ ...
- Nginx限制访问速率和最大并发连接数模块--limit (防范DDOS攻击)
Tengine版本采用http_limit_req_module进行限制 具体连接请参考 http://tengine.taobao.org/document_cn/http_limit_req_cn ...
- ADO.NET 一般操作(c#)
ADO.NET五大对象:SqlConnection.SqlCommand.SqlDataReader.SqlDataAdapter .DataSet ,其中SqlDataAdapter 不建议使用 一 ...
- js加减乘除函数
经常用到算数的时候,可以直接用:// 除法函数function accDiv(arg1, arg2) { var t1 = 0, t2 = 0, r1, r2; try { t1 = arg1.toS ...
- docker容器日志管理(清理)
原文:docker容器日志管理(清理) 前言 在使用docker容器时候,其日志的管理是我们不得不考虑的事情.因为docker容器的日志文件会占据大量的磁盘空间.下面介绍的就是对docker容器日志的 ...
- kubernetes第四章--架构
- React Native 开发豆瓣评分(八)首页开发
首页完成效果展示: 一.开发占位图组件 在没有数据的时候使用占位图替代 items 的位置. 在 components 目录里创建 moviesItemPlaceholder.js import Re ...
- 经典数据结构与算法在经典软件(linux kernel)中的应用
参考文章:Core Alorgithms deployed linux中的priority search tree数据结构研究 虚拟内存: 1.红黑树,管理与进程关联的vm_area_struct实例 ...
- 4.kafka API producer
1.Producer流程首先构建待发送的消息对象ProducerRecord,然后调用KafkaProducer.send方法进行发送.KafkaProducer接收到消息后首先对其进行序列化,然后结 ...
- jenkins安装和简单部署
jenkins安装和简单部署 jenkins历史 jenkins是一款非常好用的团队CI(Continuous Integration)工具.它可以使你的构建,集成,发布,开发流程自动化.减轻各个环节 ...