gym 100543 CERC 2014 L Outer space invaders

来源:互联网 发布:尚硅谷大数据视频 编辑:程序博客网 时间:2024/06/11 22:18

Problem

Central Europe Regional Contest 2014
vjudge.net/problem/Gym-100543L

Meaning

有 n 个敌人,第 i 个将在 ai 时间出现、bi 时间开枪、距离为 di
你有一种炸弹,可随意设置爆炸范围 R,引爆后在 R 以内的、已经出现的敌人会被炸死,消耗 R 格燃料。
要在任何一个敌人出现后、开枪前([ ai , bi ])消灭他,问最小消耗。

Analysis

当时的错误想法:先按(ai , bi)字典序排序,然后dp:
dp[i]:及时搞掂前 i 个敌人的最小消耗
对于最后一个敌人 i,可能跟着最后的若干的敌人一起被炸,于是转移:
dp[i] = min { dp[j] + cost( j+1 , i ) | 0<=j<i }
其中 cost( j+1 , i )是从第 j+1 个到第 i 个敌人里最远的距离(因为是一起炸),且要满足条件:min { bj+1 , … , bi1 } ai,这样才能等在一起炸。
但这样是错的,排序有毒,有考虑不到的情况,如反例:

131 4 102 2 13 4 11

这样是要 1、3 一起炸,2 单独炸。
官方题解是把敌人理解成一条条从(ai , di)到(bi , di)的水平线段,炸弹就是从(xi , 0)到(xi , R)的竖直线段,横线被竖穿过就是敌人被炸弹炸到。
于是把所有时刻(a 和 b)离散化后,用区间dp
dp[s][e]:与所有完全包含在时间段 [ s , e ] 内的所有横线的最小竖线长度和
(暂时没完全理解 :( …)

Accepted Code

#include <cstdio>#include <algorithm>using namespace std;const int N = 300, D = 10000, BIG = N * D;struct node{    int a, b, d;} al[N];int ts[N<<1|1], dp[N+1<<1][N+1<<1] = {{0}};int main(){    int T;    scanf("%d", &T);    while(T--)    {        int n;        scanf("%d", &n);        int m = 0;        for(int i = 0; i < n; ++i)        {            scanf("%d%d%d", &al[i].a, &al[i].b, &al[i].d);            ts[++m] = al[i].a;            ts[++m] = al[i].b;        }        sort(ts + 1, ts + m + 1);        m = unique(ts + 1, ts + m + 1) - ts - 1;        for(int i = 0; i < n; ++i)        {            al[i].a = lower_bound(ts + 1, ts + m + 1, al[i].a) - ts;            al[i].b = lower_bound(ts + 1, ts + m + 1, al[i].b) - ts;        }        for(int w = 0; w < m; ++w)            for(int s = 1; s + w <= m; ++s)        {            int e = s + w, id = -1;            for(int i = 0; i < n; ++i)                if(s <= al[i].a && al[i].b <= e && (id == -1 || al[i].d > al[id].d))                    id = i;            if(id == -1)            {                dp[s][e] = 0;                continue;            }            dp[s][e] = BIG;            for(int k = al[id].a; k <= al[id].b; ++k)                dp[s][e] = min(dp[s][e], dp[s][k-1] + al[id].d + dp[k+1][e]);        }        printf("%d\n", dp[1][m]);    }    return 0;}

Wrong DP

#include <cstdio>#include <algorithm>using namespace std;const int N = 300;struct node{    int a, b, d;    bool operator < (const node &rhs) const    {        return a == rhs.a ? b < rhs.b : a < rhs.a;    }} arr[N+10];int dp[N+10];int main(){    int T;    scanf("%d", &T);    while(T--)    {        int n;        scanf("%d", &n);        for(int i=1; i<=n; ++i)            scanf("%d%d%d", &arr[i].a, &arr[i].b, &arr[i].d);        sort(arr + 1, arr + n + 1);        dp[0] = 0;        for(int i=1; i<=n; ++i)        {            dp[i] = dp[i-1] + arr[i].d;            int r = arr[i].d;            for(int j=i-1; j; --j)            {                if(arr[j].b < arr[i].a)                    break;                r = max(r, arr[j].d);                dp[i] = min(dp[i], dp[j-1] + r);            }        }        printf("%d\n", dp[n]);    }    return 0;}