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> ...
随机推荐
- AJPFX的内存管理小结
管理范围:任何继承于 NSObject的对象原理:每一个对象都有引用计数器当使用alloc new 和 copy创建对象时引用计数器被设置为1给对象发送一条retain消息 ,引用计数器加1 ...
- How `delete’ works ?
这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=21 February 16, 2013 How `delete’ works ...
- PMP项目管理学习笔记(8)——整个管理之监控项目工作、综合变更控制、结束项目或阶段
监控项目工作 输入:企业环境要素.组织过程资产.项目管理计划.绩效报告 工具:专家判断 输出:变更请求.项目管理计划更新.项目文档更新 综合变更控制 输入:企业环境要素.组织过程资产.项目管理计划.变 ...
- RPC(Remote Procedure Call Protocol)远程过程调用协议
RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议的存在 ...
- docker使用阿里云镜像加速器(属于自己的专属加速器)
https://cr.console.aliyun.com/cn-shanghai/mirrors
- C ++ _多线程笔记
#include<iostream>#include <thread>//创建线程需要添加的头文件 using namespace std;/*thread join(阻塞:主 ...
- day13 函数模块之序列化 random 模块 os模块 sys模块 hashlib模块 collections模块
json import json dic = {'k1':'v1','k2':'v2','k3':'v3'} str_dic = json.dumps(dic) #序列化:将一个字典转换成一个字符串 ...
- 【mybatis】mybatis数据源源码剖析(JNDI、POOLED、UNPOOLED)
一.概述 二.创建 mybatis数据源的创建过程稍微有些曲折. 1. 数据源的创建过程: 2. mybatis支持哪些数据源,也就是dataSource标签的type属性可以写哪些合法的参数? 弄清 ...
- ubuntu18.04 server配置静态ip
最新发布的ubuntu18.04 server,启用了新的网络工具netplan,对于命令行配置网络参数跟之前的版本有比较大的差别,现在介绍如下:1.其网络配置文件是放在/etc/netplan/50 ...
- python基础(二) —— 流程控制语句
编程语言中的流程控制语句分为以下几类: 顺序语句 分支语句 循环语句 其中顺序语句不需要单独的关键字来控制,就是按照先后顺序一行一行的执行,不需要特殊的说明. 下面主要是 分支语句 和 循环语句的说明 ...