跳到正文

题解

题解:P15639 [ICPC 2022 Tehran R] Flower Festival

s12316约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年3月28日
更新
2026年3月28日
篇幅
约 1 分钟
题目
P15639

版本

  1. v1当前

思路:


比较简单的一道结构体排序题,直接模拟即可。

我们使用一个结构体,内部维护一个汽车的观测位置,速度,编号和到达终点所用的时间。

我们要输出到达终点所用的时间最短的汽车编号,所以要对结构体按时间从小到大排序。

到达终点所用时间计算公式

(f−x)/v(f-x)/v

小提示

到达终点所用时间要用 double 计算,不然会造成精度问题。

正解:


C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=105;
int n,f;
struct car{
	int x,v;
	int id;
	double t;
}a[N];
bool cmp(car a,car b){
	return a.t<b.t;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>f;
	for(int i=1;i<=n;i++){
		cin>>a[i].x>>a[i].v;
		a[i].id=i;
		a[i].t=(double)(f-a[i].x)/a[i].v;
	}
	sort(a+1,a+n+1,cmp);
	cout<<a[1].id;
	return 0;
}
原文