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 ...
随机推荐
- python 判断是否为有效域名
import re pattern = re.compile( r'^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|' r'([a-zA-Z]{1}[0-9]{1}) ...
- Python+页面元素高亮源码实例
简单写了一个页面元素高亮的方法,原理就是在python中调用js实现元素高亮,分享一下源码如下: 1.元素高亮源码 Js调用 js = "var q=document.getElementB ...
- poj 2411((多米诺骨牌问题))
Mondriaan's Dream Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 12854 Accepted: 748 ...
- Ruby 动态生成变量
创建: 2018/03/21 更新: 2018/03/22 把标题ruby首字母大写 方法一: eval将字符串作为代码执行, 故写在里边 eval("@#{view_name.to_s} ...
- React 事件 传参
绑定事件用 bind方法,第一个参数是 this,第二个参数是需要传入的值如下: <a onClick={this.updateLabel.bind(this,'参数1','参数2')} > ...
- [App Store Connect帮助]七、在 App Store 上发行(3.3)提交至“App 审核”:解决 App 拒绝问题
如果“App 审核”或“Beta 版 App 审核”拒绝了您的 App,您可以与 Apple 沟通并在解决方案中心中解决问题. 来自 Apple 的通讯信息包含有关 App 拒绝的信息,其中包括该 A ...
- 关于数学函数中的abs——————————————杭电2057——————————————————————————
数学函数中的abs当你用abs之后括号之中的数字就转换成了int格式.可能会丢失一些数据造成误差而且还会有, 警告: #include<stdio.h> #include<ma ...
- 进击的Python【第十六章】:Web前端基础之jQuery
进击的Python[第十六章]:Web前端基础之jQuery 一.什么是 jQuery ? jQuery是一个JavaScript函数库. jQuery是一个轻量级的"写的少,做的多&quo ...
- QString:常用成员函数总结
QString是Qt中使用频率最高的几种数据类型之一,主要在于其提供了大量功能强大的成员函数,这里重点介绍一些常用的成员函数: 一.字符串处理相关 1.1 split() (拆分字符串) split( ...
- Service官方教程(11)Bound Service示例之2-AIDL 定义跨进程接口并通信
Android Interface Definition Language (AIDL) 1.In this document Defining an AIDL Interface Create th ...