Views Matter
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 (1≤n≤1000001≤n≤100000, 1≤m≤1091≤m≤109) — the number of stacks and the height of the exhibit.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤m1≤ai≤m) — the number of blocks in each stack from left to right.
OutputPrint exactly one integer — the maximum number of blocks that can be removed.
Examples input5 6output
3 3 3 3 3
10input
3 5output
1 2 4
3input
5 5output
2 3 1 4 4
9input
1 1000output
548
0input
3 3output
3 1 1
1Note
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 }