CodeForces - 150C :Smart Cheater (线段树,求最大连续区间)
I guess there's not much point in reminding you that Nvodsk winters aren't exactly hot. That increased the popularity of the public transport dramatically. The route of bus 62 has exactly n stops (stop 1 goes first on its way and stop n goes last). The stops are positioned on a straight line and their coordinates are 0 = x1 < x2 < ... < xn.
Each day exactly m people use bus 62. For each person we know the number of the stop where he gets on the bus and the number of the stop where he gets off the bus. A ticket from stop a to stop b (a < b) costs xb - xa rubles. However, the conductor can choose no more than one segment NOT TO SELL a ticket for. We mean that conductor should choose C and D (С <= D) and sell a ticket for the segments [A, C] and [D, B], or not sell the ticket at all. The conductor and the passenger divide the saved money between themselves equally. The conductor's "untaxed income" is sometimes interrupted by inspections that take place as the bus drives on some segment of the route located between two consecutive stops. The inspector fines the conductor by c rubles for each passenger who doesn't have the ticket for this route's segment.
You know the coordinated of all stops xi; the numbers of stops where the i-th passenger gets on and off, ai and bi (ai < bi); the fine c; and also pi — the probability of inspection on segment between the i-th and the i + 1-th stop. The conductor asked you to help him make a plan of selling tickets that maximizes the mathematical expectation of his profit.
Input
The first line contains three integers n, m and c (2 ≤ n ≤ 150 000, 1 ≤ m ≤ 300 000, 1 ≤ c ≤ 10 000).
The next line contains n integers xi (0 ≤ xi ≤ 109, x1 = 0, xi < xi + 1) — the coordinates of the stops on the bus's route.
The third line contains n - 1 integer pi (0 ≤ pi ≤ 100) — the probability of inspection in percents on the segment between stop i and stop i + 1.
Then follow m lines that describe the bus's passengers. Each line contains exactly two integers ai and bi (1 ≤ ai < bi ≤ n) — the numbers of stops where the i-th passenger gets on and off.
Output
Print the single real number — the maximum expectation of the conductor's profit. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
Examples
3 3 10
0 10 100
100 0
1 2
2 3
1 3
90.000000000
10 8 187
0 10 30 70 150 310 630 1270 2550 51100
13 87 65 0 100 44 67 3 4
1 10
2 9
3 8
1 5
6 10
2 7
4 10
4 5
76859.990000000
题意:有从左到右1到N个站,每一站到下一战的费用是两站间距离,即X[i+1]-X[i]。现在假设乘客从a站到b站,那么他可以和售票员密谋,不买其中连续一段的票,然后这些钱两人对半分。而每一站到下一站有被发现的几率为Pi,如果被发现就会罚款C元。有M个乘客,问售票员一共最多赚多少钱。
思路:乘客之间没有联系,单独考虑,我们保存每一站到下一站的收益,即X[i+1]-X[i]-Pi*C,那么现在问题转化为区间最大连续区间值。也就是裸的线段树题了。
手动打的丑代码:
#include<bits/stdc++.h>
#define piii pair<pair<double,double>,double>
#define F first
#define S second
using namespace std;
const int maxn=;
double x[maxn],a[maxn];
double sum[maxn]; piii Mx[maxn];//0左,1右,2最大
void build(int Now,int L,int R)
{
if(L==R){
sum[Now]=a[L]; Mx[Now].F.F=Mx[Now].F.S=a[L];
Mx[Now].S=max(a[L],0.0);
return ;
}
int Mid=(L+R)>>;
build(Now<<,L,Mid); build(Now<<|,Mid+,R);
sum[Now]=sum[Now<<]+sum[Now<<|];
Mx[Now].F.F=max(Mx[Now<<].F.F,sum[Now<<]+Mx[Now<<|].F.F);
Mx[Now].F.S=max(Mx[Now<<|].F.S,sum[Now<<|]+Mx[Now<<].F.S);
Mx[Now].S=max(max(Mx[Now<<].S,Mx[Now<<|].S),Mx[Now<<].F.S+Mx[Now<<|].F.F);
}
piii query(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) return Mx[Now];
int Mid=(L+R)/;
if(r<=Mid) return query(Now<<,L,Mid,l,r);
if(l>Mid) return query(Now<<|,Mid+,R,l,r);
else {
piii res,tmp;
res=query(Now<<,L,Mid,l,r);
tmp=query(Now<<|,Mid+,R,l,r);
res.S=max(max(res.S,tmp.S),res.F.S+tmp.F.F);
res.F.F=max(res.F.F,sum[Now<<]+tmp.F.F);
res.F.S=max(tmp.F.S,sum[Now<<|]+res.F.S);
return res;
}
}
int main()
{
int N,M,i; double C,P,ans=;
scanf("%d%d%lf",&N,&M,&C);
for(i=;i<=N;i++) scanf("%lf",&x[i]);
for(i=;i<N;i++){
scanf("%lf",&P);
a[i]=(x[i+]-x[i])/-C*P/100.0;
}
build(,,N-);
for(i=;i<=M;i++){
int L,R; scanf("%d%d",&L,&R);
ans+=query(,,N-,L,R-).S;
}
printf("%.8lf\n",ans);
return ;
}
以及别人的重载加法的令人赏心悦目的代码:
#include <stdio.h>
#define db double
const int N=;
const int M=*N;
db max(db a, db b){ return a>b?a:b;}
struct Node
{
db l,r,sum,best;
Node(){}
Node(db x){ l=r=best=max(x,);sum=x;}
} node[M];
Node operator + (Node a, Node b)
{
Node ans;
ans.best=max(a.best,b.best);
ans.best=max(ans.best,a.r+b.l);
ans.sum=a.sum+b.sum;
ans.l=max(a.l,a.sum+b.l);
ans.r=max(b.r,b.sum+a.r);
return ans;
}
db sub[N];
int ls[M],rs[M],tsz,root;
void Build(int &c, int ss, int se)
{
c=++tsz;
if(ss==se){ node[c]=Node(sub[ss]);return;}
int mid=ss+se>>;
Build(ls[c],ss,mid);
Build(rs[c],mid+,se);
node[c]=node[ls[c]]+node[rs[c]];
}
Node Get(int c, int ss, int se, int qs, int qe)
{
if(qs<=ss && qe>=se) return node[c];
int mid=ss+se>>;
if(qe<=mid) return Get(ls[c],ss,mid,qs,qe);
if(qs>mid) return Get(rs[c],mid+,se,qs,qe);
return Get(ls[c],ss,mid,qs,qe)+Get(rs[c],mid+,se,qs,qe);
}
int x[N],p[N];
int main()
{
int n,m,c,i,l,r;
scanf("%i %i %i",&n,&m,&c);
for(i=;i<=n;i++) scanf("%i",&x[i]);
for(i=;i<=n;i++) scanf("%i",&p[i]),sub[i]=(db)(x[i]-x[i-])/-(db)c*p[i]/;
Build(root,,n);
db sol=;
while(m--)
{
scanf("%i %i",&l,&r);
sol+=Get(root,,n,l+,r).best;
Node tmp=Get(root,,n,l+,r);
}
printf("%.12llf\n",sol);
return ;
}
CodeForces - 150C :Smart Cheater (线段树,求最大连续区间)的更多相关文章
- 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 316E3]Summer Homework(线段树+斐波那契数列)
[Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- UVA 11983 Weird Advertisement --线段树求矩形问题
题意:给出n个矩形,求矩形中被覆盖K次以上的面积的和. 解法:整体与求矩形面积并差不多,不过在更新pushup改变len的时候,要有一层循环,来更新tree[rt].len[i],其中tree[rt] ...
- BNU 2418 Ultra-QuickSort (线段树求逆序对)
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=2418 解题报告:就是给你n个数,然后让你求这个数列的逆序对是多少?题目中n的范围是n & ...
- hdu 1394 (线段树求逆序数)
<题目链接> 题意描述: 给你一个有0--n-1数字组成的序列,然后进行这样的操作,每次将最前面一个元素放到最后面去会得到一个序列,那么这样就形成了n个序列,那么每个序列都有一个逆序数,找 ...
- xdoj-1324 (区间离散化-线段树求区间最值)
思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间 2 核心:dp[i] 覆盖从1到i区间的最小花费 dp[a[i].r]=min (dp[k])+a[i]s; ...
- 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)
题目链接:http://codevs.cn/problem/4163/ 题目:
- poj2299 Ultra-QuickSort(线段树求逆序对)
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
随机推荐
- CKEditor & CKFinder集成
CKEditor集成 CKEditor(原名FckEditor): 著名的HTML编辑器(可在线编辑HTML) 配置: ①将CKEditor中的(adapters images lang plugin ...
- windows下载Mysql-python
Mysql-python第三方模块官方不支持windows系统,而国外大学提供了非官方 的支持windows系统的模块,可前往 https://www.lfd.uci.edu/~gohlke/pyth ...
- 编译android源码m、mm、mmm命令的使用
http://blog.163.com/zz_forward/blog/static/212898222201442873435471/ gcc怎么查看它的默认include路径和库的路径呢? //- ...
- HNOI2019梦游记
\(Day_0\) 十点半开始睡觉,开始了八个小时的不眠之夜,整晚都没睡着,这状态明天肯定挂了 \(Day_1\) 开局一条鱼,计算几何只会\(20\) 还是\(T2\)的\(20\)纯暴力好打,\( ...
- HDU 4783 Clumsy Algorithm
题意不提. 我们可以发现,可以将最终序列分为对于第i个位置i-pi>=0与i-pi<0种两个子序列.且如果f[n]==g[n],则有两个子序列都递增. 原因是f[n]表示1-n这个排列的逆 ...
- 20145231 《Java程序设计》第一周学习总结
20145231 <Java程序设计>第一周学习总结 教材学习内容总结 Java三大平台Java SE,Java EE,Java ME.其中,Java SE是我们学习的基础. Java S ...
- python局部变量引用问题
a = [1, 2] b = 'Immutable' def test(): # global b print(a) a.append('asd') b = b + 'asd' # 当只是引用变量b的 ...
- SpringBoot Maven打包项目JAR/WAR
安装Maven 1. 登录 http://maven.apache.org/download.cgi 2. 下载 maven 压缩包 3. 解压apache-maven-3.6.0-bin.tar.g ...
- 解决spring、springMVC重复扫描导致事务失效的问题
在主容器中(applicationContext.xml),将Controller的注解排除掉 1 2 3 而在springMVC配置文件中将Service注解给去掉 1 2 3 4 因为spring ...
- SSM mapper.xml
MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL 映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JDBC 代码来比较,你会发现映射文件节省了大约 ...