NC24566. [USACO 2014 Jan B]Bessie Slows Down
描述
输入描述
Line 1: The value of N.
Lines 2..1+N: Each line is of the form "T x" or "D x", indicating a
time event or a distance event. In both cases, x is an
integer that is guaranteed to place the event before Bessie
reaches one kilometer of total distance. It is possible for
multiple events to occur simultaneously, causing Bessie to
slow down quite a bit all at once. Events may not be listed
in order.
输出描述
Line 1: The total time required for Bessie to travel 1 kilometer
示例1
输入:
2 T 30 D 10
输出:
2970
说明:
Bessie travels the first 10 meters at 1 meter/second, taking 10 seconds.Java(javac 1.8) 解法, 执行用时: 553ms, 内存消耗: 54548K, 提交时间: 2020-04-06 11:59:14
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner (System.in); while (in.hasNext()) { int n=in.nextInt(); double t[]=new double[111111]; double d[]=new double[111111]; int len1=0,len2=0; for (int i=1;i<=n;i++) { String m=in.next(); double a=in.nextDouble(); if (m.equals("T")) { t[++len1]=a; } else { d[++len2]=a; } } d[++len2]=0; d[++len2]=1000; Arrays.sort(t,1,len1+1); Arrays.sort(d,1,len2+1); double sum=0; int t1=1,t2=1; for (int i=1;i<len2;i++) { double jl=d[i]; while(jl<d[i+1] && t1<=len1 && sum+(d[i+1]-jl)*t2>t[t1] ) { jl+=(t[t1]-sum)/t2; t2++; sum=t[t1++]; } sum+=(d[i+1]-jl)*t2; t2++; } if (sum-(int)sum>0.5)sum=(int)sum+1; else sum=(int)sum; System.out.printf("%.0f\n",sum); } } }
C++14(g++5.4) 解法, 执行用时: 7ms, 内存消耗: 544K, 提交时间: 2020-04-05 15:52:57
#pragma GCC optimize(3,"Ofast") #include <bits/stdc++.h> using namespace std; template<class t> inline t read(t &x){ char c=getchar();bool f=0;x=0; while(!isdigit(c)) f|=c=='-',c=getchar(); while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar(); if(f) x=-x;return x; } template<class t> inline void write(t x){ if(x<0) putchar('-'),write(-x); else{if(x>9) write(x/10);putchar('0'+x%10);} } const int N=1e5+5; int x=1,a[N],b[N],nm,n,m; double y,z; signed main(){ read(nm); for(int i=1;i<=nm;i++){ char op; cin>>op; if(op=='T') read(a[++n]); else read(b[++m]); } sort(a+1,a+1+n); sort(b+1,b+1+m); for(int l=1,r=1,i=1;i<=nm;i++){ if((r>m||(b[r]-y)*x+z>=a[l])&&l<=n){ if((a[l]-z)/x+y>1000) break; y+=(a[l]-z)/x; z=a[l]; x++;l++; } else{ if(b[r]>1000) break; z+=(b[r]-y)*x; y=b[r]; x++;r++; } } write((int)round((1000-y)*x+z)); }
C++11(clang++ 3.9) 解法, 执行用时: 11ms, 内存消耗: 492K, 提交时间: 2020-04-04 20:11:09
#include<bits/stdc++.h> using namespace std; vector<int>a,b; int n,num1=0,num2=0; double ans=0,now=0; int main(){ cin>>n; for(int i=1;i<=n;i++){ char c;double x; cin>>c>>x; if(c=='T')a.push_back(x); else b.push_back(x); } sort(a.begin(),a.end()); sort(b.begin(),b.end()); a.push_back(1e8); b.push_back(1e8); for(int i=1;i<=n;i++){ double v=1.0/i; if(b[num2]<(a[num1]-ans)*v+now){ ans+=(b[num2]-now)/v; now=b[num2]; num2++; } else{ now+=(a[num1]-ans)*v; ans=a[num1]; num1++; } } ans+=(1000-now)*(n+1); printf("%.0lf",ans); return 0; }