[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=5142

[算法]

首先用RMQ预处理S数组的最大值

然后我们枚举右端点 , 通过二分求出合法的 , 最靠右的左端点 , 用这段区间的最大值更新答案 , 即可

时间复杂度 : O(NlogN)

[代码]

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const int MAXN = 1e5 + ;
  5. const int MAXLOG = ;
  6. const LL INF = 1e18;
  7.  
  8. int n;
  9. LL m;
  10. LL value[MAXN][MAXLOG];
  11. LL F[MAXN] , S[MAXN];
  12.  
  13. template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
  14. template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
  15. template <typename T> inline void read(T &x)
  16. {
  17. T f = ; x = ;
  18. char c = getchar();
  19. for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
  20. for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
  21. x *= f;
  22. }
  23. inline LL query(int l,int r)
  24. {
  25. int k = (int)((double)log(r - l + ) / log(2.0));
  26. return max(value[l][k] , value[r - ( << k) + ][k]);
  27. }
  28.  
  29. int main()
  30. {
  31.  
  32. read(n); read(m);
  33. for (int i = ; i <= n; i++)
  34. {
  35. read(F[i]);
  36. read(S[i]);
  37. value[i][] = S[i];
  38. }
  39. for (int i = ; i <= n; i++) F[i] += F[i - ];
  40. for (int i = ; i < MAXLOG; i++)
  41. {
  42. for (int j = ; j + ( << i) - <= n; j++)
  43. {
  44. value[j][i] = max(value[j][i - ],value[j + ( << (i - ))][i - ]);
  45. }
  46. }
  47. LL ans = INF;
  48. for (int i = ; i <= n; i++)
  49. {
  50. int l = , r = i , pos = -;
  51. while (l <= r)
  52. {
  53. int mid = (l + r) >> ;
  54. if (F[i] - F[mid - ] >= m)
  55. {
  56. pos = mid;
  57. l = mid + ;
  58. } else r = mid - ;
  59. }
  60. if (pos == -) continue;
  61. chkmin(ans,query(pos,i));
  62. }
  63. printf("%lld\n",ans);
  64.  
  65. return ;
  66.  
  67. }

[USACO 2017DEC] Haybale Feast的更多相关文章

  1. BZOJ5142: [Usaco2017 Dec]Haybale Feast(双指针&set)(可线段树优化)

    5142: [Usaco2017 Dec]Haybale Feast Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 182  Solved: 131[ ...

  2. BZOJ5142: [Usaco2017 Dec]Haybale Feast 线段树或二分答案

    Description Farmer John is preparing a delicious meal for his cows! In his barn, he has NN haybales ...

  3. [USACO 08JAN]Haybale Guessing

    Description The cows, who always have an inferiority complex about their intelligence, have a new gu ...

  4. [USACO 2017DEC] Barn Painting

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5141 [算法] 树形DP 时间复杂度 : O(N) [代码] #include< ...

  5. [USACO 2017DEC] Greedy Gift Takers

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5139 [算法] 二分答案 时间复杂度 : O(NlogN^2) [代码] #incl ...

  6. P4085 [USACO17DEC]Haybale Feast

    我又开始水了,感觉又是一道虚假的蓝题 题意 非常好理解,自己看吧 题解 可以比较轻易的发现,如果对于一段满足和大于等于 \(m\) 的区间和其满足和大于等于 \(m\) 的子区间来说,选择子区间肯定是 ...

  7. Luogu4085 [USACO17DEC]Haybale Feast (线段树,单调队列)

    \(10^18\)是要long long的. \(nlogn\)单调队列上维护\(logn\)线段树. #include <iostream> #include <cstdio> ...

  8. USACO 2015 December Contest, Gold Problem 2. Fruit Feast

    Problem 2. Fruit Feast 很简单的智商题(因为碰巧脑出来了所以简单一,一 原题: Bessie has broken into Farmer John's house again! ...

  9. USACO . Your Ride Is Here

    Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...

随机推荐

  1. json拼接含字符串变量的问题

    json键值对,当值为字符串变量时,极易搞错,拼接务必注意.String str="文字信息";String json="{\"msg\":\&quo ...

  2. Ubuntu安装sublime Text 3并配置可以输入中文

    使用Ubuntu系统后,想找一个顺手的编辑器,sublime作为我的首选编辑器,在安装和配置可输入中文时遇到各种个样的问题,总结一些: 1:问题: 我的系统是Ubuntu 18.04 LTS,尝试多次 ...

  3. Washing Clothes(poj 3211)

    大体题意:有n件衣服,m种颜色,某人和他的女炮一起洗衣服,必须一种颜色洗完,才能洗另一种颜色,每件衣服都有时间,那个人洗都一样,问最少用时. poj万恶的C++和G++,害得我CE了三次 /* 背包啊 ...

  4. Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings [dp 前缀和 ]

    传送门 D. A and B and Interesting Substrings time limit per test 2 seconds memory limit per test 256 me ...

  5. 【转】php 之 array_filter、array_walk、array_map的区别

    [转]php 之 array_filter.array_walk.array_map的区别 原文:https://blog.csdn.net/csdnzhangyiwei/article/detail ...

  6. c/s程序版本自动升级的问题,如何判断client端版本号是否最新,然后从指定ftp服务器down

    c/s程序版本自动升级的问题,如何判断client端版本号是否最新,然后从指定ftp服务器down http://blog.csdn.net/delphizhou/article/details/30 ...

  7. 基于Hexo + Git + Nginx的博客发布

    进过上一篇<树莓派搭建私人服务器>,我们已经有一个私人服务器了,现在需要做点什么实际事情了,先搭一个博客分享自己的经验吧. 相关文章:1.<树莓派搭建私人服务器>(http:/ ...

  8. CentOS7 设置系统时间

    在CentOS 6版本,时间设置有date.hwclock命令, 硬件时钟和系统时钟 (1) 硬件时钟 RTC(Real-Time Clock)或CMOS时钟,一般在主板上靠电池供电,服务器断电后也会 ...

  9. Deepin-安装git

    sudo apt-get install git 命令介绍(安装软件):apt-get install 命令介绍(Debian系列以管理员运行的前缀):sudo

  10. Guice 学习(八)AOP (面向切面的编程)

    Guice的AOP还是非常弱的.眼下只支持方法级别上的,另外灵活性也不是非常高. 看例如以下演示样例: Guice支持AOP的条件是: 类必须是public或者package (default) 类不 ...