ACM-ICPC 2018 南京赛区网络预赛G. Lpl and Energy-saving Lamps(线段树查找第一个小于k的位置)【好题】

版权声明:欢迎转载 https://blog.csdn.net/l18339702017/article/details/82292001

G-Lpl and Energy-saving Lamps

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536K 
Problem Description 
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 energysaving 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 m 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 energysaving 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 n 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. 
Sample Input 
5 4 3 10 5 2 7 10 5 1 4 8 7 2 3 6 4 7 
Sample Output 
4 0 1 1 3 6 5 1 5 1 2 0 3 2 4 4 3 6 5 1 
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. 
 

题目大意:

有n个房间,每个房间里有ai个灯泡,lpl每个月月初有权购买m个灯泡,他从1号房间开始安装灯泡,如果手里的灯泡数比当前房间所需灯泡数 多,就把这个房间的灯泡安装上,否则查看下一个房间。

有q次询问,每次询问在第k个月的时候,已经完成了多少个房间的安装,与手里还有多少个灯泡。

 

题目思路:

我们利用线段树,查找每次更新的房间号,可以优化掉大部分时间。再利用线段树的修改把已经安装好的房间号里的灯泡数更新成INF即可。

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define line cout<<"-----------------"<<endl;

typedef long long ll;
const int maxn = 1e5+10;
const int MAXN = 1e6+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int N = 1010;

int n, m, q;
int a[maxn], ans[maxn], ans1[maxn],lig[maxn];
struct node{
    int l, r, minn;
    int mid(){
        return (l + r) >> 1; 
    }
}tree[maxn << 2];
void pushup(int x){
    tree[x].minn = min(tree[x<<1].minn, tree[x<<1|1].minn);
}
void build(int x, int l, int r){
    tree[x].l = l; tree[x].r = r;
    if(l == r) {
        tree[x].minn = a[l];
        return;
    }
    int mid = tree[x].mid();
    build(x<<1, l, mid);
    build(x<<1|1, mid+1, r);
    pushup(x);
}
void update(int x, int pos, int val){
    if(tree[x].l == tree[x].r){
        tree[x].minn = val;
        return ;
    }
    int mid = tree[x].mid();
    if(pos <= mid) update(x<<1, pos, val);
    else if(pos > mid) update(x<<1|1, pos, val);
    pushup(x);
}
int query(int x, int l, int r, int val){
    if(tree[x].l == tree[x].r){
        if(tree[x].minn <= val) return tree[x].l;
        else return -1;
    }
    if(tree[x].l >= l && tree[x].r <= r){
        if(tree[x].minn > val) return -1;
    }
    int mid = tree[x].mid();
    if(r <= mid) return query(x<<1, l, r, val);
    else if(l > mid) return query(x<<1|1, l, r, val);
    else{
        int temp = query(x<<1, l, mid, val);
        if(temp != -1) return temp;
        else return query(x<<1|1, mid+1, r, val);
    }
}
int main(){
    scanf("%d%d", &n, &m);
    for(int i=1; i<=n; i++) scanf("%d", &a[i]);
    build(1, 1, n);
    int maxx = 0;
    scanf("%d", &q);
    for(int i=1;i<=q;i++){
        scanf("%d", &lig[i]);
        if(lig[i] > maxx) maxx = lig[i];
    }
    int have = 0, num = 0;
    for(int i=1; i<= maxx; i++){
        if(num != n) have += m;
        int pos = query(1, 1, n, have);
        int cnt = 0;
        while(pos != -1){
            num ++;//已经换号的房间数量
            cnt ++;//这一轮更换的房间数
            have -= a[pos];//当前手里的灯泡数
            update(1, pos, INF);
            pos = query(1, 1, n, have);//再次查找
        }
        //保存本轮数据
        ans[i] = ans[i-1] + cnt;
        ans1[i] = have;
    }
    for(int i=1; i<=q; i++)
        printf("%d %d\n", ans[lig[i]], ans1[lig[i]]);
    return 0;
}


 

猜你喜欢

转载自blog.csdn.net/l18339702017/article/details/82292001