Skip to content

Commit 8e6f322

Browse files
committed
srm 624 div 2 250
1 parent 01f229c commit 8e6f322

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

CostOfDancing.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <numeric>
8+
using namespace std;
9+
10+
class CostOfDancing {
11+
public:
12+
int minimum(int K, vector <int> danceCost)
13+
{
14+
sort(danceCost.begin(), danceCost.end());
15+
return accumulate(danceCost.begin(), danceCost.begin() + K, 0);
16+
}
17+
18+
// BEGIN CUT HERE
19+
public:
20+
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(); }
21+
private:
22+
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(); }
23+
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; } }
24+
void test_case_0() { int Arg0 = 2; int Arr1[] = {1, 5, 3, 4}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 4; verify_case(0, Arg2, minimum(Arg0, Arg1)); }
25+
void test_case_1() { int Arg0 = 3; int Arr1[] = {1, 5, 4}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 10; verify_case(1, Arg2, minimum(Arg0, Arg1)); }
26+
void test_case_2() { int Arg0 = 1; int Arr1[] = {2, 2, 4, 5, 3}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 2; verify_case(2, Arg2, minimum(Arg0, Arg1)); }
27+
void test_case_3() { int Arg0 = 39; int Arr1[] = {973, 793, 722, 573, 521, 568, 845, 674, 595, 310, 284, 794, 913, 93, 129, 758, 108, 433, 181, 163, 96, 932,
28+
703, 989, 884, 420, 615, 991, 364, 657, 421, 336, 801, 142, 908, 321, 709, 752, 346, 656, 413, 629, 801}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 20431; verify_case(3, Arg2, minimum(Arg0, Arg1)); }
29+
30+
// END CUT HERE
31+
32+
};
33+
34+
// BEGIN CUT HERE
35+
int main()
36+
{
37+
CostOfDancing ___test;
38+
___test.run_test(-1);
39+
}
40+
// END CUT HERE

CostOfDancing.txt

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
PROBLEM STATEMENT
2+
Gustavo studies at the Byteversity (one of the best universities in Byteland). He's also very keen on dancing, so he decided to enroll at a dance school.
3+
4+
The school offers many different courses, each teaching one dance. Different courses may have different costs. You are given a vector <int> danceCost. The elements of danceCost are the costs of all courses offered at the dance school.
5+
6+
Gustavo would like to learn exactly K of those dances. He is a poor student, so his only priority is to pay as little as possible for the courses.
7+
8+
You are given the int K and the vector <int> danceCost. Compute and return the smallest possible total cost of learning K dances.
9+
10+
DEFINITION
11+
Class:CostOfDancing
12+
Method:minimum
13+
Parameters:int, vector <int>
14+
Returns:int
15+
Method signature:int minimum(int K, vector <int> danceCost)
16+
17+
18+
CONSTRAINTS
19+
-danceCost will contain between 1 and 1,000 elements, inclusive.
20+
-Each element of danceCost will be between 1 and 1,000, inclusive.
21+
-K will be between 1 and the number of elements in danceCost, inclusive.
22+
23+
24+
EXAMPLES
25+
26+
0)
27+
2
28+
{1, 5, 3, 4}
29+
30+
Returns: 4
31+
32+
Gustavo must pay for exactly two out of the four given courses. The cheapest possibility is to pay 1 for one course and then 3 for another course. The total cost is 1+3 = 4.
33+
34+
1)
35+
3
36+
{1, 5, 4}
37+
38+
Returns: 10
39+
40+
Gustavo has no choice here. He has to pay for all three courses, which costs 1+5+4 = 10.
41+
42+
2)
43+
1
44+
{2, 2, 4, 5, 3}
45+
46+
Returns: 2
47+
48+
Among all 5 possible courses he can take, the cheapest one is either the course #0 or course #1 (0-based).
49+
50+
3)
51+
39
52+
{973, 793, 722, 573, 521, 568, 845, 674, 595, 310, 284, 794, 913, 93, 129, 758, 108, 433, 181, 163, 96, 932,
53+
703, 989, 884, 420, 615, 991, 364, 657, 421, 336, 801, 142, 908, 321, 709, 752, 346, 656, 413, 629, 801}
54+
55+
Returns: 20431
56+
57+

0 commit comments

Comments
 (0)