Sicily 周赛 Opening Ceremony

Description

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.

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.

Sample Input
Copy sample input to clipboard
样例一:
6
2 1 8 8 2 3
样例二:
5
1 1 1 1 10
Sample Output
样例一:
5
样例二:
2

题解:

排序之后看非零楼的数目,还有最高楼的数目,两者消去最大值。

类似贪心算法。


比如这样,看红色区域还有黄色区域谁大消谁。


代码:

#include <cstdio>
#include <algorithm>
using namespace std;
int arr[100005];
int main()
{
	int n ;
	while(~scanf("%d",&n))
	{
		long long sum = 0 ,cur = 0;
		for(int i = 0;i < n;i++)
		{
			scanf("%d",&arr[i]);
			sum += arr[i];
		}
		sort(arr, arr + n);
		int l = 0 ,r = n - 1;
		int cnt = 0;
		int height = 1 ;
		while(cur < sum)
		{
			if((r - l + 1) <= (arr[r] - height + 1))
			{
				cur += (arr[r] - height + 1);
				r--;
			}
			else
			{
				cur += (r - l + 1);   //用个height记录,循环每层减一会超时 
				while(arr[l]<=height)l++;
				height++;
			}
			cnt++;
		}
		printf("%d\n",cnt);
			
			
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值