NOIP2015普及组T4推销员(暴力+线段树)
题目:阿明是一名推销员,他奉命到螺丝街推销他们公司的产品。螺丝街是一条死胡同,出口与入口是同一个,街道的一侧是围墙,另一侧是住户。螺丝街一共有N家住户,第i家住户到入口的距离为Si米。由于同一栋房子里可以有多家住户,所以可能有多家住户与入口的距离相等。阿明会从入口进入,依次向螺丝街的X家住户推销产品,然后再原路走出去。
阿明每走1米就会积累1点疲劳值,向第i家住户推销产品会积累Ai点疲劳值。阿明是工作狂,他想知道,对于不同的X,在不走多余的路的前提下,他最多可以积累多少点疲劳值
输入格式:
第一行有一个正整数N,表示螺丝街住户的数量。
接下来的一行有N个正整数,其中第i个整数Si表示第i家住户到入口的距离。数据保证S1≤S2≤…≤Sn<10^8。
接下来的一行有N个正整数,其中第i个整数Ai表示向第i户住户推销产品会积累的疲劳值。数据保证Ai<10^3。
输出格式:
输出N行,每行一个正整数,第i行整数表示当X=i时,阿明最多积累的疲劳值。
【数据说明】
对于20%的数据,1≤N≤20;
对于40%的数据,1≤N≤100;
对于60%的数据,1≤N≤1000;
对于100%的数据,1≤N≤1000000。
NOIP之前一直以为NOIP不会考线段树(其实真的没考,这题正解是贪心但是我不会%%%),所以写T4的时候打完暴力根本就没想到写线段树。暴力思路为一重循环枚举推销多少家,第二重循环找出消耗疲劳值最大的,然后改为0,输出答案,效率O(n^2)。
暴力代码如下:
var
len,tired:array[..]of longint;
n,i,j,max,s,ans,now,t,sum:longint; begin
readln(n);
for i:= to n do read(len[i]);
for i:= to n do read(tired[i]);
ans:=;
max:=;
for i:= to n do
begin
max:=-maxlongint;
for j:= to n do
begin
if j>now then s:=(*len[j])-(*sum)+tired[j]
else s:=tired[j];
if s>max then
begin
t:=j;
max:=s;
end;
end;
if t>now then
begin
now:=t;
sum:=len[now];
end;
len[t]:=;tired[t]:=;
inc(ans,max);
writeln(ans);
end;
end.
然后我们可以发现我们找最大值效率是O(n),那我们就用线段树来维护最大值就行了,效率O(log(n)),则总时间效率为O(nlog(n))。
这里简单说一下思路。建两棵线段树,对于现在走到的点,一棵维护左边的疲劳值最大值,也就是只需要维护推销的疲劳值最大值,一棵维护右边的疲劳值最大值,需要维护推销的疲劳值+走路的疲劳值。
代码如下(3kb):
type
point=record
max,delta,l,r,num:longint;
end;
var
len,tired:array[..]of int64;
i,j:longint;
n,max,ans,now,tt,t1,t2,max2,l,x,y:int64;
a:array[..,..]of point; procedure insert(x,num,delta,t:longint);
begin
if a[x,t].l=a[x,t].r then a[x,t].max:=
else
begin
if num<=(a[x,t].l+a[x,t].r)>> then insert(*x,num,delta,t);
if num>(a[x,t].l+a[x,t].r)>> then insert(*x+,num,delta,t);
if a[*x,t].max>a[*x+,t].max then
begin
a[x,t].max:=a[*x,t].max;
a[x,t].num:=a[*x,t].num;
end
else
begin
a[x,t].max:=a[*x+,t].max;
a[x,t].num:=a[*x+,t].num;
end;
end;
end; procedure build(x,l,r,t:longint);
begin
a[x,t].l:=l;a[x,t].r:=r;
if l=r then
begin
a[x,t].num:=l;
read(a[x,t].max);
a[x,t].delta:=;
if t= then len[l]:=a[x,t].max;
if t= then tired[l]:=a[x,t].max;
end
else
begin
build(*x,l,(l+r)>>,t);
build(*x+,((l+r)>>)+,r,t);
if a[*x,t].max>a[*x+,t].max then
begin
a[x,t].max:=a[*x,t].max;
a[x,t].num:=a[*x,t].num;
end
else
begin
a[x,t].max:=a[*x+,t].max;
a[x,t].num:=a[*x+,t].num;
end;
a[x,t].delta:=;
end;
end; procedure update(x,l,r,delta,t:longint);
begin
if (l<=a[x,t].l)and(a[x,t].r<=r) then
inc(a[x,t].delta,delta)
else
begin
if l<=(a[x,t].l+a[x,t].r)>> then update(*x,l,r,delta,t);
if r>(a[x,t].l+a[x,t].r)>> then update(*x+,l,r,delta,t);
if a[*x,t].max+a[*x,t].delta>a[*x+,t].max+a[*x+,t].delta then
begin
a[x,t].max:=a[*x,t].max+a[*x,t].delta;
a[x,t].num:=a[*x,t].num;
end
else
begin
a[x,t].max:=a[*x+,t].max+a[*x+,t].delta;
a[x,t].num:=a[*x+,t].num;
end;
end;
end; function query(x,l,r,h,t:longint):int64;
var
ret:int64;
begin
ret:=;
if (l<=a[x,t].l)and(a[x,t].r<=r) then
begin
ret:=a[x,t].max+a[x,t].delta;
if ret>=h then
tt:=a[x,t].num;
end
else
begin
inc(a[*x,t].delta,a[x,t].delta);
inc(a[*x+,t].delta,a[x,t].delta);
inc(a[x,t].max,a[x,t].delta);
a[x,t].delta:=;
if l<=(a[x,t].l+a[x,t].r)>> then ret:=query(*x,l,r,h,t);
if r>(a[x,t].l+a[x,t].r)>> then
if ret<query(*x+,l,r,ret,t) then ret:=query(*x+,l,r,ret,t);
end;
query:=ret;
end; begin
readln(n);
build(,,n,);
build(,,n,);
for i:= to n do update(,i,i,len[i]+tired[i],);
ans:=;
max:=;
tt:=;
for i:= to n do
begin
max:=-maxlongint;
if now> then max:=query(,,now,,);
t1:=tt;
max2:=-maxlongint;
if now<n then max2:=query(,now,n,,);
if max<max2 then
begin
max:=max2;
t1:=tt;
end;
if t1>now then
begin
update(,t1,n,-*len[t1],);
insert(,t1,,);
now:=t1;
end;
insert(,t1,,);
inc(ans,max);
writeln(ans);
end;
end.
以下为官方数据情况:

