【P2338 [USACO14JAN]Bessie Slows Down S】题解


题目链接

纯模拟题,无任何算法或思维难度。

难度虚高了。

对于时间和空间分别排个序,然后依次进行就行了。

看一下是先遇到减速地点还是减速时间。

要注意精度问题。

时间复杂度:\(O(n)\)

Code

// Problem: P2338 [USACO14JAN]Bessie Slows Down S
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2338
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include
using namespace std;
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
#define N 10010
int n, m, i, j, k; 
double s, v=1, t, nowv, nows; 
double tme[N], dr[N]; 
int tn, dn; 
char c[5]; 

signed main()
{
//	freopen("tiaoshi.in", "r", stdin); 
//	freopen("tiaoshi.out", "w", stdout); 
	n=read(); 
	for(i=1; i<=n; ++i)
	{
		scanf("%s", c+1); 
		if(c[1]=='T') tme[++tn]=double(read()); 
		else dr[++dn]=double(read()); 
	}
	sort(tme+1, tme+tn+1); 
	sort(dr+1, dr+dn+1); 
	tme[tn+1]=0x3f3f3f3f; 
	dr[dn+1]=0x3f3f3f3f; 
	i=j=1; 
	while(i<=tn||j<=dn)
	{
		nowv=double(1)/v; 
		nows=s+(tme[i]-t)*nowv; 
		if(nows