Knight Tournament
Codeforces Round #207 (Div. 1) A:http://codeforces.com/problemset/problem/356/A
题意:给你n匹马,然后有m场比赛。每场比赛有一个l,r,x,表示编号从l,r之间的马都被x给打败了,但是这里如果之前已经败了,那么这里的区间就不会包括了。
题解:题解,直接用一个set搞定。
思维过程:首先,想到,对于一场比赛来说,如果之前已经败了的话,这里就不用在考虑了,但是如何判断这个区间里面的马已经败了,朴素的想法,就是查询还是在这个区间,但是我把之前的已经败的马打上表示,然后再在这个区间内逐个进行判断,这样判断的话,区间内的每个元素都会被判断一次,可想而知,这样的想法是不对。时间复杂度hold不住。如何对于那些已经败的马不用判断。这里,可以用一个set来搞,把所有的元素放进set,当要删除一个区间的时候,只要用lower_bound找到第一个不小于l的数,然后往后找,直到到达右端点。然后把找到的点,从set中删除即可。这样,每次查询只在没有删除的马中进行,所以减少了许多不必要的判断。现在考虑复杂度。每个数,会被放进set和从set中删除一次,所以复杂度是n(logn).一定要利用题目中的各种信息,然后利用好自己的思维信息,寻找突破口,一点一滴的,总会有解决的方法。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
using namespace std;
const int N=3e5+;
int ans[N],temp[N],top;
int n,m,l,r,x;
int main(){
while(~scanf("%d%d",&n,&m)){
set<int>Q;
for(int i=;i<=n;i++){
Q.insert(i);
}
for(int i=;i<=m;i++){
scanf("%d%d%d",&l,&r,&x);
top=;
set<int>::iterator it=Q.lower_bound(l);
for(;it!=Q.end();it++){
if((*it)>r)break;
if((*it)==x){
ans[x]=x;
}
else{
ans[(*it)]=x;
temp[++top]=(*it);
}
}
for(int i=;i<=top;i++){
Q.erase(temp[i]);
}
top=;
}
for(int i=;i<n;i++){
if(ans[i]==i)printf("0 ");
else
printf("%d ",ans[i]);
}
if(ans[n]==n)printf("0\n");
else
printf("%d\n",ans[n]);
} }
Knight Tournament的更多相关文章
- CodeForce 356A Knight Tournament(set应用)
Knight Tournament time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Knight Tournament 合并区间
Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the me ...
- Knight Tournament (set)
Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the me ...
- CodeForces - 357C Knight Tournament 伪并查集(区间合并)
Knight Tournament Hooray! Berl II, the king of Berland is making a knight tournament. The king has a ...
- D - Knight Tournament(set)
Problem description Hooray! Berl II, the king of Berland is making a knight tournament. The king has ...
- codeforces 357C Knight Tournament(set)
Description Hooray! Berl II, the king of Berland is making a knight tournament. The king has already ...
- Codeforces Round #207 (Div. 1) A. Knight Tournament(STL)
脑子又卡了...来一发set的,STL真心不熟. #include <stdio.h> #include <string.h> #include <iostream> ...
- cf C. Knight Tournament
http://codeforces.com/contest/357/problem/C #include <cstdio> #include <cstring> #includ ...
- Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)
题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...
随机推荐
- 通过WriteProcessMemory改写进程的内存
http://www.cnblogs.com/feiyucq/archive/2009/10/21/1587628.html 以PROCESS_ALL_ACCESS权限打开进程以后既能够使用ReadP ...
- [RxJS] Marble diagrams in ASCII form
There are many operators available, and in order to understand them we need to have a simple way of ...
- android 55
智能家居:可以联网可以用指令操作可以返回状态.智能微波炉智能眼镜智能手表. Android作者Andy Rubin,2007年正式由谷歌推广,开源的. 安卓体系结构和开发一个闹钟的调用结构图: 安卓和 ...
- viewpager+fragment学习笔记
有暇,总结一下viewpager+fragment的使用. 先来看看效果图: 有三个标题,三个fragment,滑动时标题的颜色会随着变化. MainActivity.java public clas ...
- frame模型
#import "CZWeiboFrame.h" #import "CZWeibo.h" @implementation CZWeiboFrame #defin ...
- 如何在单元测试中测试异步函数,block回调这种
大概有四种方法: runloop 阻塞主进程等待结果 semphaore 阻塞主进程等待结果 使用XCTestExpectation 阻塞主线程等待(我用这个,xcode自带的,为啥不用) 使用第三方 ...
- poj 3565 ants
/* poj 3565 递归分治 还有用KM的做法 这里写的分治 按紫书上的方法 不过那里说的有点冗杂了 可以简化一下 首先为啥可以分治 也就是分成子问题解决 只要有一个集合 黑白的个数相等 就一定能 ...
- C# 内存管理优化实践
内存优化畅想系列文章已经结束了,很多读者读完之后可能觉得“然并卵”,毕竟都是给微软提的建议而已,现在都没有实现.那么为了优化内存,有没有什么我们现在就能用的技巧呢?我的答案是:有.网上关于.net内存 ...
- Asp.net中向前端输出JS的一些调用
最近突然写ASP.NET项目,用到向前台输出JS脚本,但是以前在MVC里是通过异步或者一些方法来调用,但是ASP.net用到的很少.在网上找到一个HELPER.CS.保存一下,以后再用. using ...
- listActivity和ExpandableListActivity的简单用法
http://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html 今天自己简单的总结了listActivity和Expandable ...