P15639 [ICPC 2022 Tehran R] Flower Festival
思路
用一个结构体 存下汽车编号 和剩余行驶时间 ,其中 。
然后对 进行从小到大排序,输出最小 对应的 即可。
速度公式
速度公式 变形得到 ,而剩余行驶路程 。
自定义排序函数
bool cmp(car x, car y) {
return x.t < y.t; // 对于两辆车 x, y 的 t 排序
}引用方法:
sort(a + 1, a + n + 1, cmp); // 对数组 a 依照 cmp 排序代码实现
AC 代码
#include <bits/stdc++.h>
using namespace std;
int n, f;
struct car { // 结构体
int id;
double t;
} a[102];
bool cmp(car x, car y) { // 自定义排序函数
return x.t < y.t;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> f;
for (int i = 1; i <= n; i++) {
int x, v;
cin >> x >> v;
a[i].t = (f - x) * 1.0 / v, a[i].id = i; // 计算剩余时间
}
sort(a + 1, a + n + 1, cmp); // 排序
cout << a[1].id;
return 0;
}