码迷,mamicode.com
首页 > 其他好文 > 详细

计蒜客 Lpl and Energy-saving Lamps(二分+线段树区间最小)

时间:2018-09-04 23:32:05      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:always   void   lis   gre   cut   cal   tag   for   str   

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 mmm 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 n and 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 nnn integers k1,k2,...,kn
(1≤kj≤10000,j=1,2,...,n) — the number of lamps in the rooms of the Castle. The number in position j is the number of lamps in j-th room. Room numbers are given in accordance with Lpl‘s list.
The third line contains one integer q(1≤q≤100000) — the number of queries.
The fourth line contains q integers d1,d2,...,dq
(1≤dp≤100000,p=1,2,...,q)— numbers of months, in which queries are formed.
Months are numbered starting with 1; at the beginning of the first month Lpl buys the first m energy-saving lamps.

Output
Print q lines.
Line p 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 dp? month.

Hint
Explanation for the sample:
In the first month, he bought 4 energy-saving lamps and he replaced the first room in his list and remove it. And then he had 1 energy-saving lamps and skipped all rooms next. So, the answer for the first month is 1,1??????1 room‘s lamps were replaced already, 1 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

题意

城堡里有n个房间每个房间里有ki盏旧灯,Lpl每天购买m盏灯,他会按顺序,如果他的新灯>=房间的旧灯,他会拿新灯替换所有旧灯,直到不能替换,q个询问,每个询问输出两个数字分别为当前月有几个房间已经替换过了,还剩下几盏灯

题解

可以预处理出所有查询,每个查询循环1-n判断能否替换,显然是不行的

可以考虑每次找到可替换的最左边,显然可以用线段树维护区间最小值

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn=1e5+5;
 5 int minn[maxn<<2];
 6 void build(int l,int r,int rt)
 7 {
 8     if(l==r)
 9     {
10         scanf("%d",&minn[rt]);
11         return;
12     }
13     int mid=(l+r)>>1;
14     build(l,mid,rt<<1);
15     build(mid+1,r,rt<<1|1);
16     minn[rt]=min(minn[rt<<1],minn[rt<<1|1]);
17 }
18 int query(int L,int R,int l,int r,int rt)
19 {
20     if(L<=l&&r<=R)
21         return minn[rt];
22     int mid=(l+r)>>1,ans=1e9;
23     if(L<=mid)ans=min(ans,query(L,R,l,mid,rt<<1));
24     if(R>mid)ans=min(ans,query(L,R,mid+1,r,rt<<1|1));
25     return ans;
26 }
27 
28 int n,m,q;
29 int La,Fr,Left;
30 int f[maxn],r[maxn];
31 void update(int L,int R,int l,int r,int rt)
32 {
33     if(L>r||R<l)return;
34     if(L<=l&&r<=R&&minn[rt]>La)return;
35     if(l==r)
36     {
37         Fr++;
38         La-=minn[rt];
39         Left=l;
40         minn[rt]=0x3f3f3f3f;
41         return;
42     }
43     int mid=(l+r)>>1;
44     if(L<=mid)update(L,R,l,mid,rt<<1);
45     if(R>mid)update(L,R,mid+1,r,rt<<1|1);
46     minn[rt]=min(minn[rt<<1],minn[rt<<1|1]);
47 }
48 int main()
49 {
50     scanf("%d%d",&n,&m);
51     build(1,n,1);
52 
53     for(int i=1;i<=100000;i++)
54     {
55         if(Fr==n)
56         {
57             f[i]=Fr;
58             r[i]=La;
59             continue;
60         }
61         La+=m;
62         Left=1;
63         while(query(Left,n,1,n,1)<=La)update(Left,n,1,n,1);
64         f[i]=Fr;
65         r[i]=La;
66     }
67 
68     scanf("%d",&q);
69     for(int i=0,x;i<q;i++)
70     {
71         scanf("%d",&x);
72         printf("%d %d\n",f[x],r[x]);
73     }
74     return 0;
75 }

 

计蒜客 Lpl and Energy-saving Lamps(二分+线段树区间最小)

标签:always   void   lis   gre   cut   cal   tag   for   str   

原文地址:https://www.cnblogs.com/taozi1115402474/p/9589028.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!