题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6070

题意: 给出一个题目提交序列, 从中选出一个正确率最小的子串. 选中的子串中每个题目当且仅当最后一次提交是正确的.

思路: 分数规划

二分答案, 然后在 check 函数中查找是否存在某个区j间 [l, r] 使得 sum(l, r) / (r - l + 1) <= mid, 即 sum(l, r) + l * mid <= (r + 1) * mid. 可以用个线段树来维护 sum(l, r) + l * mid . 建树时直接将 l * mid 放入树中, 然后从左到右枚举 r, 对于当前 i, a[i] 对区间 [pre[i] + 1, i] 的贡献为一(区间 [1, pre[i]] 内的贡献之前的a[i]已经计算了) . 这样对于当前更新后, 1 <= j <= i , sum[j] 即为区间 [j, i] 内的贡献. 那么对于当前 i, query(1, i) 就得到了所有以 i 为后缀的区间的贡献最小值. 遍历完 r 后即得到了所有区间的贡献最小值.

最后要注意一下线段树区间更新,区间最值的 lazy 数组维护写法, 最值和区间求和是不同的.

代码:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std; const double eps = 1e-;
const int MAXN = 6e4 + ;
int a[MAXN], pre[MAXN], last[MAXN], n;
double sum[MAXN << ], lazy[MAXN << ]; void push_up(int rt){ //向上更新取最值
sum[rt] = min(sum[rt << ], sum[rt << | ]);
} void push_down(int rt){
if(lazy[rt]){//将标记向下更新,维护的是最值,sum不需要求和
lazy[rt << ] += lazy[rt];
lazy[rt << | ] += lazy[rt];
sum[rt << ] += lazy[rt];
sum[rt << | ] += lazy[rt];
lazy[rt] = ;
}
} void build(int l, int r, int rt, double value){
lazy[rt] = ;
if(l == r){
sum[rt] = value * l;
return;
}
int mid = (l + r) >> ;
build(lson, value);
build(rson, value);
push_up(rt);
} void update(int L, int R, int value, int l, int r, int rt){
if(L <= l && R >= r){
lazy[rt] += value;
sum[rt] += value;//维护的是最值,sum不需要求和
return;
}
push_down(rt);
int mid = (l + r) >> ;
if(L <= mid) update(L, R, value, lson);
if(R > mid) update(L, R, value, rson);
push_up(rt);
} double query(int L, int R, int l, int r, int rt){
if(L <= r && R >= r) return sum[rt];
push_down(rt);
double cnt = 1e5;
int mid = (l + r) >> ;
if(L <= mid) cnt = min(cnt, query(L, R, lson));
if(R > mid) cnt = min(cnt, query(L, R, rson));
return cnt;
} bool check(double mid){
build(, n, , mid);
for(int i = ; i <= n; i++){
update(pre[i] + , i, , , n, );
if(query(, i, , n, ) <= (double)mid *(i + )) return true;
}
return false;
} int main(void){
int t;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
memset(pre, , sizeof(pre));
memset(last, , sizeof(last));
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
pre[i] = last[a[i]];
last[a[i]] = i;
}
double l = , r = ;
while(r - l > eps){
double mid = (l + r) / ;
if(check(mid)) r = mid - eps;
else l = mid + eps;
}
printf("%.5lf\n", r + eps);
}
return ;
}

hdu6070(分数规划/二分+线段树区间更新,区间最值)的更多相关文章

  1. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  2. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  3. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  4. hdu 1166线段树 单点更新 区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. hdu6070 Dirt Ratio 二分+线段树

    /** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...

  6. hdu2795(线段树单点更新&区间最值)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要 ...

  7. hdu1166(线段树单点更新&区间求和模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...

  8. 【HDU】1754 I hate it ——线段树 单点更新 区间最值

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 D ...

随机推荐

  1. 京东自营,你TM太坑了。

    双12来了,京东自营好坑.昨天(12月6日)看的一条秋裤,89元,今天准备买,居然涨到了119,他大爷的. 京东你大爷的.

  2. DBGrid1

    A.ShowMessage(IntToStr(Column.Index));   B.ShowMessage(IntToStr(dbgrid1.SelectedField.Index)); .得到当前 ...

  3. Building Performant Expand & Collapse Animations

    t's starting to be pretty common knowledge that there are only 2 things you can animate cheaply in C ...

  4. Linux-NoSQL之Redis(一)

    1.Redis介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(s ...

  5. Tomcat报错:HTTP Status 500 - Wrapper cannot find servlet class

    HTTP Status 500 - Wrapper cannot find servlet class com.servlet.servlet.RegServlet or a class it dep ...

  6. redis源码笔记 - redis-cli.c

    这份代码是redis的client接口,其和server端的交互使用了deps目录下的hiredis c库,同时,在这部分代码中,应用了linenoise库完成类似history命令查询.自动补全等终 ...

  7. [转]nodejs中的process模块--child_process.exec

    1.process是一个全局进程,你可以直接通过process变量直接访问它. process实现了EventEmitter接口,exit方法会在当进程退出的时候执行.因为进程退出之后将不再执行事件循 ...

  8. python 接口测试字符类型转换

    接口返回数据: print sql[0][10] {"recv_name":"rdgztest_63126","send_name":&qu ...

  9. 启动新内核出现:Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000004

    转载请注明出处:http://blog.csdn.net/qq_26093511/article/details/51841281 启动新内核出现错误:Kernel panic - not synci ...

  10. 关于redis,学会这8点就够了(转)

    1.redis是什么 redis是一种支持Key-Value等多种数据结构的存储系统.可用于缓存.事件发布或订阅.高速队列等场景.该数据库使用ANSI C语言编写,支持网络,提供字符串.哈希.列表.队 ...