Toy Train (Simplified)

Toy Train (Simplified)                 题目链接

          time limit per test   2 seconds

          memory limit per test   256 megabytes

 


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

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


先装距终点站离最远的糖果

 

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
const ll mod = 1000000007;
const int N = 100005;
vector<int> car;
int n;
int s[200005], e[200005] ; // shi zhong
vector<int> ans ;
vector<int> v[100005];

bool in(int a, int b) {
	if ((0 <= a && a < n) && (0 <= b && b < n)) return true;
	return false;
}

int main() {
	int n, m;
	cin >> n >> m;
	rep(i, 0, m) {
		//int x, y;
		cin >> s[i] >> e[i];
		s[i]--;e[i]--; 
		//v[s[i]].pb(e[i]);
	}

	for (int i = 0; i < n; i++) {
		int j = i; int time = -1;
		rep(f, 0, m) {
			v[(s[f]+n-j)%n].pb( (e[f] + n - j) % n );
		}
		
		rep(i, 0, n) {
			sort(v[i].begin(), v[i].end());
		}
//		cout<<endl<<endl;
//		rep(g,0,n){
//			rep(r,0,v[g].size()){
//				cout<<v[g][r]<<' ';
//			}
//			cout<<endl;
//		}
//		cout<<endl<<endl;
		
		j = 0;
		int tot = 0;
		while (tot < m) {		
			if (car.size()) {
				int num = 0;
				rep(k, 0, car.size()) {
					if (car[k] == j) {
						car[k] = 400, num++;
						tot++;
					}
				}
				sort(car.begin(), car.end()); car.resize(car.size() - num);
			}
			
			// 装车 
			if (v[j].size()) {
				int len = v[j].size();
				for(int eg=(j-1+n)%n;;eg--){
					eg=(eg+n)%n; 
					for(int g=0;g<len;g++){
						if(v[j][g]==eg) {
							car.pb(eg);
							v[j][g]=400;
							eg=n;break;
						}
					}
					if(eg==n) break;
				}
				
				//int max=0, mid;
				sort(v[j].begin(), v[j].end());
				v[j].pop_back();
			}
			
			
			j = (j + 1) % n; time++;
		}
		ans.pb(time);
	}
	rep(i, 0, ans.size()) {
		printf("%d ", ans[i]); 
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值