2018-ACM-ICPC-Nanjing-online-G

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

题目链接:https://nanti.jisuanke.com/t/30996

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 mmenergy-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(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 nn integers k_1, k_2, …, k_nk1,k2,…,kn
(1 \le k_j \le 10000, j = 1, 2, …, n)(1≤kj≤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 qq integers d_1, d_2, …, d_qd1,d2,…,dq
(1 \le d_p \le 100000, p = 1, 2, …, q)(1≤dp≤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 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,1−−−−−−1 room’s lamps were replaced already, 11 energy-saving lamp remain.

样例输入复制

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

样例输出复制

1
2
3
4
5
6
7
8
9
10
4 0
1 1
3 6
5 1
5 1
2 0
3 2
4 4
3 6
5 1

题目来源

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

首先认真读题。 其实发现模拟一下就是每次从左往右找到一个不大于某个值的数,然后干掉。

这个题目范围还比较水。就是暴力枚举每个月,然后每个月找满足条件的数的时候用线段树维护一下。

线段树水题!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100010;
struct Node {
int l,r;
int mi;
}segTree[MAXN*4];

int a[MAXN];

void push_up(int i) {
if (segTree[i].l == segTree[i].r)
return;
if (a[segTree[i<<1].mi] > a[segTree[(i<<1)|1].mi])
segTree[i].mi = segTree[(i<<1)|1].mi;
else segTree[i].mi = segTree[i<<1].mi;
}

int Index[MAXN];

void build(int i, int l, int r) {
segTree[i].l = l;
segTree[i].r = r;
if (l == r) {
segTree[i].mi = l;
Index[l] = i;
return;
}
int mid = (l + r)/2;
build(i<<1, l, mid);
build((i<<1)|1, mid+1, r);
push_up(i);
}

int query(int i, int l, int r, int val) {
if (l > r) return -1;
if (a[segTree[i].mi] > val) return -1;
if (l == segTree[i].l && r == segTree[i].l) {
if (a[segTree[i].mi] <= val) return segTree[i].mi;
else return -1;
}
int mid = (segTree[i].l + segTree[i].r)/2;
int li = -1;
int ri = -1;
if (l <= mid) {
li = query(i<<1, l, mid, val);
}
if (li != -1) return li;
if (r > mid) {
ri = query((i<<1)|1, mid+1, r, val);
}
if (ri != -1) return ri;
return -1;
}

const int INF = 0x3f3f3f3f;

void update(int i) {
a[i] = INF;
int tmp = Index[i];
while (tmp) {
push_up(tmp);
tmp /= 2;
}
}


int ans1[100010], ans2[100010];

int main() {
int n,m;
while(scanf("%d%d", &n, &m) == 2) {
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
build(1, 1, n);
int num1 = 0;
int val = 0;
for (int k = 1; k <= 100000; k++) {
val += m;
int now = 1;
while (1) {
int mi = query(1, now, n, val);
if (mi == -1)break;
val -= a[mi];
num1++;
update(mi);
now = mi+1;
}
ans1[k] = num1;
ans2[k] = val;
}
int Q;
scanf("%d", &Q);
int d;
while (Q--) {
scanf("%d", &d);
printf("%d %d\n", ans1[d], ans2[d]);
}
}
return 0;
}
------ 本文结束------

本文标题:2018-ACM-ICPC-Nanjing-online-G

文章作者:kuangbin

发布时间:2018年09月01日 - 21:58:01

最后更新:2018年09月01日 - 22:05:06

原始链接:http://kuangbin.github.io/2018/09/01/2018-ACM-ICPC-Nanjing-online-G/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%