博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
topcoder Single Round 624(500)
阅读量:6882 次
发布时间:2019-06-27

本文共 3663 字,大约阅读时间需要 12 分钟。

hot3.png

题目 

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.

                   

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.)

                   

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.

                   

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.

Definition

Class: BuildingHeightsEasy
Method: minimum
Parameters: int, vector <int>
Returns: int
Method signature: int minimum(int M, vector <int> heights)
(be sure your method is public)

Limits

Time limit (s): 2.000
Memory limit (MB): 256

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)
2
                   
{1, 2, 1, 4, 3}
                   
Returns: 0
             
Note that we already have two buildings that have the                      same number of floors. Hence, no additional floors need                      to be built.
1)
3
                   
{1, 3, 5, 2, 1}
                   
Returns: 2
             
We want to have at least three buildings with the same                      number of floors. The best way to reach this goal is to                      build one floor on building #0 and one floor on building                      #4 (0-based indices). After these changes, buildings #0,                      #3, and #4 will have two floors each.
2)
1
                   
{43, 19, 15}
                   
Returns: 0
             
3)
3
                   
{19, 23, 9, 12}
                   
Returns: 15
             
4)
12
                   
{25, 18, 38, 1, 42, 41, 14, 16, 19, 46, 42, 39, 38, 31, 43, 37, 26, 41, 33, 37, 45, 27, 19, 24, 33, 11, 22, 20, 36, 4, 4}
                   
Returns: 47
             

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.

解析

暴力求解

代码

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;#define MAX_HEIGHT  (50)#define ARRAY_SIZE  (MAX_HEIGHT + 10)#define INF         (10000000)  struct BuildingHeightsEasy{int minimum(int M, vector 
 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];  };

转载于:https://my.oschina.net/u/572632/blog/278927

你可能感兴趣的文章
建造模式
查看>>
Android adt bundle 开发环境配置及第一个“Hello world”程序运行
查看>>
Ubuntu下安装LAMP及phpmyadmin
查看>>
《每个设计师都应该掌握的50个css代码段》31~35段
查看>>
Chrome浏览器插件开发心得
查看>>
ubuntu eclipse 配置 gtk+2.0 库
查看>>
Maven是什么
查看>>
Tomcat理解
查看>>
深入理解 intent (1)
查看>>
Java 中的伪共享详解及解决方案
查看>>
Spring 源码分析(一) —— 迈向Spring之路
查看>>
SVN Server 安装配置
查看>>
springCloud-9.Client使用Config Server
查看>>
多线程/线程池20问
查看>>
HBase性能优化方法总结(一):表的设计
查看>>
从异常堆栈中还原 ProGuard 混淆过的代码
查看>>
A20修改顶部状态栏 禁止跳转设置界面
查看>>
Android--多线程之Handler
查看>>
java synchronized用法
查看>>
MySQL开机自动启动的设置方法
查看>>