CodeForces - 1061B - Views Matter (贪心)

 Views Matter

 CodeForces - 1061B

Time limit 2000 ms Memory limit 262144 kB

 

Problem Description

You came to the exhibition and one exhibit has drawn your attention. It consists of nn stacks of blocks, where the ii-th stack consists of aiaiblocks resting on the surface.

The height of the exhibit is equal to mm. Consequently, the number of blocks in each stack is less than or equal to mm.

There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.

Find the maximum number of blocks you can remove such that the views for both the cameras would not change.

Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either.

Input

The first line contains two integers nn and mm (1n1000001≤n≤100000, 1m1091≤m≤109) — the number of stacks and the height of the exhibit.

The second line contains nn integers a1,a2,,ana1,a2,…,an (1aim1≤ai≤m) — the number of blocks in each stack from left to right.

 
Output

Print exactly one integer — the maximum number of blocks that can be removed.

 
Examples
input
5 6
3 3 3 3 3
output
10
input
3 5
1 2 4
output
3
input
5 5
2 3 1 4 4
output
9
input
1 1000
548
output
0
input
3 3
3 1 1
output
1
Note

The following pictures illustrate the first example and its possible solution.

Blue cells indicate removed blocks. There are 1010 blue cells, so the answer is 1010.

Solution:

要求 俯视图不变(列不变),右视图不变(高度不变),情况下删最多的方块,对立面思考下,保留最少的方块,那我至少得保留列块,行的话能少就少,怎么说呢,

就是尽量让每一列贡献新的一行出来(高度参差不齐,可能一列贡献不出新的行),先排序下,然后对于每一列我仅考虑能否新的top(最高行)就行了,更新下sum+=a[i]-1,然后能更新top就++top,最后一列的时候不管怎么样top都要=这个最高的高度了,sum+=min(top,a[i]-1);

 1 #include <cstdio>
 2 #include <algorithm>
 3 
 4 using namespace std;
 5 
 6 typedef long long ll;
 7 
 8 ll da[100010];
 9 
10 int main() {
11     int n, m;
12     scanf("%d%d", &n, &m);
13     for (int i = 1; i <= n; ++i) scanf("%lld", &da[i]);
14     sort(da + 1, da + 1 + n);
15     ll top = 0;
16     ll sum = 0;
17     for (int i = 1; i < n; ++i) {
18         if (da[i] > top) ++top;
19         sum += da[i] - 1;
20     }
21     sum += min(top,da[n]-1);
22     printf("%lld\n", sum);
23 }

猜你喜欢

转载自www.cnblogs.com/SayGB/p/10397404.html