D2. Toy Train
2 seconds
256 megabytes
standard input
standard output
Alice received a set of Toy Train™ from Bob. It consists of one train and a connected railway network of nn stations, enumerated from 11through nn. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station ii is station i+1i+1 if 1≤i<n1≤i<n or station 11 if i=ni=n. It takes the train 11 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver mm candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 11 through mm. Candy ii (1≤i≤m1≤i≤m), now at station aiai, should be delivered to station bibi (ai≠biai≠bi).
The blue numbers on the candies correspond to bibi values. The image corresponds to the 11-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
The first line contains two space-separated integers nn and mm (2≤n≤50002≤n≤5000; 1≤m≤200001≤m≤20000) — the number of stations and the number of candies, respectively.
The ii-th of the following mm lines contains two space-separated integers aiai and bibi (1≤ai,bi≤n1≤ai,bi≤n; ai≠biai≠bi) — the station that initially contains candy ii and the destination station of the candy, respectively.
In the first and only line, print nn space-separated integers, the ii-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station ii.
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
10 9 10 10 9
2 3
1 2
1 2
1 2
5 6
Consider the second sample.
If the train started at station 11, the optimal strategy is as follows.
- Load the first candy onto the train.
- Proceed to station 22. This step takes 11 second.
- Deliver the first candy.
- Proceed to station 11. This step takes 11 second.
- Load the second candy onto the train.
- Proceed to station 22. This step takes 11 second.
- Deliver the second candy.
- Proceed to station 11. This step takes 11 second.
- Load the third candy onto the train.
- Proceed to station 22. This step takes 11 second.
- Deliver the third candy.
Hence, the train needs 55 seconds to complete the tasks.
If the train were to start at station 22, however, it would need to move to station 11 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1=65+1=6 seconds.
D2. Toy Train的更多相关文章
- CF1130D Toy Train
D Toy Train 开始时,对于一个点 \(x\) ,若没有糖果需要运走,则不考虑; 否则,若点上有 \(k\) 颗糖果需要运走,火车每次只能搭上 \(1\) 个,显然经过这个点至少 \(k\) ...
- D1. Toy Train (Simplified)
D1. Toy Train (Simplified) time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #542(Div. 2) D1.Toy Train
链接:https://codeforces.com/contest/1130/problem/D1 题意: 给n个车站练成圈,给m个糖果,在车站上,要被运往某个位置,每到一个车站只能装一个糖果. 求从 ...
- Codeforces 1130D1 Toy Train (Simplified) (思维)【贪心】
<题目链接> 题目大意: 有一个的环形火车站,其中有$[1,n] n$个站台,站台上能够放糖果,火车只能朝一个方向移动,如果火车在站台$i$,那么下一秒就会在$i+1$站(如果$i=n$, ...
- Toy Train(贪心)
题目链接:http://codeforces.com/contest/1130/problem/D1 题目大意:给你n个点,然后m条运输任务,然后问你从每个点作为起点是,完成这些运输任务的最小花费?每 ...
- DMOJ IOI '17 P3 - Toy Train【拓扑排序】
传送:https://dmoj.ca/problem/ioi17p3 参考:https://blog.csdn.net/qq_27327327/article/details/80711824 妙啊- ...
- 【Codeforces 1129A】Toy Train
[链接] 我是链接,点我呀:) [题意] 火车从1,2,3...n->1的方式绕圈走.(即每次从i走到i+1) 有一些点有货物需要装载,但是每个点只能装上去一个货物. 每个货物都有目标点卸货点( ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) A - D2
A. Be Positive 链接:http://codeforces.com/contest/1130/problem/A 题意: 给一段序列,这段序列每个数都除一个d(−1e3≤d≤1e3)除完后 ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2)
A. Be Positive 题意:给出一个数组 每个树去除以d(d!=0)使得数组中大于0的数 大于ceil(n/2) 求任意d 思路:数据小 直接暴力就完事了 #include<bits/s ...
随机推荐
- 四:网络--NSURLConnection基本使用
一.NSURLConnection的常用类 (1)NSURL:请求地址 (2)NSURLRequest:封装一个请求,保存发给服务器的全部数据,包括一个NSURL对象,请求方法.请求头.请求体.... ...
- Bootstrap指定表格字段宽度
默认情况下,表格会占据屏幕所有宽度,可以使用bootstrap的布局功能.但表格标题字段的宽度必须与相应字段内容的宽度一致. <div class="container"&g ...
- 关于Lucene全文检索相关技术
Lucene技术专门解决海量数据下的模糊搜索问题. Lucene主要完成的是数据预处理.建立倒排索引,及搜索.排名.高亮显示等功能 全文检索相关词语概要: 单词和文档矩阵: 文档(Document): ...
- Pascal之工种问题
program Project2; Type ma=(A,B,C); wk=..; const x:array[ma,wk] of integer =((,,),(,,),(,,)); var max ...
- 微信公众号开发——创建自定义菜单(PHP版)
<?php include "TokenUtil.php"; //TokenUtil::build_access_token(); $access_token = Token ...
- linux下解压tgz文件(转载)
转自:http://www.blogjava.net/chenlb/archive/2008/09/03/226654.html .tgz 解压: tar zxvf myfile.tgz
- 洛谷 P3825 [NOI2017]游戏 【2-SAT+状压】
UOJ和洛谷上能A,bzoj 8ms即WA,现在也不是知道为啥--因为我太弱了 先看数据范围发现d非常小,自然想到了状压. 所以先假装都是只能跑两种车的,这显然就是个2-SAT问题了:对于x场没有hx ...
- idea 启动时报 error:java 无效的源发行版11
编译的版本不符合,需要修改统一
- [Usaco2005 Nov]Asteroids
Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...
- ACM输入外挂
一.什么是输入挂 scanf的输入速度不cin快得多,那么有没有比scanf更快的东西呢?这就是要用到输入挂了. 二.什么时候使用输入挂 当输入规模达到1x10^6次方的时候,就需要输入挂,否则很可能 ...