CF1129A2.Toy Train
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Alice received a set of Toy Train™ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n . 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 i is station i+1 if 1≤i<n or station 1 if i=n . It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m . Candy i ( 1≤i≤m ), now at station ai , should be delivered to station bi ( ai=bi ).
The blue numbers on the candies correspond to bi values. The image corresponds to the 1 -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 n and m ( 2≤n≤5000 ; 1≤m≤20000 ) — the number of stations and the number of candies, respectively.
The i -th of the following m lines contains two space-separated integers ai and bi ( 1≤ai,bi≤n ; ai=bi ) — the station that initially contains candy i and the destination station of the candy, respectively.
输出格式
In the first and only line, print n space-separated integers, the i -th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i .
输入输出样例
输入#1
5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5
输出#1
10 9 10 10 9
输入#2
2 3 1 2 1 2 1 2
输出#2
5 6
说明/提示
Consider the second sample.
If the train started at station 1 , the optimal strategy is as follows.
- Load the first candy onto the train.
- Proceed to station 2 . This step takes 1 second.
- Deliver the first candy.
- Proceed to station 1 . This step takes 1 second.
- Load the second candy onto the train.
- Proceed to station 2 . This step takes 1 second.
- Deliver the second candy.
- Proceed to station 1 . This step takes 1 second.
- Load the third candy onto the train.
- Proceed to station 2 . This step takes 1 second.
- Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2 , however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1=6 seconds.