每日一题 day48 打卡

Analysis

二分答案,判断序列的平均值是否大于等于mid

具体怎么实现呢?

将序列减去mid,再用前缀和来维护平均值就好了

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #define int long long
  6. #define maxn 100000+10
  7. #define INF 2147483647
  8. #define rep(i,s,e) for(register int i=s;i<=e;++i)
  9. #define dwn(i,s,e) for(register int i=s;i>=e;--i)
  10. using namespace std;
  11. inline int read()
  12. {
  13. int x=,f=;
  14. char c=getchar();
  15. while(c<''||c>'') {if(c=='-') f=-; c=getchar();}
  16. while(c>=''&&c<='') {x=x*+c-''; c=getchar();}
  17. return f*x;
  18. }
  19. inline void write(int x)
  20. {
  21. if(x<) {putchar('-'); x=-x;}
  22. if(x>) write(x/);
  23. putchar(x%+'');
  24. }
  25.  
  26. int n,L,l,r,ans;
  27. int a[maxn],sum[maxn],b[maxn];
  28. inline bool check(int x)
  29. {
  30. rep(i,,n) b[i]=a[i];
  31. rep(i,,n) b[i]-=x;
  32. rep(i,,n) sum[i]=sum[i-]+b[i];
  33. int min_val=INF;
  34. rep(i,L,n)
  35. {
  36. min_val=min(min_val,sum[i-L]);
  37. if(sum[i]-min_val>=) return true;
  38. }
  39. return false;
  40. }
  41. signed main()
  42. {
  43. n=read();L=read();
  44. rep(i,,n)
  45. {
  46. a[i]=read();
  47. a[i]*=;
  48. }
  49. l=,r=;
  50. while(l<r)
  51. {
  52. int mid=(l+r+)/;
  53. if(check(mid)==true)
  54. {
  55. ans=mid;
  56. l=mid;
  57. }
  58. else r=mid-;
  59. }
  60. if(ans%==) write(ans+);
  61. else write(ans);
  62. return ;
  63. }

请各位大佬斧正(反正我不认识斧正是什么意思)

LOJ P10012 Best Cow Fences 题解的更多相关文章

  1. loj#10012\poj2018 Best Cow Fences(二分)

    题目 #10012 「一本通 1.2 例 2」Best Cow Fences 解析 有序列\(\{a_i\}\),设\([l,r]\)上的平均值为\(\bar{x}\),有\(\sum_{i=l}^r ...

  2. POJ2018 Best Cow Fences —— 斜率优化DP

    题目链接:https://vjudge.net/problem/POJ-2018 Best Cow Fences Time Limit: 1000MS   Memory Limit: 30000K T ...

  3. POJ 2018 Best Cow Fences(二分+最大连续子段和)

    Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14601 Accepted: 4720 Desc ...

  4. POJ-2018 Best Cow Fences(二分加DP)

    Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10174 Accepted: 3294 Desc ...

  5. 一本通 1434:【例题2】Best Cow Fences

    Best Cow Fences 二分答案 + 前缀和 个人认为题意没有表述清楚,本题要求的是满足题意的连续子序列(难度大大降低了有木有). 本题的精度也是非常令人陶醉,请您自行体会吧! #includ ...

  6. 1434:【例题2】Best Cow Fences

    1434:[例题2]Best Cow Fences 时间限制: 1000 ms         内存限制: 65536 KB提交数: 263     通过数: 146 [题目描述] 给定一个长度为n的 ...

  7. Poj 2018 Best Cow Fences(分数规划+DP&&斜率优化)

    Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Description Farmer John's farm consists of a ...

  8. POJ 2018 Best Cow Fences(二分最大区间平均数)题解

    题意:给出长度>=f的最大连续区间平均数 思路:二分这个平均数,然后O(n)判断是否可行,再调整l,r.判断方法是,先求出每个数对这个平均数的贡献,再求出长度>=f的最大贡献的区间,如果这 ...

  9. 题解0002:Best Cow Fences

    题目描述:给定一个长度为n的正整数序列A.求一个平均数最大的,长度不小于L的子序列,输出这个平均数*1000. 题目链接:http://ybt.ssoier.cn:8088/problem_show. ...

随机推荐

  1. Java的含义

    Java是一种广泛使用的计算机编程语言,拥有跨平台.面向对象.泛型编程的特性,广泛应用于企业级Web应用开发和移动应用开发. Java语言它不是软件,这里给各位初学者们详细解释一下.简单来说计算机语言 ...

  2. 树莓派搭建python环境服务器

    树莓派搭建python环境服务器 服务器结构大致为:django+uwsgi+nginx+python+sqlite 配置python环境 系统本身自带了python2.7和python3.5.在这里 ...

  3. golang 之 context包

    概述     context是Go中广泛使用的程序包,由Google官方开发,在1.7版本引入.它用来简化在多个go routine传递上下文数据.(手动/超时)中止routine树等操作,比如,官方 ...

  4. 【题解】Luogu P5338 [TJOI2019]甲苯先生的滚榜

    原题传送门 这题明显可以平衡树直接大力整,所以我要说一下线段树+树状数组的做法 实际线段树+树状数组的做法也很暴力 我们先用树状数组维护每个ac数量有多少个队伍.这样就能快速求出有多少队伍ac数比现在 ...

  5. 切换GCC编译器版本

    当前版本信息 root@ubuntu:runninglinuxkernel_4.0# aarch64-linux-gnu-gcc -v Using built-in specs. COLLECT_GC ...

  6. CSS控制DIV水平垂直居中

    <div style="position:absolute; width: 600px; height: 200px; left: 50%; top: 50%; margin-left ...

  7. Apple SIP简介及在Clover中如何控制

    Apple SIP简介及在Clover中如何控制 来源 http://www.yekki.me/apple-sip-overview-and-how-to-disable-it-in-clover/ ...

  8. ssm(spring+springmvc+mybatis)整合之环境配置

    1-1.导包 导入SpringMVC.Spring.MyBatis.mybatis-spring.mysql.druid.json.上传和下载.验证的包 1-2.创建并配置web.xml文件 配置sp ...

  9. 【openshift】OC命令部署Openshift

    OC命令部署Openshift # install openshift wget -c https://github.com/openshift/origin/releases/download/v3 ...

  10. 【夯实基础】- Integer.valueof()和Integer.parseInt()的区别

    今天在看公司代码的时候,看到有人在将 String 转为 int 时,用到了Integer.parseInt(String s)方法,我一直用的是Integer.valueOf(String s)方法 ...