传送门

一道神奇的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 = ;
const int INF = ; int read()
{
int ans = ,op = ;
char ch = getchar();
while(ch < '' || ch > '')
{
if(ch == '-') op = -;
ch = getchar();
}
while(ch >= '' && ch <= '')
{
ans *= ;
ans += ch - '';
ch = getchar();
}
return ans * op;
} int n,f[M],g[M],a[M],head,tail,ans = INF;
int main()
{
n = read();
rep(i,,n) a[i] = read() << ;
sort(a+,a++n);
n = unique(a+,a++n) - a - ;
rep(i,,n) f[i] = g[i] = INF;
f[] = -;
rep(i,,n)
{
while(head + < i && a[i] - a[head+] > f[head+] + ) head++;
f[i] = min(f[head+] + ,a[i] - a[head]);
}
g[n] = -,tail = n;
per(i,n-,)
{
while(tail - > i && a[tail-] - a[i] > g[tail-] + ) tail--;
g[i] = min(a[tail] - a[i],g[tail-] + );
}
//rep(i,1,n) printf("%d %d\n",f[i],g[i]);
head = ,tail = n;
while(head < tail)
{
ans = min(ans,max((a[tail] - a[head]) >> , + max(g[tail],f[head])));
if(f[head+] < g[tail-]) head++;
else tail--;
}
printf("%.1lf\n",(double)ans / 2.0);
return ;
}

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

  1. [USACO16JAN]愤怒的奶牛Angry Cows (单调队列优化dp)

    题目链接 Solution 应该可以用二分拿部分分,时间 \(O(n^2logn)\) . 然后可以考虑 \(n^2\) \(dp\) ,令 \(f_i\) 代表 \(i\) 点被激活,然后激活 \( ...

  2. USACO2016 January Gold Angry Cows

    Angry Cows 题目描述:给出数轴上的\(n\)个整点\((a[i])\),现在要在数轴上选一个位置\(x\)(可以是实数),以及一个半径\(R\),在以\(x\)为中心,半径为\(R\)的范围 ...

  3. P2868 [USACO07DEC]观光奶牛Sightseeing Cows

    P2868 [USACO07DEC]观光奶牛Sightseeing Cows [](https://www.cnblogs.com/images/cnblogs_com/Tony-Double-Sky ...

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

    P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题目描述 Farmer John has decided to reward his cows for their har ...

  5. 洛谷 P3088 [USACO13NOV]挤奶牛Crowded Cows 题解

    P3088 [USACO13NOV]挤奶牛Crowded Cows 题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazing alo ...

  6. NC24017 [USACO 2016 Jan S]Angry Cows

    NC24017 [USACO 2016 Jan S]Angry Cows 题目 题目描述 Bessie the cow has designed what she thinks will be the ...

  7. [USACO16JAN]Angry Cows G 解题报告

    一图流 参考代码: #include<bits/stdc++.h> #define ll long long #define db double #define filein(a) fre ...

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

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

  9. [USACO07DEC]观光奶牛Sightseeing Cows 二分答案+判断负环

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

随机推荐

  1. 局部二值模式(Local Binary Patterns)纹理灰度与旋转不变性

    Multiresolution Gray Scale and Rotation Invariant Texture Classification with Local Binary Patterns, ...

  2. HDU 3062 简单的2-SAT问题

    在2-SAT,最让我纠结的还是添加有向线段的函数了 void add_clause(int i,int a,int j,int b){    int m=2*i+a;    int n=2*j+b;  ...

  3. 自定义PHP错误报告处理方式

    <?php //在php中注册一个函数, 来处理错误报告, 而不按原来的方式处理了 set_error_handler("myerrorfun"); $mess = &quo ...

  4. Linux怎么读? Linux读音考古一日游

    Linux怎么读?  Linux读音考古一日游/*凡是准备踏入Linux大门的叉子们(N年不关注了,不知道这个称呼是否还有),都必须经历疑问 那就是linux到底怎么读? 也许有些人很容易 什么里纽克 ...

  5. Back弹出AlertDialog

    package com.pingyijinren.helloworld.activity; import android.content.DialogInterface; import android ...

  6. HDU1533 最小费用最大流

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. POJ 2484 A Funny Game【博弈】

    相比数据结构的题..感觉这种想啊想的题可爱多了~~~代码量还少.... 题目链接: http://poj.org/problem?id=2484 题意: 一圈n个硬币,两人轮流从中取一或两个硬币,(只 ...

  8. 【Nginx】惊群问题

    转自:江南烟雨 惊群问题的产生 在建立连接的时候,Nginx处于充分发挥多核CPU架构性能的考虑,使用了多个worker子进程监听相同端口的设计,这样多个子进程在accept建立新连接时会有争抢,这会 ...

  9. nyoj473 A^B Problem (高速幂)

    题目473 题目信息 执行结果 pid=473" style="text-decoration:none; color:rgb(55,119,188)">本题排行 ...

  10. wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合

    wpf 导出Excel   1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 4 ExportDataGrid ...