codeforces1130D2. Toy Train(思维+贪心)

                                                D2. Toy Train

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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

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

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

input

Copy

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

output

Copy

10 9 10 10 9 

input

Copy

2 3
1 2
1 2
1 2

output

Copy

5 6 

Note

Consider the second sample.

If the train started at station 11, the optimal strategy is as follows.

  1. Load the first candy onto the train.
  2. Proceed to station 22. This step takes 11 second.
  3. Deliver the first candy.
  4. Proceed to station 11. This step takes 11 second.
  5. Load the second candy onto the train.
  6. Proceed to station 22. This step takes 11 second.
  7. Deliver the second candy.
  8. Proceed to station 11. This step takes 11 second.
  9. Load the third candy onto the train.
  10. Proceed to station 22. This step takes 11 second.
  11. 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个点的环,一辆火车只能单向前进。每个节点有一些货物要运到另外一些节点,每一个节点,只能上一个货,但能卸任意数量的货,假设火车容量无限,求运输完所有货物所需的最少时间。

 

三、大致思路

假设某一个节点 i 有数量为 q 的货物,那么对于这个点来讲,首先需要从起点 S 到达这个点 i ,费用为dis(S,i)。因为一次只能拿移动货物走,所以火车一定会先经过这个点 q - 1 次,那么费用就是 n*( q - 1 ),最后还要考虑最后一个货物要被运到目标点 t ,费用就是 i 点和 t 点的距离dis( i , t )。

上面的三个费用加起来就是某个点的费用,所以我们枚举起点S和他所有要考虑的点 i ,这样就确定了dis(S,i),在枚举 i 的时候n*( q - 1 )就是固定的了,然后只需要考虑最后一个 t 点和枚举到的 i 点的dis就好了,这里贪心一下,取这个点货物集合里面目的地最近的一个来更新dis( i , t )。

原本以为加强的数据5000个点n^2的复杂度肯定过不去,没想到机器跑的飞快,神奇。

 

四、丑陋代码

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<stack>
#include<bitset>
using namespace std;
#define PI 3.14159265
const int inf=0x3f3f3f3f;
typedef long long LL;
typedef unsigned long long ull;



int n,m;
vector<int>p[5005];
int minn[5005];
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)minn[i]=-1;

    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d %d",&x,&y);
        p[x].push_back(y);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<p[i].size();j++)
        {
            int to =p[i][j];
            if(to>i)
            {
                if(minn[i]==-1)minn[i]=(to-i);
                else minn[i]=min(minn[i],to-i);
            }
            else
            {
                if(minn[i]==-1)minn[i]=(n-i+to);
                else minn[i]=min(minn[i],n-i+to);
            }
        }
    }

    for(int i=1;i<=n;i++)
    {
        LL ans=0;
        for(int j=1;j<=n;j++)
        {
            LL now=0;
            if(j>=i)
            {
                now+=j-i;
            }
            else
            {
                now+=n-i+j;
            }
            int S=p[j].size();
            if(S==0)continue;
            else
            {
                now+=(S-1)*n+minn[j];
            }
            ans=max(now,ans);
        }
        printf("%d ",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值