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> ...
随机推荐
- tar.bz2
tar -xvjf gcc-4.1.0.tar.bz2 bzip2 -d gcc-4.1.0.tar.bz2
- java实现排序的几种方法
package com.ywx.count; import java.util.Scanner; /** * 题目:排序的几种方式(汇总及重构) * @author Vashon(yangwenxue ...
- EJB2的配置
1. ejb-jar.xml <?xml version="1.0" encoding="UTF-8"?> <ejb-jar xmlns=&q ...
- 第十三周翻译:SQL Server安全级别1:SQL Server安全性概述
SQL Server安全级别1:SQL Server安全性概述 源自:Stairway to SQL Server Security Level 1: Overview of SQL Server S ...
- echarts简单用法快速上手
1.html结构 简单说就是一个标签一个图表:2.初始化:var myEcharts = echarts.init(document.getElementById("xxx")): ...
- 洛谷 P2515 [HAOI2010]软件安装
题目描述 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大). 但是 ...
- C3P0连接池工具类实现步骤及方法
C3P0连接池的工具类 使用C3P0获得连接对象连接池有一个规范接口 javax.sal.DataSourse 接口定义了一个从连接池中获得连接的方法getConnection(); 步骤导入jar包 ...
- log4j.xml 精选的log4j.xml文档,比较详细,网上的版本很多,这个版本相对而言比较完整
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE log4j:configuration PUB ...
- Java ArrayList中去掉相同的元素并保留相同元素中的最后一个
实现思路:将list对象循环两次,拿外层数据和里面的数据对比,一样的删除外层(外层元素肯定比内存的靠前),如果一样的话,删除外层数据,这样最后输出外层数据的list,结果就能保证唯一性,并且保留了后面 ...
- LayuI固定块关闭
1.近期项目使用了layui的固定块,但是当到某个独立页面时,固定块还在,就显得突兀: 2.通过F12查看,发现代码: <ul class="layui-fixbar" st ...