[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 = ;
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的更多相关文章
- [USACO16JAN]愤怒的奶牛Angry Cows (单调队列优化dp)
题目链接 Solution 应该可以用二分拿部分分,时间 \(O(n^2logn)\) . 然后可以考虑 \(n^2\) \(dp\) ,令 \(f_i\) 代表 \(i\) 点被激活,然后激活 \( ...
- USACO2016 January Gold Angry Cows
Angry Cows 题目描述:给出数轴上的\(n\)个整点\((a[i])\),现在要在数轴上选一个位置\(x\)(可以是实数),以及一个半径\(R\),在以\(x\)为中心,半径为\(R\)的范围 ...
- P2868 [USACO07DEC]观光奶牛Sightseeing Cows
P2868 [USACO07DEC]观光奶牛Sightseeing Cows [](https://www.cnblogs.com/images/cnblogs_com/Tony-Double-Sky ...
- 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows
P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题目描述 Farmer John has decided to reward his cows for their har ...
- 洛谷 P3088 [USACO13NOV]挤奶牛Crowded Cows 题解
P3088 [USACO13NOV]挤奶牛Crowded Cows 题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazing alo ...
- 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 ...
- [USACO16JAN]Angry Cows G 解题报告
一图流 参考代码: #include<bits/stdc++.h> #define ll long long #define db double #define filein(a) fre ...
- 洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- [USACO07DEC]观光奶牛Sightseeing Cows 二分答案+判断负环
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
随机推荐
- rac的不完全恢复
模拟rac的不完全恢复,虽然小鱼对常规的完全和不完全恢复已经轻车熟路了,还是记录一个不完全恢复完整过程记录下来. 1 首先小鱼做了一个完全备份. RMAN> backup database in ...
- php中configure报错问题
https://blog.csdn.net/dodott/article/details/49664379 PHP的安装虽然有时候很简单,可是如果应用一多,我们安装起来就很头痛了!出错最多的就是安装P ...
- hihoCoder#1133 二分·二分查找之k小数
原题地址 经典问题了,O(n)时间内找第k大的数 代码: #include <iostream> using namespace std; int N, K; int *a; int se ...
- hdu4352 XHXJ's LIS(数位DP + LIS + 状态压缩)
#define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully reading the entire ...
- PHP建立和删除目录
<?php/*linux中的文件权限filedir 用户 组 其它 rwx rwx rwx 读写执行 6 4 6 读写 读 读写 7 7 7 rw_ r__ rw_ r__ _w_ ___ r ...
- 【Tomcat】如何优化tomcat配置(从内存、并发、缓存4个方面)优化
一.Tomcat内存优化 ** Tomcat内存优化主要是对 tomcat 启动参数优化,我们可以在 tomcat 的启动脚本 catalina.sh 中设置 java_OPTS 参数. JAVA_O ...
- Match the string--hdu1797(模拟)
http://acm.hdu.edu.cn/showproblem.php?pid=1797 就是模拟 我的思路是标记aba 和h的位置 然后就判断是否正确 就行了 还有就是 最后 fkfkfkf ...
- codevs——3064 求和
3064 求和 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 青铜 Bronze 题解 查看运行结果 题目描述 Description 输入一个数x(x <= ...
- [CERC2015]Digit Division
题目描述 We are given a sequence of n decimal digits. The sequence needs to be partitioned into one or m ...
- Java泛型的主要用途
1.泛型的主要用途就是代替各种类型,作为一个笼统的整体类型代替,也就是代替参数,不论是传入参数还是返回参数.都可以用泛型来代替. 如dao操作类的增删改查操作,因为传入参数的类型不同,但基本都是相同接 ...