topcoder SRM 506

Problem Statement

    NOTE: This problem statement contains images that may not display properly if viewed outside of the applet.



You are playing a game titled Slimes City. You are managing several towns in this game.



You have N towns in a region. For simplicity, they are named 0 through N-1. The population of town i is population[i].



You are going to merge these towns to form one big city. The procedure is as follows. While the number of towns is more than one, pick two different towns, i and j. Delete these two towns and form a new one, with a population equal to the sum of the populations of towns i and j. The name of the newly formed town will be i if i had a larger population than j, or j if j had a larger population than i. If both of the original towns had the same population, either name can be chosen.



When this process ends, one town remains. That town's name becomes the city's name. Return the number of different possible names for the resulting city.

Definition

    
Class:SlimeXSlimesCity
Method:merge
Parameters:vector <int>
Returns:int
Method signature:int merge(vector <int> population)
(be sure your method is public)
    
 

Constraints

-population will contain between 2 and 50 elements, inclusive.
-Each element of population will be between 1 and 1,000,000,000, inclusive.

Examples

0) 
    
{2, 3, 4}
Returns: 2




If towns 0 and 1 are merged first, the city's name will be 1. Otherwise the city's name will be 2. It is illustrated by the following image that shows the only three possible ways to merge the cities.



1) 
    
{1, 2, 3}
Returns: 2




If town 2 is merged first (with town 0 or 1), the city's name will be 2. Otherwise, if towns 0 and 1 are merged first (to form a new town named 1, with a population of 3) and then the resulting town is merged with town 2, the city can be named either 1 or 2. It's not possible for the city to be named 0.



2) 
    
{8,2,3,8}
Returns: 2
There may be multiple towns with the same population.
3) 
    
{1000000000, 999999999, 999999998, 999999997}
Returns: 3
 
4) 
    
{1,1,1}
Returns: 3
 
5) 
    
{1, 2, 4, 6, 14, 16, 20}
Returns: 3

 

 

其中一种解答:

 

#include <cmath>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;

class SlimeXSlimesCity {
public:
   int merge( vector <int> pop ) {
       
       sort( pop.begin(), pop.end() );
       
       int ret = 0;
       for( int i = 0; i < pop.size(); i++ ) {
       
            long long dosad = 0;
            int j;
            
            for( j = 0; j <= i; j++ )
            dosad += pop[ j ];
            
            if( j < pop.size() )
            while( 1 ) {
                   
                   if( dosad >= pop[ j ] ) dosad += (long long)pop[ j ];
                   else break;
                   
                   j++;
                   
                   if( j == pop.size() ) break;
                          
            }
            
            if( j == pop.size() ) ret++;
            
            
       }
       
       return ret;
       
   }
};

 

Problem Statement

    You are playing a game titled Slime Rancher 2. You will be training slimes in this game.



You have a slime-in-training. Associated with the slime are N attributes, numbered 0 through N-1, each represented by a positive integer. You are given vector <int> attributes containing N integers : the i-th integer is the initial value of the i-th attribute for the slime. After the training is complete, each of the slime's attributes will either stay the same or increase to some positive integer less than or equal to 999. None of the attributes will decrease in value. The weight of the training is defined as the sum of the differences between the final and initial values of all the attributes for the slime.



You are a master slime breeder, and you're able to obtain any possible final values for a slime's attributes. This time, you would like to create a well-balanced slime. A slime is well-balanced if all of its attributes have equal values. What is the minimum possible weight of the training?

Definition

    
Class:SlimeXSlimeRancher2
Method:train
Parameters:vector <int>
Returns:int
Method signature:int train(vector <int> attributes)
(be sure your method is public)
    
 

Constraints

-attributes will contain between 2 and 50 elements, inclusive.
-Each element of attributes will be between 1 and 999, inclusive.

Examples

0) 
    
{1,2,3}
Returns: 3
Train the slime such that all of its attributes are equal to 3. The total weight of the training is |3 - 1| + |3 - 2| + |3 - 3| = 3.
1) 
    
{5,5}
Returns: 0
This slime is already well-balanced.
2) 
    
{900,500,100}
Returns: 1200

 

 

其中一种解答:

 

#include <cmath>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;

class SlimeXSlimeRancher2 {
public:
   int train( vector <int> attributes ) {
   
       int ret = 0;
       int maxi = 0;
       for( int i = 0; i < attributes.size(); i++ )
       maxi >?= attributes[ i ];
   
       for( int i = 0; i < attributes.size(); i++ )
       if( attributes[ i ] < maxi ) ret += maxi - attributes[ i ];
       
       return ret;
   
   }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值