Opening Ceremony
Time Limit: 5000ms
Memory Limit: 524288KB
This problem will be judged on CodeForcesGym. Original ID: 100502E
64-bit integer IO format: %I64d Java class name: (Any)
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).Onasingle charge, this cutting-edge contraption can remove either all of the ?oors in a single tower block, or all the x-th ?oors in all the blocks simultaneously, for user’s choice of the ?oor number x. In the latter case, the blocks that are less than x ?oors high are left untouched, while for blocks having more than x ?oors, all the ?oors above the removed x-th one fall down by one level.
Task
Given the number of ?oors of all towers, output the minimum number of charges needed to eliminate all ?oors of all blocks.
Input
The ?rst line of input contains the number of blocks n, where 2 ≤ n ≤ 100000. The second line contains n consecutive block heights hi for i = 1,2,...,n, where 1 ≤ hi ≤ 1000000. Output Output oneline containingone integer: theminimum numberof charges needed totear down all the blocks.
Sample Input 1
6
2 1 8 8 2 3
Sample Output 1
5
Sample Input 2
5
1 1 1 1 10
Sample Output 2
2
NCPC 2014 Problem E: Opening Ceremony 9
解题:贪心,要消除一行肯定消除低的,要消除整列的话优先消除高的。。
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 100010; 4 int h[maxn],n; 5 int main(){ 6 while(~scanf("%d",&n)){ 7 for(int i = 1; i <= n; ++i) 8 scanf("%d",h+i); 9 sort(h+1,h+n+1); 10 int ret = n; 11 for(int i = 1; i <= n; ++i) 12 ret = min(ret,n - i + h[i]); 13 printf("%d\n",ret); 14 } 15 return 0; 16 }