Skip to content

Commit 01f229c

Browse files
committed
srm 624 div 2 500
1 parent a2175fb commit 01f229c

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

BuildingHeightsEasy.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
using namespace std;
8+
9+
class BuildingHeightsEasy {
10+
public:
11+
int minimum(int N, vector <int> heights)
12+
{
13+
sort(heights.rbegin(), heights.rend());
14+
int sol = 1000000000;
15+
int n = heights.size();
16+
for(int i = 0; i <= n-N; i++){
17+
int sum = 0;
18+
int rh = heights[i];
19+
for(int j = i; j - i < N && j < n; j++) sum += (rh - heights[j]);
20+
sol = min(sol, sum);
21+
// cout<<i<<" "<<sol<<endl;
22+
}
23+
return sol;
24+
}
25+
26+
// BEGIN CUT HERE
27+
public:
28+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
29+
private:
30+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
31+
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
32+
void test_case_0() { int Arg0 = 2; int Arr1[] = {1, 2, 1, 4, 3}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 0; verify_case(0, Arg2, minimum(Arg0, Arg1)); }
33+
void test_case_1() { int Arg0 = 3; int Arr1[] = {1, 3, 5, 2, 1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 2; verify_case(1, Arg2, minimum(Arg0, Arg1)); }
34+
void test_case_2() { int Arg0 = 1; int Arr1[] = {43, 19, 15}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 0; verify_case(2, Arg2, minimum(Arg0, Arg1)); }
35+
void test_case_3() { int Arg0 = 3; int Arr1[] = {19, 23, 9, 12}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 15; verify_case(3, Arg2, minimum(Arg0, Arg1)); }
36+
void test_case_4() { int Arg0 = 12; int Arr1[] = {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}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 47; verify_case(4, Arg2, minimum(Arg0, Arg1)); }
37+
38+
// END CUT HERE
39+
40+
};
41+
42+
// BEGIN CUT HERE
43+
int main()
44+
{
45+
BuildingHeightsEasy ___test;
46+
___test.run_test(-1);
47+
}
48+
// END CUT HERE

BuildingHeightsEasy.txt

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
PROBLEM STATEMENT
2+
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.
3+
4+
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.)
5+
6+
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.
7+
8+
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.
9+
10+
DEFINITION
11+
Class:BuildingHeightsEasy
12+
Method:minimum
13+
Parameters:int, vector <int>
14+
Returns:int
15+
Method signature:int minimum(int M, vector <int> heights)
16+
17+
18+
CONSTRAINTS
19+
-heights will contain between 1 and 50 elements, inclusive.
20+
-M will be between 1 and the number of elements in heights, inclusive.
21+
-Each element in heights will be between 1 and 50, inclusive.
22+
23+
24+
EXAMPLES
25+
26+
0)
27+
2
28+
{1, 2, 1, 4, 3}
29+
30+
Returns: 0
31+
32+
Note that we already have two buildings that have the same number of floors. Hence, no additional floors need to be built.
33+
34+
1)
35+
3
36+
{1, 3, 5, 2, 1}
37+
38+
Returns: 2
39+
40+
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.
41+
42+
2)
43+
1
44+
{43, 19, 15}
45+
46+
Returns: 0
47+
48+
49+
50+
3)
51+
3
52+
{19, 23, 9, 12}
53+
54+
Returns: 15
55+
56+
57+
58+
4)
59+
12
60+
{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}
61+
62+
Returns: 47
63+
64+

0 commit comments

Comments
 (0)