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;
   
   }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值