CodeForces1230 D2. Toy Train (贪心)

Problem

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

Output

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.

Examples
input

5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5

output

10 9 10 10 9

input

2 3
1 2
1 2
1 2

output

5 6

Note

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.

思路:
对于每一个点,假设当前点为x
如果x上没有糖果,显然不用管
如果x上有k颗糖果,因为每次只能把一颗糖装上车,所以至少经过这个点k次
经过k次则一定已经走完了k-1圈,所以先上车的k-1个糖果肯定已经到达目的地了
那么考虑最后一个糖果,对于点x,最后一个运送的糖果肯定选择目的地距离x最小的
(距离大的优先在前面的k-1圈送掉),这样能保证总路程最小

设dis(a,b)表示把糖果从a送到b所需要的距离
mi[a]表示以a为起点的到达终点所需的最小距离(作为运送的最后一颗糖)
输入的时候预处理mi[]数组

假设出发点为st
则送完点x上的糖需要的最少时间为:
ans=dis(st,x)+n*(k-1)+mi[x]
以st为期间送完所有糖的最少时间就是x从1到n遍历取得的最大值
code:
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
#define int long long
const int maxm=5e3+5;
int mi[maxm];//mi[i]表示点i上的某个糖果到达终点的最短距离
int cnt[maxm];
int n,m;
int dis(int a,int b){//求从a到b行驶的距离
    if(b>=a){
        return b-a;
    }else{//b>a的时候说明要绕一圈到后面,距离为n-abs(a-b)
        return n-(a-b);
    }
}
int solve(int st){
    int ans=0;
    for(int i=1;i<=n;i++){
        if(cnt[i]){
            ans=max(ans,dis(st,i)+n*(cnt[i]-1)+mi[i]);
        }
    }
    return ans;
}
signed main(){
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int a,b;
        cin>>a>>b;
        cnt[a]++;
        if(mi[a]==0){
            mi[a]=dis(a,b);
        }else{
            mi[a]=min(mi[a],dis(a,b));
        }
    }
    for(int i=1;i<=n;i++){
        cout<<solve(i)<<' ';
    }
    cout<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值