CodeForces - 356A Knight Tournament
http://codeforces.com/problemset/problem/356/A
首先理解题意
每次给出l 和r 在l - r之间还有资格的选手中得出一个胜者
暴力思路:
首先维护还有资格的选手的集合
用一个数组 表示 这个选手被谁击败
每次遍历 l - r 然后把不是胜者 且 还在集合中的选手踢出 并更新这个选手的数组值
最终 输出这个数组即可
这样会TLE
1、 如果用数组维护这个集合的话 每次遍历都是这样就是O(n^2) -->> 所以用set维护这个集合
2、使用set后 如果每次依然从l - r去遍历找在集合中的元素 去find的话 那么就会在 (l, r)的区间两端有浪费的运算 如果每次l, r 都是1 和 n的话 那就浪费得非常得多
所以使用set :: lower_bound() 直接得到第一个大于l 并在集合中的元素 O(logn)
这样优化后 即可
#include <iostream>
#include <stdio.h>
#include <set>
using namespace std; int Par[];
int tmp[];
set<int> s; int find(int x)
{
if (Par[x] == x) return x;
else return find(Par[x]);
} int main()
{
int n, m;
freopen("in.txt", "r", stdin);
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++)
{
Par[i] = i;
s.insert(i);//加入set中
}
for(int i = ; i < m; i++)
{
int l, r, x;
scanf("%d%d%d", &l, &r, &x);
set<int> :: iterator pit = s.lower_bound(l);//返回第一个>= l的位置
int num = ;
while (pit != s.end() && (*pit) <= r )
{
if ((*pit) != x)
{
Par[*pit] = x;
//s.erase(*pit); 不能在这里直接删除 删除后set结构发生改变 pit就失效了
tmp[num++] = *pit;
}
pit++;
}
for (int j = ; j < num; j++) s.erase(tmp[j]);
}
for (int i = ; i <= n; i++)
{
if (Par[i] == i) printf("0 ");
else printf("%d ", Par[i]);
}
putchar('\n');
}
CodeForces - 356A Knight Tournament的更多相关文章
- CodeForce 356A Knight Tournament(set应用)
Knight Tournament time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- CodeForces - 357C Knight Tournament 伪并查集(区间合并)
Knight Tournament Hooray! Berl II, the king of Berland is making a knight tournament. The king has a ...
- codeforces 357C Knight Tournament(set)
Description Hooray! Berl II, the king of Berland is making a knight tournament. The king has already ...
- 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 ...
- D - Knight Tournament(set)
Problem description Hooray! Berl II, the king of Berland is making a knight tournament. The king has ...
- 【Codeforces 356A】Knight Tournament
[链接] 我是链接,点我呀:) [题意] n个人矩形m场比赛 每场比赛由编号为li~ri且之前没有被淘汰的人进行. 已知第i场的winner是xi winner会把这一场其他所有的人都淘汰. 问你n个 ...
- 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所 ...
- Codeforces Round #207 (Div. 1) A. Knight Tournament(STL)
脑子又卡了...来一发set的,STL真心不熟. #include <stdio.h> #include <string.h> #include <iostream> ...
随机推荐
- oracle 数据导入、导出
导入导出 --数据导出备份和导入 ------注意 导出和导入 必须是CMD 命令行下操作,而不是SQL编辑器中 --1.导出表 . --exp:导出关键字 ,userid:用户权限 ,file:保存 ...
- 通俗易懂的Nhibernate教程(1) ----- 基本操作,映射,CURD
网站架构: 1.图片 2.说明 Data ----------------------- 类库项目,数据访问层,由Nhibernate提供数据相关操作 Mapping ------------- ...
- AlertDialog的几种用法
xml代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...
- B/S网络架构
B/S基于统一的应用层协议HTTP来交互数据,目前的B/S网络架构大多采用如图所示的架构设计,既要满足海量用户访问请求,又要保持用户请求的快速响应. 当一个用户在浏览器输入www.taobao.com ...
- Mac OSX简单使用中会用到的
选择操作系统(例如选择BootCamp分区的Windows):开机按住Option键直到磁盘图标出现后选择. 忘记本地账号密码:按着Command+R开机选择Recovered启动打开终端输入re ...
- 如何破解密码的哈希值,破解双MD5密码值
这是关于我如何破解密码的哈希值1亿2200万* John the Ripper和oclHashcat-plus故事. 这是几个月前,当我看到一条推特:从korelogic约含共1亿4600万个密码的密 ...
- python之路——函数进阶
阅读目录 楔子 命名空间和作用域 函数嵌套及作用域链 函数名的本质 闭包 本章小结 楔子 假如有一个函数,实现返回两个数中的较大值: def my_max(x,y): m = x if x> ...
- C字符串指针遇到的问题
看下面的示例代码: int main() { char *ptr = "GeeksQuiz"; printf("%c\n", *&*&*ptr) ...
- ajax 实现订单商品数量的增减及订单的删除进行异步更新界面
[转载]https://blog.csdn.net/luliuying_01/article/details/78177617?locationNum=8&fps=1 由于在做答辩项目,做到购 ...
- 7 SQL 集合运算
7 集合运算 7-1 表的加减法 本章将会和大家一起学习“集合运算”操作.在数学领域,“集合”表示“(各种各样的)事物的总和”:在数据库领域,表示“记录的集合”.具体来说,表.视图和查询的执行结果都是 ...