计蒜客 30996 - Lpl and Energy-saving Lamps - [线段树][2018ICPC南京网络预赛G题]
题目链接:https://nanti.jisuanke.com/t/30996
During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Dragon imprisoned Lpl in the Castle? Dragon smiled enigmatically and answered that it is a big secret. After a pause, Dragon added:
— We have a contract. A rental agreement. He always works all day long. He likes silence. Besides that, there are many more advantages of living here in the Castle. Say, it is easy to justify a missed call: a phone ring can't reach the other side of the Castle from where the phone has been left. So, the imprisonment is just a tale. Actually, he thinks about everything. He is smart. For instance, he started replacing incandescent lamps with energy-saving lamps in the whole Castle...
Lpl chose a model of energy-saving lamps and started the replacement as described below. He numbered all rooms in the Castle and counted how many lamps in each room he needs to replace.
At the beginning of each month, Lpl buys mm energy-saving lamps and replaces lamps in rooms according to his list. He starts from the first room in his list. If the lamps in this room are not replaced yet and Lpl has enough energy-saving lamps to replace all lamps, then he replaces all ones and takes the room out from the list. Otherwise, he'll just skip it and check the next room in his list. This process repeats until he has no energy-saving lamps or he has checked all rooms in his list. If he still has some energy-saving lamps after he has checked all rooms in his list, he'll save the rest of energy-saving lamps for the next month.
As soon as all the work is done, he ceases buying new lamps. They are very high quality and have a very long-life cycle.
Your task is for a given number of month and descriptions of rooms to compute in how many rooms the old lamps will be replaced with energy-saving ones and how many energy-saving lamps will remain by the end of each month.


