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 | |||||||||||||
| |||||||||||||
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) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
| |||||||||||||
5) | |||||||||||||
|
其中一种解答:
#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 | |||||||||||||
| |||||||||||||
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) | |||||||||||||
|
其中一种解答:
#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;
}
};