[USACO16JAN]愤怒的奶牛Angry Cows

传送门

一道神奇的DP………(鬼知道他为什么在tarjan里面)

一开始可能会考虑贪心或者什么其他神奇的算法,不过还是DP比较靠谱。

我们用f[i]表示摧毁所有i左侧的炸 药包最少需要的能量,用g[i]表示摧毁所有i右侧的炸 药包最少需要的能量。

那么我们只要找到满足j < i,a[i] - a[j] > f[j]+1的最后一个j炸 药包,就可以更新f[i]的值,f[i]  = min(f[i],a[i]-a[j],f[j]+1);

同样的g也是同理。

为什么这么找呢……因为首先我们发现如果a[i]-a[j]比f[j]+1还要小的话,那么从i点引发的爆炸是可以波及到j点的,所以并不需要更新答案,直到不满足的时候我们才更新。

最后枚举爆炸的点就可以了。

然后这题有个技巧,如果往数轴上投炸 药你要么投在点上要么投在两者中间,也就是只可能有整数或者.5的情况。这样直接把所有数据×2计算最后/2就可以了。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar(‘\n‘)

using namespace std;
typedef long long ll;
const int M = 50005;
const int INF = 2000000009;

int read()
{
    int ans = 0,op = 1;
    char ch = getchar();
    while(ch < ‘0‘ || ch > ‘9‘)
    {
    if(ch == ‘-‘) op = -1;
    ch = getchar();
    }
    while(ch >= ‘0‘ && ch <= ‘9‘)
    {
    ans *= 10;
    ans += ch - ‘0‘;
    ch = getchar();
    }
    return ans * op;
}

int n,f[M],g[M],a[M],head,tail,ans = INF;
int main()
{
    n = read();
    rep(i,1,n) a[i] = read() << 1;
    sort(a+1,a+1+n);
    n = unique(a+1,a+1+n) - a - 1;
    rep(i,1,n) f[i] = g[i] = INF;
    f[1] = -2;
    rep(i,2,n)
    {
    while(head + 1 < i && a[i] - a[head+1] > f[head+1] + 2) head++;
    f[i] = min(f[head+1] + 2,a[i] - a[head]);
    }
    g[n] = -2,tail = n;
    per(i,n-1,1)
    {
    while(tail - 1 > i && a[tail-1] - a[i] > g[tail-1] + 2) tail--;
    g[i] = min(a[tail] - a[i],g[tail-1] + 2);
    }
    //rep(i,1,n) printf("%d %d\n",f[i],g[i]);
    head = 1,tail = n;
    while(head < tail)
    {
    ans = min(ans,max((a[tail] - a[head]) >> 1,2 + max(g[tail],f[head])));
    if(f[head+1] < g[tail-1]) head++;
    else tail--;
    }
    printf("%.1lf\n",(double)ans / 2.0);
    return 0;
}
 

原文地址:https://www.cnblogs.com/captain1/p/9680913.html

时间: 2024-10-16 23:04:21

[USACO16JAN]愤怒的奶牛Angry Cows的相关文章

bzoj4509【Usaco2016 Jan】Angry Cows

4509: [Usaco2016 Jan]Angry Cows Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 83  Solved: 38 [Submit][Status][Discuss] Description Bessie the cow has designed what she thinks will be the next big hit video game: "Angry Cows". The premise, whi

[USACO07DEC]观光奶牛Sightseeing Cows

[USACO07DEC]观光奶牛Sightseeing Cows 题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to spend their free time. Fortunately, they have a detailed city map showing

洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows

题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to spend their free time. Fortunately, they have a detailed city map showing the L (2 ≤ L ≤ 1000) major landma

洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows

题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to spend their free time. Fortunately, they have a detailed city map showing the L (2 ≤ L ≤ 1000) major landma

P3088 [USACO13NOV]挤奶牛Crowded Cows(单调队列)

题目描述 Farmer John has N cows that need to be milked (1 <= N <= 10,000), each of which takes only one unit of time to milk. Being impatient animals, some cows will refuse to be milked if Farmer John waits too long to milk them. More specifically, cow

bzoj4525: [Usaco2016 Jan]Angry Cows

二分. #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn = 50000 + 10; int n,k,l,r,mid,ans,d; int a[maxn]; bool check(int dist) { dist=2*dist; int d=0,sum=1; for(int i=2;i<=n;i++) { if(a[i]-a[i-1]

BZOJ4509 Angry Cows(dp)

题意: 大概就是一条线上有n个炸弹,然后让你随意扔一个爆炸半径为r的炸弹使他们全部爆炸, 第一次被引爆的炸弹爆炸半径为r-1,第二次为r-2... 求r最小是多少 思路: 用两个数组处理得到从左往右和从右往左到当前炸弹时的爆炸半径最小是多少,然后枚举投弹位置就可以了 /* *********************************************** Author :devil ************************************************ */

洛谷P1578 奶牛浴场

P1578 奶牛浴场 题目描述 由于John建造了牛场围栏,激起了奶牛的愤怒,奶牛的产奶量急剧减少.为了讨好奶牛,John决定在牛场中建造一个大型浴场.但是John的奶牛有一个奇怪的习惯,每头奶牛都必须在牛场中的一个固定的位置产奶,而奶牛显然不能在浴场中产奶,于是,John希望所建造的浴场不覆盖这些产奶点.这回,他又要求助于Clevow了.你还能帮助Clevow吗? John的牛场和规划的浴场都是矩形.浴场要完全位于牛场之内,并且浴场的轮廓要与牛场的轮廓平行或者重合.浴场不能覆盖任何产奶点,但是

vijos p1005 奶牛浴场[ 极大化思想]

奶牛浴场 描述 由于John建造了牛场围栏,激起了奶牛的愤怒,奶牛的产奶量急剧减少.为了讨好奶牛,John决定在牛场中建造一个大型浴场.但是John的奶牛有一个奇怪的习惯,每头奶牛都必须在牛场中的一个固定的位置产奶,而奶牛显然不能在浴场中产奶,于是,John希望所建造的浴场不覆盖这些产奶点.这回,他又要求助于Clevow了.你还能帮助Clevow吗? John的牛场和规划的浴场都是矩形.浴场要完全位于牛场之内,并且浴场的轮廓要与牛场的轮廓平行或者重合.浴场不能覆盖任何产奶点,但是产奶点可以位于浴