传送门

一道神奇的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就可以了。

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<cmath>
  6. #include<queue>
  7. #include<set>
  8. #include<map>
  9. #define rep(i,a,n) for(int i = a;i <= n;i++)
  10. #define per(i,n,a) for(int i = n;i >= a;i--)
  11. #define enter putchar('\n')
  12.  
  13. using namespace std;
  14. typedef long long ll;
  15. const int M = ;
  16. const int INF = ;
  17.  
  18. int read()
  19. {
  20. int ans = ,op = ;
  21. char ch = getchar();
  22. while(ch < '' || ch > '')
  23. {
  24. if(ch == '-') op = -;
  25. ch = getchar();
  26. }
  27. while(ch >= '' && ch <= '')
  28. {
  29. ans *= ;
  30. ans += ch - '';
  31. ch = getchar();
  32. }
  33. return ans * op;
  34. }
  35.  
  36. int n,f[M],g[M],a[M],head,tail,ans = INF;
  37. int main()
  38. {
  39. n = read();
  40. rep(i,,n) a[i] = read() << ;
  41. sort(a+,a++n);
  42. n = unique(a+,a++n) - a - ;
  43. rep(i,,n) f[i] = g[i] = INF;
  44. f[] = -;
  45. rep(i,,n)
  46. {
  47. while(head + < i && a[i] - a[head+] > f[head+] + ) head++;
  48. f[i] = min(f[head+] + ,a[i] - a[head]);
  49. }
  50. g[n] = -,tail = n;
  51. per(i,n-,)
  52. {
  53. while(tail - > i && a[tail-] - a[i] > g[tail-] + ) tail--;
  54. g[i] = min(a[tail] - a[i],g[tail-] + );
  55. }
  56. //rep(i,1,n) printf("%d %d\n",f[i],g[i]);
  57. head = ,tail = n;
  58. while(head < tail)
  59. {
  60. ans = min(ans,max((a[tail] - a[head]) >> , + max(g[tail],f[head])));
  61. if(f[head+] < g[tail-]) head++;
  62. else tail--;
  63. }
  64. printf("%.1lf\n",(double)ans / 2.0);
  65. return ;
  66. }

[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. 数列分段Section II(二分)

    洛谷传送门 输入时处理出最小的答案和最大的答案,然后二分答案即可. 其余细节看代码 #include <iostream> #include <cstdio> using na ...

  2. POJ3041:Asteroids【二分图匹配】

    二分图的最大匹配=最小顶点覆盖(Konig定理)=最大独立集的补集最大匹配经典的三种模型  这题就是最小顶点覆盖,顺便这题留给我的经验就是调试的时候一定要细心细心再细心对模板的各个细节都要熟!! #i ...

  3. 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同上 死于未 ...

  4. 字符串常量与const常量内存区(——选自陈皓的博客)

    1. 一个常见的考点: char* p = "test"; 那么理利用指针p来改变字符串test的内容都是错误的非法的. 例如: p[0] = 's'; strcpy(p, &qu ...

  5. Es首页

    https://www.elastic.co/guide/en/elasticsearch/reference/index.html

  6. 通过socket过去本地ip,port和远端ip,port

    struct sockaddr addr;struct sockaddr_in* addr_v4;int addr_len = sizeof(addr); //获取local ip and portZ ...

  7. spring mvc get请求也可以接受DTO对象

    spring mvc get请求也可以接受DTO对象,比如:url上面你还是将参数&符号连接起来,并自动封装进一个DTO对象里. 只有@RequestBody注解spring mvc才会从ht ...

  8. Meteor Blaze

    Blaze是Meteor 软件包用于构建现场反应模板. Render方法 这种方法被用于绘制模板到DOM.首先,我们将创建 myNewTemplate 之后渲染. 我们增加 myContainer 这 ...

  9. Linux下的lds链接脚本简介(一)

    转载自:http://linux.chinaunix.net/techdoc/beginner/2009/08/12/1129972.shtml 一. 概论 每一个链接过程都由链接脚本(linker ...

  10. linux设备驱动归纳总结

    前言: (总结已经基本写完,这段时间我会从新排版和修正.错误总会有的,望能指正!) 前段时间学习了嵌入式驱动,趁着没开始找工作,这段时间我会每天抽出时间来复习. 我的总结是根据学习时的笔记(李杨老师授 ...