【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 [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- 基于JWT机制的单点登录
使用JWT实现单点登录时,需要注意token时效性.token是保存在客户端的令牌数据,如果永久有效,则有被劫持的可能.token在设计的时候,可以考虑一次性有效或一段时间内有效.如果设置有效时长,则 ...
- bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线【线段树+hash】
bzoj题面什么鬼啊-- 题目大意:有一个初始值均为0的数列,n次操作,每次将数列(ai,bi-1)这个区间中的数与ci取max,问n次后元素和 离散化,然后建立线段树,每次修改在区间上打max标记即 ...
- FTP文件服务器
import java.io.InputStream; import java.io.Serializable; import lombok.Data; @Data public class FtpB ...
- 2017杭电多校06Rikka with Graph
Rikka with Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- CDH搭建Hadoop分布式服务器集群(java新手小白)
1首先对于一个java还白的小白,先理解CDH与Hadoop的关系 一.Hadoop版本选择. Hadoop大致可分为Apache Hadoop和第三方发行第三方发行版Hadoop,考虑到Hadoop ...
- 24 C#的类和对象
类是C#面向对象编程的基本单元.一个类都可以包含2种成员:字段和方法. 1)类的字段代表类中被处理的数据(变量): 2)类的方法代表对这些数据的处理过程或用于实现某种特定的功能,方法中的代码往往需 ...
- Laravel5.1学习笔记23 Eloquent 序列化
Eloquent: Serialization Introduction Basic Usage Hiding Attributes From JSON Appending Values To JSO ...
- 图标文件ico制作以及使用说明
今天说一个图标文件——ico.我们在pc端浏览网页的时候网页栏那块都会显示一个本网站特有的图片,就是我们说的ico了.示例:<link href="image/favicon.ico& ...
- 用antlr4来实现《按编译原理的思路设计的一个计算器》中的计算器
上次在公司内部讲<词法分析——使用正则文法>是一次失败的尝试——上午有十几个人在场,下午就只来了四个听众. 本来我还在构思如何来讲“语法分析”的知识呢,但现在看来已不太可能. 这个课程没有 ...
- HTML5——移动端的点击、拖拽
移动端浏览器不支持mouse事件 https://www.cnblogs.com/joyco773/p/6519668.html https://www.cnblogs.com/yjhua/p/525 ...