题目传送门

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. leetcode 125 验证回文字符串 Valid Palindrome

    验证回文字符串 C++ 思路就是先重新定义一个string ,先遍历第一遍,字符串统一小写,去除空格:然后遍历第二遍,首尾一一对应比较:时间复杂度O(n+n/2),空间O(n); class Solu ...

  2. FreeBSD上安装Cassandra 3.10

    哈哈,你居然点进来了,来吧,一起吐槽FreeBSD啊,装了一上午Cassandra 3.10都没有装成功, 终于,鄙人一条 shutdown -p now 结束了FreeBSD,默默打开了CentOS ...

  3. Python学习之==>第三方模块的安装、模块导入

    一.模块&包 1.模块 模块实质上就是一个Python文件,它是用来组织代码的.意思就是把Python代码写在里面,文件名就是模块的名称.例如:random.py,random就是模块的名称. ...

  4. 旅游局nginx配置

    #user nobody;worker_processes 1; #error_log logs/error.log;#error_log logs/error.log notice;#error_l ...

  5. redis 锦集

    redis 锦集url:http://blog.csdn.net/lqadam/article/category/7479450 1. redis 排序 2.redis 慢查询.位数组和事务 3.re ...

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

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

  7. [LeetCode] 1092. Shortest Common Supersequence

    LeetCode刷题记录 传送门 Description Given two strings str1 and str2, return the shortest string that has bo ...

  8. springboot + mybaits + oracle 项目

    1.pom设置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  9. Spring中的常见注解

    @Controller 标识一个该类是Spring MVC controller处理器,用来创建处理http请求的对象.   @RestController Spring4之后加入的注解,原来在@Co ...

  10. 增强 Jupyter Notebook的功能

    增强 Jupyter Notebook的功能 Jupyter Notebook 是所有开发者共享工作的神器,它为共享 Notebooks 提供了一种便捷方式:结合文本.代码和图更快捷地将信息传达给受众 ...