样例输入:
5 4
3 10 5 2 7
10
5 1 4 8 7 2 3 6 4 7
样例输出:
4 0
1 1
3 6
5 1
5 1
2 0
3 2
4 4
3 6
5 1
题意:
有一修理工,知道了一个城堡里的 n 个房间里的灯泡需要更换,每个房间需要更换的灯泡数是 k[1~n],
现在修理工每个月固定买进 m 个节能灯,进行更换操作:
修理工把这 1~n 个房间按顺序从左到右写在一张列表上,每次都从最左边开始选择房间,
假设选到的房间内需要更换的灯泡数量超过手头的节能灯数量,就跳过,直到找到第一个小于手头节能灯数目的,
他会把节能灯换上去,然后继续往右遍历房间。而且,修理工一个月只遍历一次房间,同时,若所有房间灯泡全部换完,则不再购入节能灯,停止工作。
且若当月遍历完房间之后,还有剩下若干节能灯,则留到下个月,和下个月购入的 m 个节能灯合并到一起。
现在给出 q 个查询,每个查询包含一个数字 d,表示查询第 d 个月的工作完毕时,有多少间房间灯泡已经更换完毕,修理工手头还有多少个节能灯。
题解:
模拟,枚举月份,对于第 i 个月:
假设已知本月购买进 m 个节能灯后,手头节能灯数为 K,
找到从左往右遍历的第一个待更换灯泡数比 K 小的房间,更换灯泡即可。
那么,如何找到从左到右第一个待更换灯泡数比 K 小的房间呢……用线段树。
用一个能满足 ①单点修改 ②查询第一个比 k 小的数的位置 的线段树就可以了。
AC代码:
#include<bits/stdc++.h>
using namespace std; const int maxn=1e5+;
const int INF=0X3f3f3f3f; int n,m,q;
int k[maxn],d[maxn];
struct Ans{
int room; //已经把灯泡更换完的房间
int lamp; //手头剩下的节能灯数量
}ans[maxn]; /********************************* Segment Tree - st *********************************/
struct Node{
int l,r;
int val;
}node[*maxn];
void pushup(int root)
{
node[root].val=min(node[root*].val,node[root*+].val);
}
void build(int root,int l,int r)
{
if(l>r) return;
node[root].l=l; node[root].r=r;
node[root].val=;
if(l==r) node[root].val=k[l];
else
{
int mid=l+(r-l)/;
build(root*,l,mid);
build(root*+,mid+,r);
pushup(root);
}
}
void update(int root,int pos,int val)
{
if(node[root].l==node[root].r)
{
node[root].val=val;
return;
} int mid=node[root].l+(node[root].r-node[root].l)/;
if(pos<=mid) update(root*,pos,val);
if(pos>mid) update(root*+,pos,val);
pushup(root);
}
int query(int root,int k) //查询第一个比k小的数位置
{
if(node[root].val>k) return ; if(node[root].l==node[root].r) return node[root].l;
else
{
if(node[root*].val<=k) return query(root*,k);
else if(node[root*+].val<=k) return query(root*+,k);
}
}
/********************************* Segment Tree - ed *********************************/ int main()
{
cin>>n>>m;
for(int i=;i<=n;i++) cin>>k[i];
build(,,n); int lamp=,tot=;
for(int mon=;mon<maxn;mon++)
{
if(tot>=n)
{
ans[mon].room=tot;
ans[mon].lamp=lamp;
continue;
} lamp+=m;
while()
{
int pos=query(,lamp);
if(pos==) break; lamp-=k[pos];
update(,pos,INF);
tot+=;
} ans[mon].room=tot;
ans[mon].lamp=lamp;
} cin>>q;
for(int i=,d;i<=q;i++)
{
cin>>d;
printf("%d %d\n",ans[d].room,ans[d].lamp);
}
}
计蒜客 30996 - Lpl and Energy-saving Lamps - [线段树][2018ICPC南京网络预赛G题]的更多相关文章
- 计蒜客 31459 - Trace - [线段树][2018ICPC徐州网络预赛G题]
题目链接:https://nanti.jisuanke.com/t/31459 样例输入 3 1 4 4 1 3 3 样例输出 10 题意: 二维平面上给出 $n$ 个点,每个点坐标 $\left( ...
- 计蒜客 31460 - Ryuji doesn't want to study - [线段树][2018ICPC徐州网络预赛H题]
题目链接:https://nanti.jisuanke.com/t/31460 Ryuji is not a good student, and he doesn't want to study. B ...
- 计蒜客 30996.Lpl and Energy-saving Lamps-线段树(区间满足条件最靠左的值) (ACM-ICPC 2018 南京赛区网络预赛 G)
G. Lpl and Energy-saving Lamps 42.07% 1000ms 65536K During tea-drinking, princess, amongst other t ...
- 计蒜客 31001 - Magical Girl Haze - [最短路][2018ICPC南京网络预赛L题]
题目链接:https://nanti.jisuanke.com/t/31001 题意: 一带权有向图,有 n 个节点编号1~n,m条有向边,现在一人从节点 1 出发,他有最多 k 次机会施展魔法使得某 ...
- 计蒜客 30999 - Sum - [找规律+线性筛][2018ICPC南京网络预赛J题]
题目链接:https://nanti.jisuanke.com/t/30999 样例输入258 样例输出814 题意: squarefree数是指不含有完全平方数( 1 除外)因子的数, 现在一个数字 ...
- 计蒜客 30994 - AC Challenge - [状压DP][2018ICPC南京网络预赛E题]
题目链接:https://nanti.jisuanke.com/t/30994 样例输入: 5 5 6 0 4 5 1 1 3 4 1 2 2 3 1 3 1 2 1 4 样例输出: 55 样例输入: ...
- 计蒜客 30990 - An Olympian Math Problem - [简单数学题][2018ICPC南京网络预赛A题]
题目链接:https://nanti.jisuanke.com/t/30990 Alice, a student of grade 6, is thinking about an Olympian M ...
- 计蒜客 31453 - Hard to prepare - [递归][2018ICPC徐州网络预赛A题]
题目链接:https://nanti.jisuanke.com/t/31453 After Incident, a feast is usually held in Hakurei Shrine. T ...
- 计蒜客 31447 - Fantastic Graph - [有源汇上下界可行流][2018ICPC沈阳网络预赛F题]
题目链接:https://nanti.jisuanke.com/t/31447 "Oh, There is a bipartite graph.""Make it Fan ...
随机推荐
- NYOJ 116 士兵杀敌 (线段树,区间和)
题目链接:NYOJ 116 士兵杀敌 士兵杀敌(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描写叙述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的 ...
- ubuntu 上安装vnc server
Ubuntu下设置VNCServer Virtual Network Computing(VNC)是进行远程桌面控制的一个软件.客户端的键盘输入和鼠标操作通过网络传输到远程服务器,控制服务器的操作 ...
- SaltStack 批量执行脚本
这里演示如何使用 salt-master 对多台 salt-minion 批量执行脚本,步骤如下: [root@localhost ~]$ cat /srv/salt/top.sls # 先定义入口配 ...
- 【译】Apache Flink Kafka consumer
Flink提供了Kafka connector用于消费/生产Apache Kafka topic的数据.Flink的Kafka consumer集成了checkpoint机制以提供精确一次的处理语义. ...
- RabbitMQ之总结
P:生成者,消息产生者: C:消息消费者: 红:消息队列: java实现 步骤: 创建连接 从连接中创建通道(相当于JDBC中的Statement) 通过channel声明(创建)队列.(如果队列存在 ...
- JavaScript Promise迷你书(中文版)
最近,发现了一个很不错的关于Promise介绍的迷你电子版书,分享给大家: http://liubin.org/promises-book/#chapter4-advanced-promise (篇幅 ...
- $ cd `dirname $0` 和PWD用法
在命令行状态下单纯执行 $ cd `dirname $0` 是毫无意义的.因为他返回当前路径的".".这个命令写在脚本文件里才有作用,他返回这个脚本文件放置的目录,并可以根据这 ...
- CSS-筛选 获取第一个td
tr td:first-child{ font-weight:bold; } 看样子,应该是jquery中有些筛选,css也是能够同样进行筛选的,只是模式有些可能不同 a[data-toggle*=' ...
- php原生实现图片上传和查看
先上源码:upload_file.php <html> <body> <form action="upload_file.php" method=&q ...
- 【架构师之路】Nginx负载均衡与反向代理—《亿级流量网站架构核心技术》
本篇摘自<亿级流量网站架构核心技术>第二章 Nginx负载均衡与反向代理 部分内容. 当我们的应用单实例不能支撑用户请求时,此时就需要扩容,从一台服务器扩容到两台.几十台.几百台.然而,用 ...