4509: [Usaco2016 Jan]Angry Cows
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 83 Solved: 38
Description
Bessie the cow has designed what she thinks will be the next big hit video game: "Angry Cows". The premise, which she believes is completely original, is that the player shoots a cow with a slingshot
into a one-dimensional scene consisting of a set of hay bales located at various points on a number line; the cow lands with sufficient force to detonate the hay bales in close proximity to her landing site, which in turn might set of a chain reaction that
causes additional hay bales to explode. The goal is to use a single cow to start a chain reaction that detonates all the hay bales.
There are NN hay bales located at distinct integer positions x1,x2,…,xNx1,x2,…,xN on the number line. If a cow is launched with power RR landing at position xx, this will causes a blast of "radius
RR", engulfing all hay bales within the range x?R…x+Rx?R…x+R. These hay bales then themselves explode (all simultaneously), each with a blast radius of R?1R?1. Any not-yet-exploded bales caught in these blasts then all explode (all simultaneously) with blast
radius R?2R?2, and so on.
Please determine the minimum amount of power RR with which a single cow may be launched so that, if it lands at an appropriate location, it will cause subsequent detonation of every single hay bale
in the scene.
Input
The first line of input contains NN (2≤N≤50,000). The remaining NN lines all contain integers x1…xN (each in the range 0…1,000,000,000).
Output
Please output the minimum power RR with which a cow must be launched in order to detonate all the hay bales. Answers should be rounded and printed to exactly 1 decimal point.
Sample Input
5
8
10
3
11
1
Sample Output
3.0
In this example, a cow launched with power 3 at, say, location 5, will cause immediate detonation of hay bales at positions 3 and 8. These then explode (simultaneously) each with blast radius 2, engulfing bales at positions 1 and 10, which next explode (simultaneously)
with blast radius 1, engulfing the final bale at position 11, which finally explodes with blast radius 0.
HINT
Source
DP= =
f[i]表示要炸掉i之前的所有炸药包,在i点最少需要的半径。
g[i]表示要炸掉i之后的所有炸药包,在i点最少需要的半径。
找到j<i且a[i]-a[j]>f[j]+1的最后一个j,f[i]=min(a[i]-a[j],f[j+1]+1),g数组同理。
最后枚举起始的攻击区间,计算答案。
有一个小技巧,答案的小数部分只可能是0或者0.5,所以将所有数乘以2就可以避免小数问题,最后答案再除以2就可以了。
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> #define F(i,j,n) for(int i=j;i<=n;i++) #define D(i,j,n) for(int i=j;i>=n;i--) #define ll long long #define N 50005 #define inf 2000000000 using namespace std; int n,a[N],f[N],g[N]; 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*10+ch-'0';ch=getchar();} return x*f; } int main() { n=read(); F(i,1,n) a[i]=read()*2; sort(a+1,a+n+1); F(i,1,n) f[i]=g[i]=inf; int t=1;f[1]=0; F(i,2,n) { while (t+1<i&&a[i]-a[t+1]>f[t+1]+2) t++; f[i]=min(a[i]-a[t],f[t+1]+2); } t=n;g[n]=0; D(i,n-1,1) { while (t-1>i&&a[t-1]-a[i]>g[t-1]+2) t--; g[i]=min(a[t]-a[i],g[t-1]+2); } int ans=inf; for(int i=1,j=n;i<j;) { ans=min(ans,max((a[j]-a[i])/2,max(f[i],g[j])+2)); if (f[i+1]<g[j-1]) i++;else j--; } printf("%.1lf\n",(double)ans/2); return 0; }