ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树

目录

ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树

题面

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\)) — 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_nk (1 \le k_j \le 10000, j = 1, 2, ..., n)$ — the number of lamps in the rooms of the Castle. The number in position jj is 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 q integers$ d_1, d_2, ..., d_q $
\((1 \le d_p \le 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 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 \(d_p\)month.

题意

这是道阅读题。。。n间房子,每间若干灯,每个月会买m盏灯。每个月会按顺序扫过去,如果手中的灯数大于房间的灯数,就更换整间房间的灯。然后删除。问第i个月。更换过多少间房间,手中剩下多少灯。

思路

线段树如果左区间存在可选的点,就递归左边。维护最小值判断是否存在点。然后递归右边。预处理每月的答案,然后询问的时候输出。
#include <bits/stdc++.h>

using namespace std;
const int MAXN = 1e5 + 10;
const int INF = 0x7FFFFFFF;

int n, m, arr[MAXN], ans[MAXN][2], sum, cnt;

struct Node {
    int l, r, mi, siz;
} tree[MAXN * 4];

void push_up(int rt) {
    tree[rt].mi = min(tree[rt << 1].mi, tree[rt << 1 | 1].mi);
}

void build(int rt, int l, int r) {
    tree[rt].l = l;
    tree[rt].r = r;
    if(l == r) {
        tree[rt].mi = arr[l];
        return ;
    }
    int mid = (l + r) >> 1;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid + 1, r);
    push_up(rt);
}

void update(int rt, int l, int r) {
    if(tree[rt].l == l && tree[rt].r == r && l == r) {
        if(sum >= tree[rt].mi) {
            cnt++;
            sum -= tree[rt].mi;
            tree[rt].mi = INF;
        }
        return ;
    }
    int mid = (tree[rt].l + tree[rt].r) >> 1;
    if(sum >= tree[rt << 1].mi) {
        update(rt << 1, l, mid);
    }
    if(sum >= tree[rt << 1 | 1].mi) {
        update(rt << 1 | 1, mid + 1, r);
    }
    push_up(rt);
}

int main()
{
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++ ) {
        scanf("%d", &arr[i]);
    }
    build(1, 1, n);
    sum = m;
    cnt = 0;
    int first = 1;
    for(int i = 1; i < MAXN; i++ ) {
        update(1, 1, n);
        if(cnt == n && !first) {
            ans[i][0] = ans[i - 1][0];
            ans[i][1] = ans[i - 1][1];
            continue;
        }
        if(cnt == n && first) {
            first = 0;
        }
        ans[i][0] = cnt;
        ans[i][1] = sum;
        sum += m;
    }
    int q;
    scanf("%d", &q);
    while(q--) {
        int d;
        scanf("%d", &d);
        printf("%d %d\n", ans[d][0], ans[d][1]);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Q1143316492/p/9571588.html