Problem Statement |
|||||||||||||
Byteland is a city with many skyscrapers, so it's a perfect venue for BASE jumping. Danilo is an enthusiastic BASE jumper. He plans to come to Byteland and to jump off some of its buildings.ios Danilo wants to make M jumps in Byteland. However, he has some rules. First, he never jumps off the same building twice. Second, all buildings he selects for his jumps must have the same number of floors. (This is for safety reasons: It is hard to get the timing right if each jump starts at a different height.)ui Philipe is the mayor of Byteland. He welcomes Danilo's visit as he would like to use it as a publicity stunt. However, Philipe knows that Danilo will only come to Byteland if there are at least M buildings that each have the same number of floors. To ensure that, the mayor is willing to build additional floors on some of the skyscrapers in Byteland.this You are given the int M and a vector <int> heights. Each element of heights is the number of floors in one of Byteland's skyscrapers. Compute and return the smallest number of additional floors the mayor has to build so that there will be at least M buildings with the same number of floors.spa |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Constraints |
|||||||||||||
- | heights will contain between 1 and 50 elements, inclusive. | ||||||||||||
- | M will be between 1 and the number of elements in heights, inclusive. | ||||||||||||
- | Each element in heights will be between 1 and 50, inclusive. | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
|||||||||||||
4) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.code
暴力求解orm
#include <cstring> #include <iostream> #include <vector> #include <stack> #include <algorithm> #include <cstring> using namespace std; #define MAX_HEIGHT (50) #define ARRAY_SIZE (MAX_HEIGHT + 10) #define INF (10000000) struct BuildingHeightsEasy{ int minimum(int M, vector <int> heights){ int min_costnum = INF; memset(height_table, 0, ARRAY_SIZE * sizeof(int)); for(int i = 0; i < heights.size(); i++){ height_table[heights[i]]++; } for(int base_height = MAX_HEIGHT; base_height >= 1; base_height--){ int build_num = M; int costnum = 0; int totbuilds; int curr_height = base_height; while(build_num > 0 && curr_height){ //build cost totbuilds = min(build_num, height_table[curr_height]); costnum = costnum + totbuilds * (base_height - curr_height); //build num if(build_num <= height_table[curr_height]){ build_num = 0; }else{ build_num -= height_table[curr_height]; } curr_height--; } if(build_num > 0){ continue; } if(costnum < min_costnum){ min_costnum = costnum; } } return min_costnum; } int height_table[ARRAY_SIZE]; };