1. 程式人生 > >usaco16 jan gold

usaco16 jan gold

long direct data tee truct origin () 下一個 equal

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?R\ldosx+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,0002N50,000 ). The remaining

NN lines all contain integers x_1 \ldots x_Nx1?xN? (each in the range

0 \ldots 1,000,000,00001,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左邊全部被引爆的最小半徑,有:

dp1i=min{ai?aj,dp1j+1+1}(ai?aj>dp1j+1,j<i)dp1i=min{ai?aj,dp1j+1+1}(ai?aj>dp1j+1,j<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 10001N,M1000 ). 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 10000fx?,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 10000x,y1000 ) 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\n", dp[n][m]);
    return 0;
} 

T3:暫時還沒搞出來。

usaco16 jan gold