codeforces 626 G. Raffles(线段树+思维+贪心)
题目链接:http://codeforces.com/contest/626/problem/G
题解:这题很明显买彩票肯定要买贡献最大的也就是说买p[i]*(num[i]+1)/(num[i]+a[i]+1)-p[i]*num[i]/(num[i]+a[i])的最大值,当然这个最大值时随时改变的所以要用线段树来维护,先不考虑加彩票减彩票,可以先一开始for一遍全部的彩票先买好,然后再加彩票的或者减彩票的时候考虑不买那个改买那个。这个就需要在线段树上维护一个买一张获得贡献最大的点和不买那个减掉的贡献最小的一个,每次修改只要更新这个线段树上的这两个点就行。最后注意一下细节
#include <iostream>
#include <cstring>
#include <cstdio>
#define inf 100000000000000
using namespace std;
typedef double db;
typedef long long ll;
const db lim = 1e-;
const int M = 2e5 + ;
int a[M] , num[M];
db p[M];
struct TnT {
int l , r;
int fi , se;
db win , lose , ans;
}T[M << ];
void push_up(int i) {
T[i].ans = T[i << ].ans + T[(i << ) | ].ans;
T[i].win = max(T[i << ].win , T[(i << ) | ].win);
T[i].lose = min(T[i << ].lose , T[(i << ) | ].lose);
if(T[i].win == T[i << ].win) T[i].fi = T[i << ].fi;
else T[i].fi = T[(i << ) | ].fi;
if(T[i].lose == T[i << ].lose) T[i].se = T[i << ].se;
else T[i].se = T[(i << ) | ].se;
}
void getnum(int i , int l) {
T[i].ans = p[l] * (1.0 * num[l]) / (1.0 * (num[l] + a[l]));
T[i].ans = min(T[i].ans , 1.0 * p[l] / (1.0 * ));
if(num[l] >= a[l]) T[i].win = 0.0;
else T[i].win = p[l] * (1.0 * (num[l] + )) / (1.0 * (num[l] + a[l] + )) - p[l] * (1.0 * num[l]) / (1.0 * (num[l] + a[l]));
if(num[l] == ) T[i].lose = 1.0 * inf;
else if(num[l] > a[l]) T[i].lose = 0.0;
else T[i].lose = p[l] * (1.0 * num[l]) / (1.0 * (num[l] + a[l])) - p[l] * (1.0 * (num[l] - )) / (1.0 * (num[l] + a[l] - ));
T[i].fi = l , T[i].se = l;
}
void build(int l , int r , int i) {
int mid = (l + r) >> ;
T[i].l = l , T[i].r = r , T[i].win = , T[i].lose = , T[i].ans = ;
if(l == r) {
getnum(i , l);
return ;
}
build(l , mid , i << );
build(mid + , r , (i << ) | );
push_up(i);
}
void update(int pos , int i) {
int mid = (T[i].l + T[i].r) >> ;
if(pos == T[i].l && T[i].r == pos) {
getnum(i , pos);
return ;
}
if(mid < pos) update(pos , (i << ) | );
else update(pos , i << );
push_up(i);
}
int main() {
int n , t , q;
memset(num , , sizeof(num));
scanf("%d%d%d" , &n , &t , &q);
for(int i = ; i <= n ; i++) {
scanf("%lf" , &p[i]);
}
for(int i = ; i <= n ; i++) {
scanf("%d" , &a[i]);
}
build( , n , );
for(int i = ; i <= t ; i++) {
int now = T[].fi;
num[now]++;
update(now , );
}
while(q--) {
int t , r;
scanf("%d%d" , &t , &r);
if(t == ) a[r]++;
else a[r]--;
update(r , );
while(T[].win - T[].lose > lim) {
int now1 = T[].fi , now2 = T[].se;
num[now1]++ , num[now2]--;
update(now1 , );
update(now2 , );
}
printf("%.8lf\n" , T[].ans);
}
return ;
}
codeforces 626 G. Raffles(线段树+思维+贪心)的更多相关文章
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- 8VC Venture Cup 2016 - Elimination Round G. Raffles 线段树
G. Raffles 题目连接: http://www.codeforces.com/contest/626/problem/G Description Johnny is at a carnival ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- 【arc073e】Ball Coloring(线段树,贪心)
[arc073e]Ball Coloring(线段树,贪心) 题面 AtCoder 洛谷 题解 大型翻车现场,菊队完美压中男神的模拟题 首先钦定全局最小值为红色,剩下的袋子按照其中较大值排序. 枚举前 ...
- 【agc028E】High Elements(动态规划,线段树,贪心)
[agc028E]High Elements(动态规划,线段树,贪心) 题面 AtCoder 你有一个\([1,N]\)的排列\(P\). 一个长度为\(N\)的字符串\(S\)是好的,当且仅当: 两 ...
- Buses and People CodeForces 160E 三维偏序+线段树
Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- [Codeforces 1199D]Welfare State(线段树)
[Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...
- [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)
[Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...
随机推荐
- PID算法通俗理解,平衡车,倒立摆,适合不理解PID算法的人来看!
先插句广告,本人QQ522414928,不熟悉PID算法的可以一起交流学习,随时在线(PID资料再我的另一篇博客里) 倒立摆资料连接↓ https://www.cnblogs.com/LiuXinyu ...
- Git 学习笔记之(一) 使用 git gui 从github上下载代码
背景: 目前一些开源代码均在 GitHub上管理的,包括自己写的代码也可以放在上面进行管理.但问题是,当你换一台电脑,想要将你自己放在 GitHub 上的代码工程下载下来的时候,会遇到各种问题,目前可 ...
- 理解nodejs中的stream(流)
阅读目录 一:nodeJS中的stream(流)的概念及作用? 二:fs.createReadStream() 可读流 三:fs.createWriteStream() 可写流 回到顶部 一:node ...
- java并发编程(十三)----(JUC原子类)引用类型介绍(CAS和ABA的介绍)
这一节我们将探讨引用类型原子类:AtomicReference, AtomicStampedRerence, AtomicMarkableReference.AtomicReference的使用非常简 ...
- JAVA MQ API方式通信采用Binding MQ Server方式
package com.mqapi; /** * @modified by actorai E-mail:actorai@163.com * @version 创建时间:2010-9-15 * ...
- Spring.Net 依赖注入
一.Spring.Net概念 编程模型(Ioc,DI方式) IoC:控制反转 原来创建对象的权利由程序来控制就是new实例,IoC就是改由容器来创建,相当于一个工厂, DI:依赖注入 没有IoC就没有 ...
- 测试自动化:java+selenium3 UI自动化(1) - 环境搭建
1.前言 我大概是在2012年第一次正式接触到自动化测试,那个时候跟随我的团队一起,就当时项目的UI自动化尝试做出了探索. 在我离开那家公司的时候,我们的自动化测试体系仍然难言完美,但是也已经达到了非 ...
- 8.14 day32 TCP服务端并发 GIL解释器锁 python多线程是否有用 死锁与递归锁 信号量event事件线程q
TCP服务端支持并发 解决方式:开多线程 服务端 基础版 import socket """ 服务端 1.要有固定的IP和PORT 2.24小时不间断提供服务 3.能够支 ...
- ArrayList用法整理
System.Collections.ArrayList类是一个特殊的数组.通过添加和删除元素,就可以动态改变数组的长度. 一.优点 1.支持自动改变大小的功能 2.可以灵活的插入元素 3.可以灵活的 ...
- 使用charls抓包微信小程序的解决方案(终极解决,各种坑不怕,亲测可用,不服来战!)
第一步:使用charles进行https抓包 https://www.jianshu.com/p/7a88617ce80b 使用charles进行https抓包 使用Charles进行HTTPS抓 ...