题目传送门

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

题解

令 \(s_i\) 表示 \(a_i\) 的前缀和。

那么平均难度系数就是

\[\frac{s_r - s_{l-1}}{r-(l-1)}
\]

于是直接分数规划。二分以后是这个样子的:

\[(s_r - k\cdot r) - (s_l - k \cdot l) \geq 0
\]

这个很好求。

但是问题在于答案需要以分数形式输出。但是二分的时候只能二分出来浮点数。

怎么办呢?

实际上我们可以把二分出来的答案对应的区间给重新用分数计算一遍答案。


时间复杂度 \(O(n\log A)\)。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} const int N = 100000 + 7; int n, k, ansl, ansr, maxa, mina, posi;
ll a[N];
int p[N];
double b[N], c[N]; inline bool check(double mid) {
for (int i = 1; i <= n; ++i) b[i] = a[i] - mid * i, c[i] = std::min(c[i - 1], b[i]), p[i] = c[i] == b[i] ? i : p[i - 1];
double ans = mina;
for (int i = k; i <= n; ++i) smax(ans, b[i] - c[i - k]) && (ansl = p[i - k] + 1, ansr = i);
// dbg("mid = %lf, ans = %lf, ansl = %d, ansr = %d\n", mid, ans, ansl, ansr);
return ans >= 0;
} inline void work() {
double l = mina, r = maxa;
while (r - l >= 0.1) {
double mid = (l + r) / 2;
if (check(mid)) l = mid;
else r = mid;
}
ll sum = a[ansr] - a[ansl - 1], tmp;
posi = sum >= 0, sum = std::abs(sum);
tmp = std::__gcd(sum, ansr - ansl + 1ll);
// dbg("ansl = %d, ansr = %d\n", ansl, ansr);
if (!posi) putchar('-');
printf("%lld/%lld\n", sum / tmp, (ansr - ansl + 1) / tmp);
} inline void init() {
read(n), read(k);
for (int i = 1; i <= n; ++i) read(a[i]), smax(maxa, a[i]), smin(mina, a[i]), a[i] += a[i - 1];
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

bzoj5090 [Lydsy1711月赛]组题 分数规划的更多相关文章

  1. BZOJ5090: [Lydsy1711月赛]组题(01分数规划)

    5090: [Lydsy1711月赛]组题 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 785  Solved: 186[Submit][Status ...

  2. 【BZOJ5090】组题 分数规划

    [BZOJ5090]组题 Description 著名出题人小Q的备忘录上共有n道可以出的题目,按照顺序依次编号为1到n,其中第i道题目的难度系数被小Q估计为a_i,难度系数越高,题目越难,负数表示这 ...

  3. bzoj5090组题 分数规划

    组题 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 542  Solved: 114[Submit][Status][Discuss] Descript ...

  4. bzoj5090[lydsy11月赛]组题

    裸的01分数规划,二分答案,没了. #include<cstdio> #include<algorithm> using namespace std; const int ma ...

  5. BZOJ5090 组题 BZOJ2017年11月月赛 二分答案 单调队列

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ5090 11月月赛A题 题意概括 给出n个数. 求连续区间(长度大于等于k)最大平均值. 题解 这题 ...

  6. Yougth的最大化(好题,二分查找 0 1分数规划)

    Yougth的最大化 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价 ...

  7. 【poj 2976】Dropping tests(算法效率--01分数规划 模版题+二分){附【转】01分数规划问题}

    P.S.又是一个抽时间学了2个小时的新东西......讲解在上半部分,题解在下半部分. 先说一下转的原文:http://www.cnblogs.com/perseawe/archive/2012/05 ...

  8. bzoj5091 [Lydsy1711月赛]摘苹果 概率题

    [Lydsy1711月赛]摘苹果 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 174  Solved: 135[Submit][Status][Dis ...

  9. POJ2976 题解 0/1分数规划入门题 二分

    题目链接:http://poj.org/problem?id=2976 关于 0/1分数规划 参见 这篇博客 实现代码如下: #include <cstdio> #include < ...

随机推荐

  1. leetcode234 回文链表 两种做法(stack(空间非O(1)),空间O(1))

    link: leetcode234 回文链表 方法1, 快慢指针,把前半部分存入栈中和后半部分比较 public boolean isPalindrome(ListNode head) { if(he ...

  2. python - del 方法

    转自:http://blog.csdn.net/love1code/article/details/47276683 python中的del用法比较特殊,新手学习往往产生误解,弄清del的用法,可以帮 ...

  3. Vue知识整理12:事件绑定

    采用v-on命令进行事件的绑定操作,通过单击按钮,实现按钮文字上数值的增加 带参数的事件过程 可以添加$event事件,实现事件信息的获取

  4. 【漏洞学习】HOST 头攻击漏洞

    日期:2018-03-06 14:32:51 作者:Bay0net 0x01. 前言 在一般情况下,几个网站可能会放在同一个服务器上,或者几个 web 系统共享一个服务器,host 头来指定应该由哪个 ...

  5. lua 十进制转二进制

    -- Converts a byte to a string of 0s and 1s. function byte2bin(n) local t = {} for i=7,0,-1 do t[#t+ ...

  6. Spring获取request、session以及servletContext

    获取request: HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestA ...

  7. 【HANA系列】SAP HANA STUDIO客户端升级更新

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA STUDIO客 ...

  8. 【ABAP系列】SAP ABAP 运算符

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 运算符   前 ...

  9. Centos快速安装 Memcached

    rpm qa|grep memcached //首先检查memcache是否已经安装完成 yum install memcached //(提示你是否确认安装输入y)检查完成后执行安装命令 yum i ...

  10. python 并发编程 多进程 目录

    python multiprocessing模块 介绍 python 开启进程两种方法 python 并发编程 查看进程的id pid与父进程id ppid python 并发编程 多进程 Proce ...