目录

CF1394A-Boboniu Chats with Du

CF1394A-Boboniu Chats with Du

题目:

题目描述:

Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.

In Boboniu’s chat group, there’s a person called Du Yi who likes to make fun of Boboniu every day.

Du will chat in the group for $ n $ days. On the $ i $ -th day:

  • If Du can speak, he’ll make fun of Boboniu with fun factor $ a_i $ . But after that, he may be muzzled depending on Boboniu’s mood.
  • Otherwise, Du won’t do anything.

Boboniu’s mood is a constant $ m $ . On the $ i $ -th day:

  • If Du can speak and $ a_i>m $ , then Boboniu will be angry and muzzle him for $ d $ days, which means that Du won’t be able to speak on the $ i+1, i+2, \cdots, \min(i+d,n) $ -th days.
  • Otherwise, Boboniu won’t do anything.

The total fun factor is the sum of the fun factors on the days when Du can speak.

Du asked you to find the maximum total fun factor among all possible permutations of $ a $ .

输入格式:

The first line contains three integers $ n $ , $ d $ and $ m $ ( $ 1\le d\le n\le 10^5,0\le m\le 10^9 $ ).

The next line contains $ n $ integers $ a_1, a_2, \ldots,a_n $ ( $ 0\le a_i\le 10^9 $ ).

输出格式:

Print one integer: the maximum total fun factor among all permutations of $ a $ .

样例:

样例输入 1:

1
2
5 2 11
8 10 15 23 5

样例输出 1:

1
48

样例输入 2:

1
2
20 2 16
20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7

样例输出 2:

1
195

思路:

实现:

 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
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
long long a[maxn];
int n, d, m;
int tot1, tot2;
long long c[maxn], b[maxn];
long long sum[maxn << 1], ans;
priority_queue<pair<long long, long long>> q;
int main()
{
    cin >> n >> d >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        if (a[i] <= m)
            ans += a[i], b[++tot1] = a[i];
        else
            c[++tot2] = a[i];
    }
    sort(b + 1, b + tot1 + 1);
    sort(c + 1, c + tot2 + 1);
    for (int i = 1; i <= tot1; i++)
    {
        q.push(make_pair(-b[i], i));
    }
    for (int i = 1; i <= tot2; i++)
        q.push(make_pair(0, i));
    ans += c[tot2];
    q.pop();
    for (int i = tot2 - 1; i >= 1; i--)
    {
        if (q.size() < d + 1)
            break;
        long long x = 0;
        for (int j = 1; j <= d + 1; j++)
            x += q.top().first, q.pop();
        // cout << c[i] << " " << x << endl;
        if (c[i] > -x)
            ans += c[i] + x;
    }
    cout << ans << endl;
    return 0;
}