CF #542 A1 - Toy Train (Simplified)

A1. Toy Train (Simplified)

This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.

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.

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.

题意:有一个环形轨道,有n个车站,轨道上面有个小火车,它能装无限颗糖,但是在每一个站只能往上面装一颗。每颗糖需要运送到对应的车站,而且糖不会出现在需要去的车站里,问对于每一个车站,火车从这里出发最少需要多少秒才能把全部糖送完(火车每一秒走到下一个站)。

分析

对于一个车站里的糖果,因为一次只能取一颗,所以我们要考虑取糖的先后顺序以达到最优。那么由于轨道是圆形的,所以你在送前n颗糖是没有区别的,因为你还是得要回来。所以区别就是在最后一颗糖上,我们只要让最后一颗糖离车站最近就行了。那么从一个车站出发的最优解就是从max(每一个车站出发需要的时间+到这个车站的时间)。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<map>
#include<stack>
#include<string>
#include<algorithm>
#include<cmath>
#define rg register
#define il inline
using namespace std;
typedef unsigned long long ll;
ll read(){
    ll ans=0,flag=1;char ch;
    while((ch=getchar())<'0'||ch>'9') if(ch=='-') flag=-1;
    ans=ch^48;
    while((ch=getchar())>='0'&&ch<='9') ans=(ans<<3)+(ans<<1)+(ch^48);
    return flag*ans;
}
void WRI(ll x){
    if(x<0){putchar('-');x=-x;}
    if(x>9) WRI(x/10);
    putchar(x%10+'0');
}
const ll mod=1e9+7;
void write(ll x,char o){WRI(x),putchar(o);}
int m[105],need[105],s,candy;
int main(){
    int n=read(),x=read();
    for(rg int i=0;i<x;i++){
        s=read(),candy=read();
        need[s]++;
        int dis=s<candy?candy-s:(n-(s-candy));
        if(need[s]==1) m[s]=dis;
        else m[s]=min(dis,m[s]);
    }
    for(rg int i=1;i<=n;i++){
        if(need[i]>=1) need[i]=(need[i]-1)*n+m[i];
        else need[i]=0;
    }
    for(rg int i=1;i<=n;i++){
        int ans=0;
        for(rg int j=1;j<=n;j++){
            if(need[j]==0) continue;
            int dis=i<=j?j-i:(n-(i-j));
            ans=max(dis+need[j],ans);
        }write(ans,' ');
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ffscas/article/details/88081827