For the grand opening of the algorithmic games in NlogNsglow, a row of tower blocks is set to be demolished in a grand demonstration of renewal. Originally the plan was to accomplish this with controlled explosions, one for each tower block, but time constraints now require a hastier solution.
To help you remove the blocks more rapidly you have been given the use of a Universal Kinetic / Incandescent Energy Particle Cannon (UKIEPC). On a single charge, this cutting-edge contraption can remove either all of the floors in a single tower block, or all the x-th floors in all the blocks simultaneously, for user’s choice of the floor number x. In the latter case, the blocks that are less than x floors high are left untouched, while for blocks having more than x floors, all the floors above the removed x-th one fall down by one level.
Task
Given the number of floors of all towers, output the minimum number of charges needed to eliminate all floors of all blocks.
Input
The first line of input contains the number of blocks n, where 2 ≤ n ≤ 100 000. The second line contains n consecutive block heights hi for i = 1, 2, . . . , n, where 1 ≤ hi ≤ 1 000 000.
Output
Output one line containing one integer: the minimum number of charges needed to tear down all the blocks.
题意:有n个物体,每个物体高hi ,现在我们可以进行lia两种操作
1: 对所有物体的高度都减少1.
2:直接消去某一列。
问把所有物体都消去最少需要多少次操作。
思路:因为只是对高度进行操作,和顺序无关,所以我们先对所有高度进行从小到大的排序。然后遍历一遍,到达第 i 个物体,其高度大于前面所有物体的高度,那么如果前面那些按照第一种方法消去的需要 h[i-1]次操作,后面(含i)进行第二种操作时,需要 n - i +1 次操作,对比所有请况,取最小值。
代码如下:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<iostream>
#define inf 0x3f3f3f3f
#define ll long long
#define mod 1e9+7
using namespace std;
const double pi=acos(-1.0);
int a[100010],dp[100010][2];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
int minn=min(n,a[n]);
for(int i=1;i<=n;i++)
{
minn=min(minn,a[i-1]+n-i+1);
}
printf("%d\n",minn);
return 0;
}