1153A A. Serval and Bus

在雨天,Serval首次前往幼儿园,面对多种公交路线,本算法探讨如何选择最快到达的公交线路。通过分析每条路线的首班车时间和发车间隔,算法能够快速决定最优出行方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 <iostream> 
#include <algorithm>
#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;
}
B. Serval and Final MEX time limit per test1 second memory limit per test256 megabytes You are given an array a consisting of n≥4 non-negative integers. You need to perform the following operation on a until its length becomes 1 : Select two indices l and r (1≤l<r≤|a| ), and replace the subarray [al,al+1,…,ar] with a single integer mex([al,al+1,…,ar]) , where mex(b) denotes the minimum excluded (MEX)∗ of the integers in b . In other words, let x=mex([al,al+1,…,ar]) , the array a will become [a1,a2,…,al−1,x,ar+1,ar+2,…,a|a|] . Note that the length of a decreases by (r−l) after this operation. Serval wants the final element in a to be 0 . Help him! More formally, you have to find a sequence of operations, such that after performing these operations in order, the length of a becomes 1 , and the final element in a is 0 . It can be shown that at least one valid operation sequence exists under the constraints of the problem, and the length of any valid operation sequence does not exceed n . Note that you do not need to minimize the number of operations. ∗ The minimum excluded (MEX) of a collection of integers b1,b2,…,bk is defined as the smallest non-negative integer x which does not occur in the collection b . Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000 ). The description of the test cases follows. The first line of each test case contains a single integer n (4≤n≤5000 ) — the length of the array a . The second line contains n integers a1,a2,…,an (0≤ai≤n ) — the elements of the array a . It is guaranteed that the sum of n over all test cases does not exceed 5000 . Output For each test case, output a single integer k (0≤k≤n ) in the first line of output — the length of the operation sequence. Then, output k lines, the i -th line containing two integers li and ri (1≤li<ri≤|a| ) — the two indices you choose in the i -th operation, where |a| denotes the length of the array be
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值