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 ...
随机推荐
- chrome浏览器插件让你浏览github的时候像IDE一样提供项目目录
GitHub 作为代码托管平台,竟然没有提供项目目录,方便用户在线快速浏览项目结构.所以,在线分析项目源码就会变得很繁琐,必须一层一层点击,然后再一次一次地向上返回.要知道,本来 GitHub 网站在 ...
- php的异常和处理
常见错误处理类型 语法错误 环境错误 逻辑错误 常见错误级别 Deprecated 最低级别的错误 不推荐,不建议,使用一些过期函数的时候会出现,程序继续执行 Notice 通知级别的错误 使用一些未 ...
- Android sdk manager加载缓慢或加载不出来
1.打开android sdk manager 2.打开tool->options,如图所示 3.将Proxy Settings 里的HTTP Proxy Server和HTTP Proxy P ...
- WEB网页专业词汇 汇总
Accessibility 可访问性 accessor properties 存取器属性 addition 加法 aggregate 聚合 alphabetical order 字母表顺序 Anch ...
- Django---Blog系统开发之建库
数据库配置: #sqlite3数据库配置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os. ...
- Hearbeat 介绍
Hearbeat 介绍 Linux-HA的全称是High-Availability Linux,它是一个开源项目,这个开源项目的目标是:通过社区开发者的共同努力,提供一个增强linux可靠性(reli ...
- Ubuntu 12.04下安装OpenCV 2.4.2
http://sourceforge.net/projects/opencvlibrary/files/ Ubuntu 12.04下安装OpenCV 2.4.2 http://blog.csdn.ne ...
- WINDOWS下好用的MongoDB 3.0以上客户端工具: NoSql Manager
WINDOWS下好用的MongoDB 3.0以上客户端工具: NoSql Manager https://www.mongodbmanager.com/download
- centos 源码安装php5.5
系统环境: CentOS 6.5 / 7.0 x86_64 Fedora 20 x86_64下载 PHP 源码包 # wget http://cn2.php.net/distributions/php ...
- BZOJ3244/UOJ122 [Noi2013]树的计数
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...