AtCoder ABC 128E Roadwork
题目链接:https://atcoder.jp/contests/abc128/tasks/abc128_e
题目大意
在一条路上(这条路可以看做坐标轴 x 轴从 0 开始方向为正无穷方向的射线),有 N 个道路工程,每个道路工程用三元组$(S_i, T_i, X_i)$表示,意思是第 i 个道路工程在$S_i$时间点开始,在$T_i$时间点完全结束(持续时间区间不包括$T_i$),施工地点是在 x 轴上的$X_i$位置。在同一地点的施工工程,他们的时间不会重叠。现在有 Q 个人依次从原点以一米每秒的速度在$D_i$时间点出发,碰到正在施工的工程就停下不走了,问每个人最后停下来的位置,如果停不下来,输出-1。
分析
- $d_i \in \{0, 1\}$。
- 当$d_i = 0$时,$t_i = S_i - X_i$。
- 当$d_i = 1$时,$t_i = T_i - X_i$。
- $x_i = X_i$。
然后专门用一个集合存放与当前$D_i$有关的$X_i$,把三元组按照$t_i$排个序,然后顺序遍历所有$t_i \leq D_i$的事件,如果$d_i = 0$,就把$x_i$加入集合,否则就从集合中删去。
然后集合中最小的位置就是当前人所停留的位置。
代码如下
#include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< string, int > PSI;
typedef set< int > SI;
typedef vector< int > VI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef multimap< int, int > MMII;
typedef unordered_map< int, int > uMII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 2e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; struct Tuple{
int T, X, D; inline bool operator< (const Tuple &x) const{
return T < x.T;
}
}; int N, Q;
Tuple event[maxN << ];
int len;
multiset< int > msi; int main(){
//INIT();
N = ri();
Q = ri();
For(i, , N) {
int S, T, X;
S = ri();
T = ri();
X = ri(); event[++len].T = S - X;
event[len].X = X;
event[len].D = ; event[++len].T = T - X;
event[len].X = X;
event[len].D = ;
}
sort(event + , event + len + ); int k = , D;
For(i, , Q) {
D = ri();
//********************************************************************
while(k <= len) {
if(event[k].T <= D) {
if(event[k].D) msi.insert(event[k].X);
else msi.erase(msi.find(event[k].X));
++k;
}
else break;
} /*
血的教训,一开始我写的不是while循环,而是for循环,貌似是没问题
但是提交一直超时,原因是某些案例使得else分支一直没有执行到,于是时间复杂度就激增了
贴上错误代码,引以为戒
For(j, k, len) {
if(event[j].T <= D) {
if(event[j].D) msi.insert(event[j].X);
else msi.erase(msi.find(event[j].X));
++k;
}
else {
k = j;
break;
}
}
*/ //******************************************************************** int ans = -;
if(msi.size()) ans = *msi.begin();
printf("%d\n", ans);
}
return ;
}
AtCoder ABC 128E Roadwork的更多相关文章
- ATCODER ABC 099
ATCODER ABC 099 记录一下自己第一场AK的比赛吧...虽然还是被各种踩... 只能说ABC确实是比较容易. A 题目大意 给你一个数(1~1999),让你判断它是不是大于999. Sol ...
- Atcoder ABC 141
Atcoder ABC 141 A - Weather Prediction SB题啊,不讲. #include<iostream> #include<cstdio> #inc ...
- Atcoder ABC 139E
Atcoder ABC 139E 题意: n支球队大循环赛,每支队伍一天只能打一场,求最少几天能打完. 解法: 考虑抽象图论模型,既然一天只能打一场,那么就把每一支球队和它需要交手的球队连边. 求出拓 ...
- Atcoder ABC 139D
Atcoder ABC 139D 解法: 等差数列求和公式,记得开 $ long long $ CODE: #include<iostream> #include<cstdio> ...
- Atcoder ABC 139C
Atcoder ABC 139C 题意: 有 $ n $ 个正方形,选择一个起始位置,使得从这个位置向右的小于等于这个正方形的高度的数量最多. 解法: 简单递推. CODE: #include< ...
- Atcoder ABC 139B
Atcoder ABC 139B 题意: 一开始有1个插口,你的插排有 $ a $ 个插口,你需要 $ b $ 个插口,问你最少需要多少个插排. 解法: 暴力模拟. CODE: #include< ...
- Atcoder ABC 139A
Atcoder ABC 139A 题意: 给你两个字符串,记录对应位置字符相同的个数 $ (n=3) $ 解法: 暴力枚举. CODE: #include<iostream> #inclu ...
- atcoder abc 244
atcoder abc 244 D - swap hats 给定两个 R,G,B 的排列 进行刚好 \(10^{18}\) 次操作,每一次选择两个交换 问最后能否相同 刚好 \(10^{18}\) 次 ...
- AtCoder ABC 250 总结
AtCoder ABC 250 总结 总体 连续若干次一样的结果:30min 切前 4 题,剩下卡在 T5 这几次卡在 T5 都是一次比一次接近, 什么 dp 前缀和打挂,精度被卡,能水过的题连水法都 ...
随机推荐
- 牛客 某练习赛 Data Structure
Data Structure 题目描述 将一个非负整数序列划分为 \(K\) 段,分别计算出各段中的整数按位或的结果,然后再把这些结果按位与起来得到一个最终结果,把这个最终结果定义为这个序列的一个 \ ...
- delphi INI文件
INI 文件读写 filecreate('路径加文件名')://创建一个文件. (1) INI文件的结构: ;这是关于INI文件的注释部分 [节点] 关键字=值 ... INI文件允许有多个节点,每个 ...
- Ext OOP基础
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- STM32时钟初始化函数SystemInit()详解【转】
花了一天的时间,总算是了解了SystemInit()函数实现了哪些功能,初学STM32,,现记录如下(有理解错误的地方还请大侠指出): 使用的是3.5的库,用的是STM32F107VC,开发环境RVM ...
- Navicat 连接MongoDB 查询语句
https://www.cnblogs.com/viviman/archive/2012/11/21/2780562.html
- jQuery 对文档的操作
通过jquery方式实现页面各种节点的追加.修改.删除.复制等操作 节点追加 1 父子关系追加 /*************************************************** ...
- layui的选项卡(tab)的问题
当页面打开单个tab时,操作栏显示: 当页面打开多个tab时,会发现操作栏与下面第一个tab显示的操作栏类型一样,并且操作栏的按钮无作用 第一个标签操作栏显示: 产生这样的原因:使用layui时,每个 ...
- 使用Python将字符串转换为格式化的日期时间字符串
我正在尝试将字符串“20091229050936”转换为“2009年12月29日(UTC)” >>>import time >>>s = time.strptime ...
- 52-Ubuntu-打包压缩-2-打包/解包
tar是Linux中最常用的备份工具,此命令可以把一系列文件打包到一个大文件中,也可以把一个打包的大文件恢复成一系列文件. 序号 命令 作用 01 tar -cvf 打包文件.tar 被打包文件 打包 ...
- 「SNOI2019」通信 分治优化费用流建图
题意: n 个排成一列的哨站要进行通信.第 i 个哨站的频段为 ai. 每个哨站 ii 需要选择以下二者之一: 1.直接连接到控制中心,代价为 W:2.连接到前面的某个哨站 j(j<i),代价为 ...