1. 程式人生 > 其它 >【P2338 [USACO14JAN]Bessie Slows Down S】題解

【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<bits/stdc++.h>
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<dr[j])
		{
			s=nows; 
			t=tme[i]; 
			++i; 
		}
		else
		{
			t=t+(dr[j]-s)/nowv;
			s=dr[j]; 
			++j; 
		}
		++v; 
	}
	t=t+(double(1000)-s)/(double(1)/v); 
	printf("%.lf", t); 
	return 0; 
}