【Codeforces 356A】Knight Tournament
【链接】 我是链接,点我呀:)
【题意】
n个人矩形m场比赛
每场比赛由编号为li~ri且之前没有被淘汰的人进行。
已知第i场的winner是xi
winner会把这一场其他所有的人都淘汰。
问你n个人每个人都是被谁给淘汰的.
【题解】
并查集
初始条件f[i] = i,nex[i] = i + 1;
每一轮战斗,让输的人的f[find_father(i)]变成x[i]
但是在执行f[find_father(i)]=x[i]的时候
我们要记录一下ans[find_father(i)] = x[i]
因为到最后并查集经过路径压缩以后,就没有明显的主从关系了
之后,让li~ri里面的每个nex[i]都等于r[i]+1(要先记录下nex[i]再改变nex[i],因为i需要跳到nex[i],进而继续将li~ri这个区间里面的nex[i]都指向r[i]+1,以及进行
合并操作,把对应区间的子winner指向新winner,记录ans值)就好
这样从左到右枚举的时候就会跳过败者了(只会通过find_father(i)找到这一棵子树的根)
最后输出ans[]就好
注意ans[i]=i的话 那个人是最后的winner
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = (int)3e5;
static class Task{
int n,m;
int nex[],f[],ans[];
int ff(int x) {
if (x==f[x]) return x;
else {
f[x] = ff(f[x]);
return f[x];
}
}
public void solve(InputReader in,PrintWriter out) {
nex = new int[N+10];f = new int[N+10];ans = new int[N+10];
for (int i = 1;i <= N;i++) {
nex[i] = i + 1;
f[i] = i;
}
n = in.nextInt();m = in.nextInt();
for (int i = 1;i <= m;i++) {
int li,ri,xi;
li = in.nextInt();ri = in.nextInt();xi = in.nextInt();
for (int j = li;j <= ri;) {
int fa = ff(j);
f[fa] = xi;
ans[fa] = xi;
int temp = nex[j];
nex[j] = ri+1;
j = temp;
}
}
for (int i = 1;i <= n;i++) {
if (ans[i]==i)
out.print(0);
else
out.print(ans[i]);
out.print(" ");
}
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
【Codeforces 356A】Knight Tournament的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 1450:【例 3】Knight Moves
1450:[例 3]Knight Moves 题解 这道题可以用双向宽度搜索优化(总介绍在 BFS ) 给定了起始状态和结束状态,求最少步数,显然是用BFS,为了节省时间,选择双向BFS. 双向B ...
- 【codeforces 602E】Kleofáš and the n-thlon
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 752F】Santa Clauses and a Soccer Championship
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- P3297 [SDOI2013]逃考
传送门 完全看不出这思路是怎么来的-- 首先对于两个亲戚,他们监视范围的边界是他们连线的中垂线.那么对于一个亲戚来说它能监视的范围就是所有的中垂线形成的半平面交 然后如果某两个亲戚的监视范围有公共边, ...
- 面试官:聊一下你对MySQL索引实现原理?
在数据库中,如果索引太多,应用程序的性能可能会受到影响,如果索引太少,又会对查询性能产生影响.所以,我们要追求两者的一个平衡点,足够多的索引带来查询性能提高,又不因为索引过多导致修改数据等操作时负载过 ...
- 源码阅读之LinkedHashMap(JDK8)
概述 LinkedHashMap继承自HashMap,实现了Map<K,V>接口.其内部还维护了一个双向链表,在每次插入数据,或者访问.修改数据时,会增加节点.或调整链表的节点顺序.以决定 ...
- fiddler不同代理模式的区别
Fiddler有不同的代理模式,分为以下两种: 流模式(Streaming)和缓冲模式(Buffering). 流模式可以理解为一种实时通信的模式,有请求就有返回,也就是实时返回. 缓冲模式是等所有请 ...
- 64位linux安装wine等软件
我的系统是centos7 64位的,安装wine的时候以为和32位的一样,结果执行./configure的时候,出现错误(无法建立一个32位程序,您需要安装32位的开发库) configure: er ...
- Java 8 (1) 行为参数化
行为参数化就是可以帮助你处理频繁变更需求的一种软件开发模式.它意味着拿出一个代码块,把它准备好却不去执行它.这个代码块以后可以被你程序的其他部分调用,这意味着你可以推迟这块代码的执行.例如:你可以将代 ...
- LN : leetcode 538 Convert BST to Greater Tree
lc 538 Convert BST to Greater Tree 538 Convert BST to Greater Tree Given a Binary Search Tree (BST), ...
- cocos2d-x win7 部署
1. 安装 下载python https://www.python.org/downloads/release/python-279/ 2.从官网下载cocos2d-x http://www.co ...
- putty中文乱码问题解决
###putty中文乱码问题解决 用putty从windows xp连接ubuntu server或者FreeBSD系统,其中中文部分乱码,经常遇到这个问题的时候,会觉得很郁闷.现共享一些解决这个问题 ...
- 盒子模型,top和margin-top
1. 标准盒子模型: width只是内容的宽度. 元素的总宽度=width + padding*2 +border*2 +margin*2. IE盒子模型: width=内容的宽度 + padding ...