计蒜客 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 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.
Input
Each input will consist of a single test case.
The first line contains integers nn and m (1 \le n \le 100000, 1 \le m \le 100)m(1≤n≤100000,1≤m≤100) — the number of rooms in the Castle and the number of energy-saving lamps, which Lpl buys monthly.
The second line contains nn integers k_1, k_2, ..., k_nk1,k2,...,kn
(1 \le k_j \le 10000, j = 1, 2, ..., n)(1≤kj≤10000,j=1,2,...,n) — the number of lamps in the rooms of the Castle. The number in position jjis the number of lamps in jj-th room. Room numbers are given in accordance with Lpl's list.
The third line contains one integer q (1 \le q \le 100000)q(1≤q≤100000) — the number of queries.
The fourth line contains qq integers d_1, d_2, ..., d_qd1,d2,...,dq
(1 \le d_p \le 100000, p = 1, 2, ..., q)(1≤dp≤100000,p=1,2,...,q) — numbers of months, in which queries are formed.
Months are numbered starting with 11; at the beginning of the first month Lpl buys the first m energy-saving lamps.
Output
Print qq lines.
Line pp contains two integers — the number of rooms, in which all old lamps are replaced already, and the number of remaining energy-saving lamps by the end of d_pdp month.
Hint
Explanation for the sample:
In the first month, he bought 44 energy-saving lamps and he replaced the first room in his list and remove it. And then he had 11 energy-saving lamps and skipped all rooms next. So, the answer for the first month is 1,1------11,1−−−−−−1 room's lamps were replaced already, 11 energy-saving lamp remain.
样例输入复制
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
题目来源
题意就是换灯泡,从头走到尾,满足条件的就更换灯泡,直到灯泡没了或者走到尾了,每个月都是这样,然后给你一堆查询。
因为是找最靠左的满足条件的区间,所以线段树找区间满足条件的最靠左的数就可以,然后单点更新就没了。
这个题有毒,真的,我感觉数据都有问题,比赛时队友过的代码样例都没过,交了就过了,但是我按照自己理解的题意写的样例过了,但是交了wa了,然后把需要操作的月份结合了一下我和队友的思路,找一下需要月份的最小值才过的,吐槽这道题,不好玩(༼༎ຶᴗ༎ຶ༽)
代码:
//G-线段树
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 int tree[maxn<<]; void pushup(int rt)
{
tree[rt]=min(tree[rt<<],tree[rt<<|]);
} void update(int p,int val,int l,int r,int rt)
{
if(l==r){
tree[rt]=val;
return ;
} int m=(l+r)>>;
if(p<=m) update(p,val,lson);
else update(p,val,rson);
pushup(rt);
} int query(int val,int l,int r,int rt)
{
if(l==r){
return l;
} int m=(l+r)>>;
if(tree[rt<<] <=val) return query(val,lson);//尽量往左找
if(tree[rt<<|]<=val) return query(val,rson);
return -;
} int a[maxn],op[maxn],ans[maxn],over[maxn]; int main()
{
int n,k;
scanf("%d%d",&n,&k);
ll sum=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
sum+=a[i];
update(i,a[i],,n,);
}
int m=sum/k+(sum%k==?:);//实际上最多需要买灯泡的月份
int q,mx=-;
scanf("%d",&q);
for(int i=;i<=q;i++){
scanf("%d",&op[i]);
mx=max(mx,op[i]);//查询里最多需要的月份
}
ll ret=;
int cnt=;
for(int i=;i<=min(mx,m);i++){//取最小即可
ret+=k;
while(){
int pos=query(ret,,n,);
if(pos!=-){
cnt++;ret-=a[pos];
update(pos,inf,,n,);
ans[i]=cnt;over[i]=ret;
}
else{
ans[i]=cnt;over[i]=ret;
break;
}
}
}
for(int i=;i<=q;i++){
if(op[i]<=m)
printf("%d %d\n",ans[op[i]],over[op[i]]);
else
printf("%d %d\n",ans[m],over[m]);//如果查询的月份比实际换完需要的月份大,直接输出换完时的数据
}
}
计蒜客 30996.Lpl and Energy-saving Lamps-线段树(区间满足条件最靠左的值) (ACM-ICPC 2018 南京赛区网络预赛 G)的更多相关文章
- ACM-ICPC 2018 南京赛区网络预赛 G Lpl and Energy-saving Lamps(线段树)
题目链接:https://nanti.jisuanke.com/t/30996 中文题目: 在喝茶的过程中,公主,除其他外,问为什么这样一个善良可爱的龙在城堡里被监禁Lpl?龙神秘地笑了笑,回答说这是 ...
- ACM-ICPC 2018 南京赛区网络预赛 G Lpl and Energy-saving Lamps(模拟+线段树)
https://nanti.jisuanke.com/t/30996 题意 每天增加m个灯泡,n个房间,能一次性换就换,模拟换灯泡过程.询问第几天的状态 分析 离线做,按题意模拟.比赛时线段树写挫了. ...
- ACM-ICPC 2018 南京赛区网络预赛 G. Lpl and Energy-saving Lamps(二分+线段树区间最小)
During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Drag ...
- ACM-ICPC 2018 南京赛区网络预赛 G. Lpl and Energy-saving Lamps (弱线段树)
线段树节点维护区间最小值,查找时优先从左侧的区间寻找. 每一次循环都在树中不停寻找第一个小于等于当前持有数的值,然后抹去,直到找不到为止. #include<cstdio> #includ ...
- 计蒜客 30996 - Lpl and Energy-saving Lamps - [线段树][2018ICPC南京网络预赛G题]
题目链接:https://nanti.jisuanke.com/t/30996 During tea-drinking, princess, amongst other things, asked w ...
- 计蒜客 30999.Sum-筛无平方因数的数 (ACM-ICPC 2018 南京赛区网络预赛 J)
J. Sum 26.87% 1000ms 512000K A square-free integer is an integer which is indivisible by any squar ...
- 计蒜客 30990.An Olympian Math Problem-数学公式题 (ACM-ICPC 2018 南京赛区网络预赛 A)
A. An Olympian Math Problem 54.28% 1000ms 65536K Alice, a student of grade 66, is thinking about a ...
- 计蒜客 28437.Big brother said the calculation-线段树+二分-当前第k个位置的数 ( ACM训练联盟周赛 M)
M. Big brother said the calculation 通过线段树维护. 这个题和杭电的一道题几乎就是一样的题目.HDU5649.DZY Loves Sorting 题意就是一个n的排 ...
- ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps (线段树:无序数组找到第一个小于val)
题意:n个房间,每个房间有ai盏旧灯,每个月可以买m盏新灯,要求:按房间顺序换灯,如果剩下的新灯数目大于ai,那么进行更换,否则跳过该房间,判断下一个房间.如果所有房间都换完灯,那么久不会再买新灯. ...
随机推荐
- regex & form validation & phone
regex & form validation https://regexper.com/ https://gitlab.com/javallone/regexper-static https ...
- NET 的 ELK 监控方案
NET 的 ELK 监控方案 https://www.jianshu.com/p/3c26695cfc38 背景就不多说了,谁家没有个几个十系统在跑啊.如何监控这几十个系统的运行状况,对于非运营人员来 ...
- poj 1034 The dog task (二分匹配)
The dog task Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2559 Accepted: 1038 Sp ...
- P4754 True Vegetable
题目描述 小A现在有 NN 道题,编号为 1,2,\cdots,N1,2,⋯,N .每道题的起始毒瘤程度为 00 或 11 .在每回合,小A可以将编号连续的 KK 道题的毒瘤程度+1.但小B因为本身比 ...
- Hibernate技术入门
ORM原理 ORM(Object Relational Mapping)是对象到关系的映射,它的作用是在关系数据库和对象之间做一个自动映射,将数据库中的数据表映射成对象(持久化类),对关系型数据库以对 ...
- JS容易理解错误的地方
在这端代码执行的末尾,你会不会hi变量回事函数中的hi了?你会不会认为这不是按引用传递了? 对值传递和引用传递产生质疑了? 1 var hi = {}; 2 function sayHello(hi) ...
- [Leetcode] Merge two sorted lists 合并两已排序的链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- BZOJ4567 [Scoi2016]背单词 【trie树 + 贪心】
题目链接 BZOJ4567 题解 题意真是鬼畜= = 意思就是说我们应先将一个串的所有后缀都插入之后再插入这个串,产生代价为其到上一个后缀的距离 我们翻转一下串,转化为前缀,就可以建\(trie\)树 ...
- gogole调试请求体的数据怎么知道
在network---->header->request payload中看 详细情况见下图所示:
- javascript实现倒计时(转)
<html> <head> <title>倒计时</title> <meta charset="utf-8"> < ...