[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 ...
随机推荐
- 数列分段Section II(二分)
洛谷传送门 输入时处理出最小的答案和最大的答案,然后二分答案即可. 其余细节看代码 #include <iostream> #include <cstdio> using na ...
- POJ3041:Asteroids【二分图匹配】
二分图的最大匹配=最小顶点覆盖(Konig定理)=最大独立集的补集最大匹配经典的三种模型 这题就是最小顶点覆盖,顺便这题留给我的经验就是调试的时候一定要细心细心再细心对模板的各个细节都要熟!! #i ...
- BZOJ2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛
n<=50000个点的树,求选最多不相邻点的个数. f[i][0]=sigma max(f[j][0],f[j][1]),j为i的儿子 f[i][1]=sigma f[j][0],j同上 死于未 ...
- 字符串常量与const常量内存区(——选自陈皓的博客)
1. 一个常见的考点: char* p = "test"; 那么理利用指针p来改变字符串test的内容都是错误的非法的. 例如: p[0] = 's'; strcpy(p, &qu ...
- Es首页
https://www.elastic.co/guide/en/elasticsearch/reference/index.html
- 通过socket过去本地ip,port和远端ip,port
struct sockaddr addr;struct sockaddr_in* addr_v4;int addr_len = sizeof(addr); //获取local ip and portZ ...
- spring mvc get请求也可以接受DTO对象
spring mvc get请求也可以接受DTO对象,比如:url上面你还是将参数&符号连接起来,并自动封装进一个DTO对象里. 只有@RequestBody注解spring mvc才会从ht ...
- Meteor Blaze
Blaze是Meteor 软件包用于构建现场反应模板. Render方法 这种方法被用于绘制模板到DOM.首先,我们将创建 myNewTemplate 之后渲染. 我们增加 myContainer 这 ...
- Linux下的lds链接脚本简介(一)
转载自:http://linux.chinaunix.net/techdoc/beginner/2009/08/12/1129972.shtml 一. 概论 每一个链接过程都由链接脚本(linker ...
- linux设备驱动归纳总结
前言: (总结已经基本写完,这段时间我会从新排版和修正.错误总会有的,望能指正!) 前段时间学习了嵌入式驱动,趁着没开始找工作,这段时间我会每天抽出时间来复习. 我的总结是根据学习时的笔记(李杨老师授 ...