1153A A. Serval and Bus

It is raining heavily. But this is the first day for Serval, who just became 3 years old, to go to the kindergarten. Unfortunately, he lives far from kindergarten, and his father is too busy to drive him there. The only choice for this poor little boy is to wait for a bus on this rainy day. Under such circumstances, the poor boy will use the first bus he sees no matter where it goes. If several buses come at the same time, he will choose one randomly.

Serval will go to the bus station at time t, and there are n bus routes which stop at this station. For the i-th bus route, the first bus arrives at time si minutes, and each bus of this route comes di minutes later than the previous one.

As Serval’s best friend, you wonder which bus route will he get on. If several buses arrive at the same time, you can print any of them.

Input
The first line contains two space-separated integers n and t (1≤n≤100, 1≤t≤105) — the number of bus routes and the time Serval goes to the station.

Each of the next n lines contains two space-separated integers si and di (1≤si,di≤105) — the time when the first bus of this route arrives and the interval between two buses of this route.

Output
Print one number — what bus route Serval will use. If there are several possible answers, you can print any of them.

Examples
input

2 2
6 4
9 5
output
1
input
5 5
3 3
2 5
5 6
4 9
6 1
output
3
input
3 7
2 2
2 3
2 4
output
1
Note
In the first example, the first bus of the first route arrives at time 6, and the first bus of the second route arrives at time 9, so the first route is the answer.

In the second example, a bus of the third route arrives at time 5, so it is the answer.

In the third example, buses of the first route come at times 2, 4, 6, 8, and so fourth, buses of the second route come at times 2, 5, 8, and so fourth and buses of the third route come at times 2, 6, 10, and so on, so 1 and 2 are both acceptable answers while 3 is not.
题目大意: Serval要去公园,有n辆公交,且他在t时刻到达公交站,下面n行给出1-n个路线汽车的最开始达到时间和该趟车的间隔时间,问Serval最早应该乘坐哪一路线车(多个输出任意一个解)。
思路: 如果当前公交到达时间大于等于Serval到达车站的时间,找出最短时间的那趟车,如果比Serval早到达,则算出该趟车次最早什么时候可以乘坐并用结构体存储该趟车的路线和乘坐时间,最后将这个数组排序,找出最小的乘车路线。最后将两次找出的最短路线进行时间比较,输出最小时间的乘车路线。

#include  
#include 
#define inf 1e9 + 7
using namespace std;
struct node {
	int id, num; // 存储乘车路线和上车时间 
}g[120];
bool cmp(node p, node q) { // 将乘车时间有小到大排序 
	if (p.num == q.num) return p.id < q.id;
	return p.num < q.num;
}
int main() {
	int n, t, s, d, ans = 110, tt = inf, cnt = 0;
	for (int i = 0; i < 120; i++) { // 初始化结构体数组 
		g[i].id = inf;
		g[i].num = inf;
	}
	scanf("%d %d", &n, &t);
	for (int i = 1; i <= n; i++) {
		scanf("%d %d", &s, &d);
		if (s >= t && s < tt) { // 如果当前车次到达时间大于等于Serval到达车站的时间,找出Serval最早的上车时间和乘车路线 
			ans = i; // 乘车路线 
			tt = s; // 乘车时间 
		}
		if (s < t) { // 如果当前车次到达时间小于Serval到达车站的时间,算出Serval最早的上车时间和乘车路线 
			if ((t - s) % d == 0) g[cnt].num = t; // 如果等该趟车的时间为d的倍数,乘车时间为t,否则为s + ((t - s) / d + 1) * d 
			else g[cnt].num = s + ((t - s) / d + 1) * d;
			g[cnt].id = i; // 乘车路线 
			cnt++;
		}
	}
	sort(g, g + cnt, cmp);
	if (tt < g[0].num) printf("%d", ans); // 找出这两种情况的最早乘车时间,输出最佳答案 
	else printf("%d", g[0].id);
	return 0;
}

你可能感兴趣的:(codeforces,思维)