【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 [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- foxmail地址簿导入thunderbird的乱码问题 (转载)
转自:http://blog.csdn.net/gexueyuan/article/details/9032595 由于foxmail的地址簿格式和thunderbird的格式不一样,另外也存在编码问 ...
- E20171102-E
segment n. 环节; 部分,段落; [计算机] (字符等的) 分段; [动物学] 节片; distinct adj. 明显的,清楚的; 卓越的,不寻常的; 有区别的; 确切的;
- python实现对某招聘网接口测试获取平台信息
"""通过接口测试的技术获取某招聘网平台的资料"""url = "https://www.lagou.com/jobs/posit ...
- 【题解】二逼平衡树 [P3380] [BZOJ3196] [Tyvj1730]
[题解]二逼平衡树 [P3380] [BZOJ3196] [Tyvj1730] 传送门:[模板]二逼平衡树(树套树)\([P3380]\) \([BZOJ3196]\) \([TYVJ1730]\) ...
- python程序展现图片遇到的坑
使用cv2展示图片的时候遇到了问题,提示:TypeError: Required argument 'mat' (pos 2) not found 给定的图片路径是没得问题的,代码如下: 使用open ...
- [转]ASP .NET MVC 之Entity Framework- code first
本文转自:http://www.cnblogs.com/tomin/archive/2012/02/29/MVC_EntityFramework.html 最近,用到了ASP.NET MVC Ent ...
- Python_购物车问题
import os goods = [ {"name": "电脑", "price": 1999}, {"name&q ...
- ubuntu查看系统资源占用(内存,cpu和进程)
http://blog.csdn.net/vivian187/article/details/51476043 http://bluexp29.blog.163.com/blog/static/338 ...
- 扩展Snackbar 使其支持居中显示
https://github.com/nispok/snackbar 默认Snackbar支持底部或者顶部显示,不支持居中显示 查看Snackbar.java的源码可以看到createMarginLa ...
- HP M177打印机驱动安装问题与解决
问题描述 采购HP Color LaserJet Pro MFP M177 PCLmS一台,采用局域网WIFI网络与办公室各电脑连接.安装打印机自带光盘驱动,发现有些电脑安装驱动失败,提示“出现严重错 ...