题目链接: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题]的更多相关文章

  1. 计蒜客 31459 - Trace - [线段树][2018ICPC徐州网络预赛G题]

    题目链接:https://nanti.jisuanke.com/t/31459 样例输入 3 1 4 4 1 3 3 样例输出 10 题意: 二维平面上给出 $n$ 个点,每个点坐标 $\left( ...

  2. 计蒜客 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 ...

  3. 计蒜客 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 ...

  4. 计蒜客 31001 - Magical Girl Haze - [最短路][2018ICPC南京网络预赛L题]

    题目链接:https://nanti.jisuanke.com/t/31001 题意: 一带权有向图,有 n 个节点编号1~n,m条有向边,现在一人从节点 1 出发,他有最多 k 次机会施展魔法使得某 ...

  5. 计蒜客 30999 - Sum - [找规律+线性筛][2018ICPC南京网络预赛J题]

    题目链接:https://nanti.jisuanke.com/t/30999 样例输入258 样例输出814 题意: squarefree数是指不含有完全平方数( 1 除外)因子的数, 现在一个数字 ...

  6. 计蒜客 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 样例输入: ...

  7. 计蒜客 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 ...

  8. 计蒜客 31453 - Hard to prepare - [递归][2018ICPC徐州网络预赛A题]

    题目链接:https://nanti.jisuanke.com/t/31453 After Incident, a feast is usually held in Hakurei Shrine. T ...

  9. 计蒜客 31447 - Fantastic Graph - [有源汇上下界可行流][2018ICPC沈阳网络预赛F题]

    题目链接:https://nanti.jisuanke.com/t/31447 "Oh, There is a bipartite graph.""Make it Fan ...

随机推荐

  1. Js页面刷新前提示-jquery页面刷新事件

    //原理很简单,就是在body的onbeforeunload事件绑定函数,代码如下: document.body.onbeforeunload = function (event) { var c = ...

  2. Objective-C 协议和运行时检查方法、类是否存在

    协议的声明: // // Person.h // TestOC01 // // Created by xinye on 13-10-23. // Copyright (c) 2013年 xinye. ...

  3. 禁用ViewPager左右两侧拉到边界的渐变颜色

    Android ViewPager在拖拽到左边和右边的时候,禁止显示黄色或者蓝色的渐变图片的解决方法 先说明哦,想看看院里的,从头开始看,否则,就拉到最下面啦.解决方案就在最下面. 修改前: 修改后: ...

  4. Dubbo -- 系统学习 笔记 -- 示例 -- 启动时检查

    示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 启动时检查 Dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成,以便上线时,能及早发 ...

  5. graphicsmagick常用命令

    显示图像文件详细信息 gm identify a.jpg 1.更改当前目录下.jpg的尺寸大小,并保存于目录.thumb里面 gm mogrify -output-directory .thumbs ...

  6. mongodb int字段的一个小坑

    在使用 php mongodb 搜索时,如果字段类型用 int,则使用 php 搜索时一定要把数值转换成整型来搜索,用字符串类型的数字搜索是没有结果的!!!! $condition = ['membe ...

  7. Nginx(十二)-- Nginx+keepalived实现高可用

    1.前提 两台Linux服务器,IP分别为192.168.80.128 和 192.168.80.129,都安装Nginx和keepalived,并启动. 2.配置双机热备 1.将192.168.80 ...

  8. input回车问题

    今天有一个问题,就是input对象没有加任何事件自己回车导致跳到了404页面.处理的时候,并发现没找到回车事件的控制. 那么只有一种情况,就是自带的回车控制. 百度了一下,如下面博文里面的写法.我这边 ...

  9. SNAT DNAT MASQUERADE 区别

    SNAT,DNAT,MASQUERADE都是NATMASQUERADE是SNAT的一个特例SNAT是指在数据包从网卡发送出去的时候,把数据包中的源地址部分替换为指定的IP,这样,接收方就认为数据包的来 ...

  10. JS 验证URL

    var strVal = $("#urlText").val(); var Expression = "^((https|http|ftp|rtsp|mms)?://)& ...