Problem D1 - Toy Train (Simplified)

Alice received a set of Toy Train™ from Bob. It consists of one train and a connected railway network of nn stations, enumerated from 11 through 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.

![在这里插入图片描述](https://img-blo
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.

Input
The first line contains two space-separated integers nn and mm (2≤n≤1002≤n≤100; 1≤m≤2001≤m≤200) — 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.

Output
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.

Examples
inputCopy
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
outputCopy
10 9 10 10 9
inputCopy
2 3
1 2
1 2
1 2
outputCopy
5 6
Note
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.

解题思路:由于火车是一圈一圈转的,假如某个站有n个candy,那么从它开始,肯定是要走3圈多,一圈搞定一个,所以我们只要关心最后一个candy,当然离它越近越好,还有就是要加上这个站到起点的距离,然后我们就只要枚举1到n个站,那个站所需时间最长,就是答案。
上代码:

#include<iostream> 
using namespace std;
const int N=5001; 
int n,m,x,y,c[N],p[N],A;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    p[i]=N;
    while(m--&&cin>>x>>y)
    c[x]++,p[x]=min(p[x],(y+n-x)%n);
    for(int i=1;i<=n;i++)
    {
        A=0;
        for(int j=1;j<=n;j++)
        if(c[j])
        A=max(A,(j+n-i)%n+(c[j]-1)*n+p[j]);
        cout<<A<<" ";
        
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值