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

目录

  • 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;
}

原文地址:https://www.cnblogs.com/Q1143316492/p/9571588.html

时间: 2024-08-01 18:17:53

ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树的相关文章

ACM-ICPC 2018 南京赛区网络预赛 E题

ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest with n (0 < n \le 20)n(0<n≤20) problems. And he knows the answer of all of these problems. However, he can submit ii-th problem if and only if he has s

ACM-ICPC 2018 南京赛区网络预赛 J.Sum

Sum A square-free integer is an integer which is indivisible by any square number except 11. For example, 6 = 2 \cdot 36=2⋅3 is square-free, but 12 = 2^2 \cdot 312=22⋅3 is not, because 2^222 is a square number. Some integers could be decomposed into

ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze

262144K There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance c_ici?. Haze is a Magical Girl that lives in City 11, she can choose no more than KK roads and make their distances

计蒜客 ACM-ICPC 2018 南京赛区网络预赛 A. An Olympian Math Problem-数学公式题

A. An Olympian Math Problem 54.28% 1000ms 65536K Alice, a student of grade 66, is thinking about an Olympian Math problem, but she feels so despair that she cries. And her classmate, Bob, has no idea about the problem. Thus he wants you to help him.

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个房间,能一次性换就换,模拟换灯泡过程.询问第几天的状态 分析 离线做,按题意模拟.比赛时线段树写挫了..导致不断超时,我太弱了.每次询问符合要求的最左边的点. #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #i

ACM-ICPC 2018 南京赛区网络预赛

A An Olympian Math Problem #include <bits/stdc++.h> using namespace std; typedef long long ll; ll T,n; int main(){ scanf("%lld",&T); while(T--){ scanf("%lld",&n); printf("%lld\n",n-1); } } B The writing on the w

线性素数筛 ACM-ICPC 2018 南京赛区网络预赛 J Sum

https://www.jisuanke.com/contest/1555?view=challenges 题意: 题解:写完都没发现是个积性函数233 想法就是对x分解质因数,f(x)就是2^k,其中k是x分解结果中次数为一的质因子个数.如果有某个次数大于等于3,f(x)==0; 这样明显会TLE 所以就想一个递推的方法. 于是魔改了一下线性筛. #include<iostream> #include<cstdlib> #include<cstdio> #includ

ACM-ICPC 2018 南京赛区网络预赛 - C GDY (模拟)

诶,比赛时差一点就debug出来了 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int mod = 14; map<int,int> vz[205]; LL ans[205]; int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); freopen("out.tx