博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
计蒜客 30996.Lpl and Energy-saving Lamps-线段树(区间满足条件最靠左的值) (ACM-ICPC 2018 南京赛区网络预赛 G)...
阅读量:5132 次
发布时间:2019-06-13

本文共 5810 字,大约阅读时间需要 19 分钟。

  • 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(1n100000,1m100) — 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)(1kj10000,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(1q100000) — 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)(1dp100000,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,11 room's lamps were replaced already, 11 energy-saving lamp remain.

样例输入复制

5 43 10 5 2 7105 1 4 8 7 2 3 6 4 7

样例输出复制

4 01 13 65 15 12 03 24 43 65 1

题目来源

 

 

题意就是换灯泡,从头走到尾,满足条件的就更换灯泡,直到灯泡没了或者走到尾了,每个月都是这样,然后给你一堆查询。

因为是找最靠左的满足条件的区间,所以线段树找区间满足条件的最靠左的数就可以,然后单点更新就没了。

 

这个题有毒,真的,我感觉数据都有问题,比赛时队友过的代码样例都没过,交了就过了,但是我按照自己理解的题意写的样例过了,但是交了wa了,然后把需要操作的月份结合了一下我和队友的思路,找一下需要月份的最小值才过的,吐槽这道题,不好玩(༼༎ຶᴗ༎ຶ༽)

 

代码:

1 //G-线段树  2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include
20 using namespace std; 21 typedef long long ll; 22 23 const double PI=acos(-1.0); 24 const double eps=1e-6; 25 const ll mod=1e9+7; 26 const int inf=0x3f3f3f3f; 27 const int maxn=1e5+10; 28 const int maxm=100+10; 29 #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 30 #define lson l,m,rt<<1 31 #define rson m+1,r,rt<<1|1 32 33 int tree[maxn<<2]; 34 35 void pushup(int rt) 36 { 37 tree[rt]=min(tree[rt<<1],tree[rt<<1|1]); 38 } 39 40 void update(int p,int val,int l,int r,int rt) 41 { 42 if(l==r){ 43 tree[rt]=val; 44 return ; 45 } 46 47 int m=(l+r)>>1; 48 if(p<=m) update(p,val,lson); 49 else update(p,val,rson); 50 pushup(rt); 51 } 52 53 int query(int val,int l,int r,int rt) 54 { 55 if(l==r){ 56 return l; 57 } 58 59 int m=(l+r)>>1; 60 if(tree[rt<<1] <=val) return query(val,lson);//尽量往左找 61 if(tree[rt<<1|1]<=val) return query(val,rson); 62 return -1; 63 } 64 65 int a[maxn],op[maxn],ans[maxn],over[maxn]; 66 67 int main() 68 { 69 int n,k; 70 scanf("%d%d",&n,&k); 71 ll sum=0; 72 for(int i=1;i<=n;i++){ 73 scanf("%d",&a[i]); 74 sum+=a[i]; 75 update(i,a[i],1,n,1); 76 } 77 int m=sum/k+(sum%k==0?0:1);//实际上最多需要买灯泡的月份 78 int q,mx=-1; 79 scanf("%d",&q); 80 for(int i=1;i<=q;i++){ 81 scanf("%d",&op[i]); 82 mx=max(mx,op[i]);//查询里最多需要的月份 83 } 84 ll ret=0; 85 int cnt=0; 86 for(int i=1;i<=min(mx,m);i++){ //取最小即可 87 ret+=k; 88 while(1){ 89 int pos=query(ret,1,n,1); 90 if(pos!=-1){ 91 cnt++;ret-=a[pos]; 92 update(pos,inf,1,n,1); 93 ans[i]=cnt;over[i]=ret; 94 } 95 else{ 96 ans[i]=cnt;over[i]=ret; 97 break; 98 } 99 }100 }101 for(int i=1;i<=q;i++){102 if(op[i]<=m)103 printf("%d %d\n",ans[op[i]],over[op[i]]);104 else105 printf("%d %d\n",ans[m],over[m]);//如果查询的月份比实际换完需要的月份大,直接输出换完时的数据106 }107 }

 

转载于:https://www.cnblogs.com/ZERO-/p/9641733.html

你可能感兴趣的文章
npm 常用指令
查看>>
20几个正则常用正则表达式
查看>>
TextArea中定位光标位置
查看>>
非常棒的Visual Studo调试插件:OzCode 2.0 下载地址
查看>>
判断字符串在字符串中
查看>>
hdu4374One hundred layer (DP+单调队列)
查看>>
类间关系总结
查看>>
properties配置文件读写,追加
查看>>
Linux环境下MySql安装和常见问题的解决
查看>>
lrzsz——一款好用的文件互传工具
查看>>
ZPL语言完成条形码的打印
查看>>
这20件事千万不要对自己做!
查看>>
Linux环境下Redis安装和常见问题的解决
查看>>
玩转小程序之文件读写
查看>>
HashPump用法
查看>>
cuda基础
查看>>
virutalenv一次行安装多个requirements里的文件
查看>>
Vue安装准备工作
查看>>
.NET 母版页 讲解
查看>>
Android Bitmap 和 Canvas详解
查看>>