NOIP2015普及组T4推销员(暴力+线段树)的更多相关文章
- 【NOIP2015普及组】 推销员(纪中数据-标准)
题目 [题目描述] 阿明是一名推销员,他奉命到螺丝街推销他们公司的产品.螺丝街是一条死胡同,出口与入口是同一个,街道的一侧是围墙,另一侧是住户.螺丝街一共有 N 家住户,第 i 家住户到入口的距离为 ...
- 【NOIP2015普及组】推销员_详解
题目 题目大意 阿明是一名推销员--螺丝街是一条直线,一端有入口,一共有 \(N(<100,000)\) 家住户,第 \(i\) 家住户到入口的距离为 \(S_i\) 米.由于同一栋房子里可以有 ...
- NOIP2015普及组总结
NOIP2015普及组总结 这次考试总体感觉不错,不过觉得时间有点紧,在最后30分钟才打完. 第一题(金币coin):大大的W!爆搜O(N),一分钟打完: 第二题(扫雷游戏mine):同上: 第三题( ...
- NOIP2004普及组第3题 FBI树
/* 1106: NOIP2004普及组第3题 FBI树 时间限制: 1 Sec 内存限制: 128 MB 提交: 10 解决: 9 [提交] [状态] [讨论版] [命题人:外部导入] 题目描述 我 ...
- [NOIP2015 普及组] 扫雷游戏
[NOIP2015 普及组] 扫雷游戏 难度:入门 题目描述 扫雷游戏是一款十分经典的单机小游戏.在nn行mm列的雷区中有一些格子含有地雷(称之为地雷格),其他格子不含地雷(称之为非地雷格).玩家翻开 ...
- NOIP2015普及组第四题推销员
好久没有写博客了,今天再写一篇.还是先看题: 试题描述 阿明是一名推销员,他奉命到螺丝街推销他们公司的产品.螺丝街是一条死胡同,出口与入口是同一个,街道的一侧是围墙,另一侧是住户.螺丝街一共有 N 家 ...
- hdu 5475 An easy problem(暴力 || 线段树区间单点更新)
http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...
- HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)
题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS Memory Limit: 32768 K Description The inve ...
- HDU 3974 Assign the task 暴力/线段树
题目链接: 题目 Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- Lua学习笔记(4): 字符串
Lua的字符串有3种初始化方式 str = "2333" str = 'hahahaha' str = [[ruarua]] 其中单引号和双引号的初始化方式并无区别,[[]]双中括 ...
- idea下增加scala
1 idea工具下,下载scala插件 2 idea下新建scala工程 File——New——module 如果按照上图,设置后点击下载,出现下图下载过慢情况下, 这里我选择了等待,大概等了半小时才 ...
- MSCOCO - pycocoDemo 学习版
Reference: https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoDemo.ipynb https://git ...
- Machine Learning笔记整理 ------ (一)基本概念
机器学习的定义:假设用P来评估计算机程序在某任务类T上的性能,若一个程序通过利用经验E,使其在T中任务获得了性能改善,我们则说关于任务类T和P,该程序对经验E进行了学习(Mitchell, 1997) ...
- Factorials 阶乘(思维)
Description N 的阶乘写作N!表示小于等于N的所有正整数的乘积.阶乘会很快的变大,如13!就必须用32位整数类型来存储,70!即使用浮点数也存不下了.你的任务是 找到阶乘最后面的非零位.举 ...
- ZOJ 1842 Prime Distance(素数筛选法2次使用)
Prime Distance Time Limit: 2 Seconds Memory Limit: 65536 KB The branch of mathematics called nu ...
- Visual C++ 8.0对象布局
哈哈,从M$ Visual C++ Team的Andy Rich那里又偷学到一招:VC8的隐含编译项/d1reportSingleClassLayout和/d1reportAllClassLayout ...
- WebService(一)
1.简介 Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用 ...
- C语言语法树
- 第四周PSP &进度条
团队项目PSP 一:表格 C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 讨论 讨论开发环境.工具以及技术 8:37 10:42 25 10 ...