Skip to content

Commit dac1d54

Browse files
committed
srm 167 div 1 750
1 parent caebad5 commit dac1d54

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

TeamPhoto.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <cstring>
8+
#include <climits>
9+
using namespace std;
10+
typedef pair<int,int> pi;
11+
typedef vector<int> vi;
12+
typedef vector<vi> vvi;
13+
typedef vector<string> vs;
14+
typedef vector<vs> vvs;
15+
int c, a1, a2;
16+
17+
class TeamPhoto {
18+
public:
19+
20+
int findMin(vi b, int a, int z){
21+
22+
int x1[4] = { b[0], b[a-1], b[a-1], b[0] };
23+
int y1[4] = { b[a-1], b[0], b[0], b[a-1] };
24+
int x2[4] = { b[z-1], b[z-1], b[a], b[a] };
25+
int y2[4] = { b[a], b[a], b[z-1], b[z-1] };
26+
int d1 = 0, d2 = 0;
27+
d1 = b[a-1]-b[0]; d2 = b[z-1]-b[a];
28+
int minv = 1000000;
29+
30+
for(int i = 0; i < 4; i++){
31+
minv = min(minv, min( abs(a1-x1[i]) + abs(a2-x2[i]), abs(a1-x2[i]) + abs(a2-x1[i]) ) + d1 + d2 + abs(c-y1[i]) + abs(c-y2[i]));
32+
}
33+
return minv;
34+
}
35+
36+
int minDiff(vector <int> h)
37+
{
38+
c = h[0], a1 = h[1], a2 = h[2];
39+
h.erase(h.begin(), h.begin()+3);
40+
sort(h.begin(), h.end());
41+
int k = h.size();
42+
if(k%2 == 0) return findMin(h, k/2, k);
43+
return min(findMin(h, k/2, k), findMin(h, k/2+1, k));
44+
}
45+
46+
// BEGIN CUT HERE
47+
public:
48+
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(); }
49+
private:
50+
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(); }
51+
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; } }
52+
void test_case_0() { int Arr0[] = {80,82,81,50,90,65}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 79; verify_case(0, Arg1, minDiff(Arg0)); }
53+
void test_case_1() { int Arr0[] = {70,82,91,50,50,50,50,50,50}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 113; verify_case(1, Arg1, minDiff(Arg0)); }
54+
void test_case_2() { int Arr0[] = {13, 17, 11, 12, 10}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 10; verify_case(2, Arg1, minDiff(Arg0)); }
55+
56+
// END CUT HERE
57+
58+
};
59+
60+
// BEGIN CUT HERE
61+
int main()
62+
{
63+
TeamPhoto ___test;
64+
___test.run_test(-1);
65+
}
66+
// END CUT HERE

TeamPhoto.txt

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
PROBLEM STATEMENT
2+
It's time to line up for the team photo. We want all the team members, plus the
3+
head coach and the two assistant coaches to be in the photo. We want the head coach
4+
in the middle and the two assistant coaches on the two ends. But
5+
we don't want to have much disparity in heights between adjacent people.
6+
7+
Let's line up to minimize the sum of the absolute height differences of
8+
adjacent people. If there is an even number of people, the coach can go in
9+
either center position. Create a class TeamPhoto that contains method minDiff that
10+
takes vector <int> height, the heights of all the people, and returns the minimum
11+
possible sum of adjacent absolute height differences.
12+
13+
height lists the coach first, then the two assistant coaches, then the team
14+
members.
15+
16+
17+
DEFINITION
18+
Class:TeamPhoto
19+
Method:minDiff
20+
Parameters:vector <int>
21+
Returns:int
22+
Method signature:int minDiff(vector <int> height)
23+
24+
25+
CONSTRAINTS
26+
-height has between 5 and 50 elements inclusive
27+
-each element of height is between 10 and 100 inclusive
28+
29+
30+
EXAMPLES
31+
32+
0)
33+
{80,82,81,50,90,65}
34+
35+
Returns: 79
36+
37+
The coach has a height of 80 and must be in one of the two middle positions, with the assistant
38+
coaches (82 and 81) on the two ends.
39+
The best lineup is 81,65,50,80,90,82 with the minDiff calculated as
40+
|81-65| + |65-50| + |50-80| + |80-90| + |90-82| = 79.
41+
There are other ways to achieve this minDiff.
42+
43+
44+
1)
45+
{70,82,91,50,50,50,50,50,50}
46+
47+
Returns: 113
48+
49+
50+
Line up in the order 82,50,50,50,70,50,50,50,91
51+
52+
2)
53+
{13, 17, 11, 12, 10}
54+
55+
Returns: 10
56+
57+
One optimal way to line up is 17, 12, 13, 10, 11 making the
58+
sum of the absolute differences 5 + 1 + 3 + 1 = 10

0 commit comments

Comments
 (0)