usaco16 jan gold
Posted beiju-z
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了usaco16 jan gold相关的知识,希望对你有一定的参考价值。
T1:angry cows
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 x_1, x_2, ldots, x_Nx1?,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 ldo s x+Rx?Rldosx+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 leq N leq 50,0002≤N≤50,000 ). The remaining
NN lines all contain integers x_1 ldots x_Nx1?…xN? (each in the range
0 ldots 1,000,000,0000…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.
题目是说把一头牛砸到某个位置会发生爆炸,爆炸半径是砸牛所用的能量R,每个干草堆如果被引爆会继续产生半径R-1(递减)的爆炸,问怎么扔可以把所有草堆引爆并且R最小。
令dp1[i]表示i左边全部被引爆的最小半径,有:
即表示最远直接引爆到j的最小半径。
然后发现dp1和j是单调递增的,因此O(n)O(n)。
dp2对称。
然后再枚举投掷点即可。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define rep(i,j,k) for(i=j;i<k;++i) const int N = 50005; int dp1[N], dp2[N], a[N]; int main() { int n, i, j, ans = 0x3f3f3f3f; scanf("%d", &n); rep(i,0,n) scanf("%d", a + i), a[i] *= 2; sort(a, a + n); n = unique(a, a + n) - a; memset(dp1, 127, sizeof dp1); dp1[j = 0] = -2; rep(i,1,n) { while (j + 1 < i && a[i] - a[j + 1] > dp1[j + 1] + 2) ++j; dp1[i] = min(a[i] - a[j], dp1[j + 1] + 2); } memset(dp2, 127, sizeof dp2); dp2[j = n - 1] = -2; for (i = n - 2; i >= 0; --i) { while (j - 1 > i && a[j - 1] - a[i] > dp2[j - 1] + 2) --j; dp2[i] = min(a[j] - a[i], dp2[j - 1] + 2); } for (i = 0, j = n - 1; i < j; ) { ans = min(ans, max((a[j] - a[i]) / 2, 2 + max(dp1[i], dp2[j]))); if (dp1[i + 1] < dp2[j - 1]) ++i; else --j; } printf("%.1f", 1.0 * ans / 2); return 0; }
by:http://blog.csdn.net/huanghongxun/ https://blog.csdn.net/huanghongxun/article/details/51193632
T2:
Farmer John has lost his favorite cow bell, and Bessie the cow has agreed to help him find it! They both fan out and search the farm along different paths, but stay in contact via radio so they can keep in touch with each-other. Unfortunately, the batteries in their radios are running low, so they want to plan their movements so as to conserve power, by trying to stay always within a short distance apart.
Farmer John starts at location ( f_x, f_yfx?,fy? ) and plans to follow a path consisting of NN steps, each of which is either ‘N‘ (north), ‘E‘ (east), ‘S‘ (south), or ‘W‘ west. Bessie starts at location ( b_x, b_ybx?,by? ) and follows a similar path consisting of MM steps. Both paths may share points in common. At each time step, Farmer John can either stay put at his current location, or take one step forward along his path, in whichever direction happens to be next (assuming he has not yet reached the final location in his path). Bessie can make a similar choice. At each time step (excluding the first step where they start at their initial locations), their radios consume energy equal to the square of the distance between them.
Please help FJ and Bessie plan a joint movement strategy that will minimize the total amount of energy consumed up to and including the final step where both of them first reach the final locations on their respective paths.
input:
The first line of input contains NN and MM ( 1 leq N, M leq 10001≤N,M≤1000 ). The
second line contains integers f_xfx? and f_yfy? , and the third line contains b_xbx?
and b_yby? ( 0 leq f_x, f_y, b_x, b_y leq 10000≤fx?,fy?,bx?,by?≤1000 ). The next line contains a
string of length NN describing FJ‘s path, and the final line contains a string
of length MM describing Bessie‘s path.
It is guranteed that Farmer John and Bessie‘s coordinates are always in the
range ( 0 leq x,y leq 10000≤x,y≤1000 ) throughout their journey. Note that East points in the positive x direction and North points in the positive y direction.
output:
Output a single integer specifying the minimum energy FJ and Bessie can use
during their travels.
FJ从位置(fx,fy)开始,并计划遵循由N步骤组成的路径,每个步骤都是“N”(北),“E”(东),“S”(南),或“W”(西)。Bessie从位置(bx,by)开始,并遵循由M步骤组成的类似路径。两个路径可以经过相同的点。在每个时间段,FJ可以保持在他现在的位置,或沿着他的道路前进一步,无论哪个方向恰好在下一个(假设他还没有到达他的路径的最后位置)。Bessie可以做出类似的选择。在每个时间步(不包括从初始位置开始的第一步),他们的无线电消耗的能量等于它们之间距离的平方。
dp[i][j]表示F走i步,J走j步的最小花费
然后进行状态转移就好了
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; const int maxn = 1005; int n, m; int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0}; char str[maxn]; LL dp[maxn][maxn]; struct _po { int x, y; } F[maxn], B[maxn]; inline int getid(char ch) { if(ch == ‘N‘) return 0; if(ch == ‘S‘) return 1; if(ch == ‘W‘) return 2; if(ch == ‘E‘) return 3; } inline LL dis(_po A, _po B) { return (LL)(A.x - B.x) * (A.x - B.x) + (LL)(A.y - B.y) * (A.y - B.y); } int main() { scanf("%d%d%d%d%d%d", &n, &m, &F[0].x, &F[0].y, &B[0].x, &B[0].y); scanf("%s", str + 1); for(int i = 1, op; str[i]; i++) { op = getid(str[i]); F[i] = (_po){F[i - 1].x + dx[op], F[i - 1].y + dy[op]}; } scanf("%s", str + 1); for(int i = 1, op; str[i]; i++) { op = getid(str[i]); B[i] = (_po){B[i - 1].x + dx[op], B[i - 1].y + dy[op]}; } memset(dp, 0x3f, sizeof(dp)); dp[0][0] = 0; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + dis(F[i + 1], B[j])); dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + dis(F[i], B[j + 1])); dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + dis(F[i + 1], B[j + 1])); } printf("%lld ", dp[n][m]); return 0; }
T3:暂时还没搞出来。
以上是关于usaco16 jan gold的主要内容,如果未能解决你的问题,请参考以下文章
POJ3659 [usaco2008jan_gold]电话网络
bzoj2581 [USACO 2012 Jan Gold] Cow RunAnd-Or Tree
洛谷 P3133 [USACO16JAN]无线电联系Radio Contact