Toy Train

127 阅读1分钟

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

原题

有点曲折,最主要是最后一圈和倒数第二圈的处理

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#define mem(a,x) memset(a,x,sizeof(a))
#define s1(x) scanf("%d",&x)
#define s2(x,y) scanf("%d%d",&x,&y)
#define s3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s4(x,y,z,k) scanf("%d%d%d%d",&x,&y,&z,&k)
#define ls 2*rt
#define rs 2*rt+1
#define lson ls,L,mid
#define rson rs,mid+1,R
#define ll long long
using namespace std;
typedef pair<int,int> pii;
//inline ll ask(int x){ll res=0;while(x)res+=c[x],x-=x&(-x);return res;}
//inline void add(int x,int d){while(x<=n)c[x]+=d,x+=x&(-x);}
//int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b);}
const int inf = 0x3f3f3f3f;
const int mx = 5005	;
int  n,m;
int co[mx],best[mx],b[mx];
int ans[mx];

int main(){
//	freopen("C:\\Users\\black\\Desktop\\in.txt","r",stdin);
	//int T=10;	scanf("%d",&T);
	s2(n,m);
	mem(best,inf);
	int st,en,len,qu=0;
	for(int i=0; i < m;i++){
		s2(st,en);	
		if(st == en ) continue;
		len = en-st;
		if(len < 0) len += n;
		co[st]++;
		qu = max(co[st],qu);
		if(len < best[st]){
			best[st]=len;
			b[st]=en;
		}
		//if(int){	}
	}
	int te = 1ll*(qu-1)*n;
	for(int k = 1; k <= n; k++){
	    int bl=0;
		for(int i = 1; i <= n; i++){
			if(co[i] < qu-1|| co[i] == 0) continue;   // =0注意, co[i] = qu-1 注意 
			st = i - k; if(st < 0) st += n;
			en =b[i] - k;  if(en < 0) en += n;
			if(co[i] == qu-1){
				if(en < st)
					len = en;
				else 
					continue;
			}
			else{
				len = en;
				if(en < st)
					len += n;
			}
			
			bl = max(bl,len);
		}
		ans[k] = bl + te;
	}
	
	for(int i = 1; i <= n ;i++){
		if(i==1) printf("%d",ans[i]);
		else printf(" %d",ans[i]);
	} 

	return 0;
}

 

本文已参与「新人创作礼」活动,一起开启掘金创作之路