计蒜客 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 ...
随机推荐
- ios开发之--tableview刷新某一个区和某一行
在开发中,有时候,我们不需要刷新整个表,只需要刷新局部数据即可,具体代码如下: //section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWith ...
- mysql 字符串转数据丢失精度,mysql转换丢失精度,mysql CAST 丢失精度
mysql 字符串转数据丢失精度,mysql转换丢失精度,mysql CAST 丢失精度 =============================== ©Copyright 蕃薯耀 2017年9月1 ...
- PostgreSQL存储过程(1)-基于SQL的存储过程
什么是SQL函数? SQL函数包体是一些可执行的SQL语言.同时包含1条以上的查询,但是函数只返回最后一个查询(必须是SELECT)的结果. 除非SQL函数声明为返回void,否则最后一条语句必须是S ...
- MHL相关资源链接
http://www.mhlconsortium.org/ 消费者网站: www.meetmhl.com采用者网站:www.mhltech.org 博客http://blog.sina.com.cn/ ...
- AddComponentRecursively
class AddComponentRecursively extends ScriptableWizard { var componentName : String = ""; ...
- codeforces水题100道 第八题 Codeforces Round #274 (Div. 2) A. Expression (math)
题目链接:http://www.codeforces.com/problemset/problem/479/A题意:给你三个数a,b,c,使用+,*,()使得表达式的值最大.C++代码: #inclu ...
- secureCRT使用退格键(backspace)出现^H解决办法
解决办法步骤如下: 选项--->会话选项---> 把下面两个打个钩就行了. 原文地址:http://skykiss.blog.51cto.com/blog/2892603/769771 另 ...
- xp下对dinput8.dll 游戏键盘输入的模拟 非函数hook
https://www.xuebuyuan.com/833929.html 很多游戏或者3d模拟软件为了更好的支持外设使用directinput作为输入接口调用.那么如果要模拟鼠标或键盘来控制游戏或者 ...
- VC++进行窗口枚举
https://blog.csdn.net/u012372584/article/details/53735242 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- xmlWriter
MemoryStream msXml = new MemoryStream(); XmlTextWriter xmlWriter = new XmlTextWriter(msXml, Encoding ...