Skip to content

Commit 757fb3d

Browse files
committed
srm 191 div 1 900
1 parent 896be9e commit 757fb3d

File tree

2 files changed

+251
-0
lines changed

2 files changed

+251
-0
lines changed

MagicianTour.cpp

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <queue>
8+
#include <cstring>
9+
#include <climits>
10+
using namespace std;
11+
typedef pair<int,int> pi;
12+
typedef vector<int> vi;
13+
typedef vector<vi> vvi;
14+
typedef vector<string> vs;
15+
typedef vector<vs> vvs;
16+
vs r;
17+
vi p;
18+
vi v;
19+
int n;
20+
21+
class MagicianTour {
22+
public:
23+
int dfs(int i, int k){
24+
v[i] = 1;
25+
int sum = p[i]*k;
26+
for(int j = 0; j < n; j++)
27+
if(r[i][j] == '1' && v[j] == 0) sum += dfs(j, -1*k);
28+
29+
return sum;
30+
}
31+
32+
int bestDifference(vs rr, vi pp){
33+
r = rr; p = pp;
34+
n = rr.size();
35+
v.resize(n);
36+
fill(v.begin(), v.end(), 0);
37+
vi vp;
38+
for(int i = 0; i < n; i++)
39+
if(!v[i]) vp.push_back(abs(dfs(i, 1)));
40+
41+
set<int> d;
42+
d.insert(0);
43+
for(int k : vp){
44+
set<int> dd;
45+
for(int kk : d){
46+
dd.insert(abs(k+kk));
47+
dd.insert(abs(k-kk));
48+
}
49+
d = dd;
50+
}
51+
return *d.begin();
52+
53+
}
54+
55+
// BEGIN CUT HERE
56+
public:
57+
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(); if ((Case == -1) || (Case == 5)) test_case_5(); if ((Case == -1) || (Case == 6)) test_case_6(); }
58+
private:
59+
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(); }
60+
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; } }
61+
void test_case_0() { string Arr0[] = {"01","10"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {15,20}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 5; verify_case(0, Arg2, bestDifference(Arg0, Arg1)); }
62+
void test_case_1() { string Arr0[] = {"0100",
63+
"1000",
64+
"0001",
65+
"0010"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2,4,2,4}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 0; verify_case(1, Arg2, bestDifference(Arg0, Arg1)); }
66+
void test_case_2() { string Arr0[] = {"0010",
67+
"0001",
68+
"1000",
69+
"0100"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2,2,2,2}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 0; verify_case(2, Arg2, bestDifference(Arg0, Arg1)); }
70+
void test_case_3() { string Arr0[] = {"000",
71+
"000",
72+
"000"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {6,7,15}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 2; verify_case(3, Arg2, bestDifference(Arg0, Arg1)); }
73+
void test_case_4() { string Arr0[] = {"0000",
74+
"0010",
75+
"0101",
76+
"0010"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {8,10,15,10}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 3; verify_case(4, Arg2, bestDifference(Arg0, Arg1)); }
77+
void test_case_5() { string Arr0[] = {"010",
78+
"101",
79+
"010"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {5,1,5}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 9; verify_case(5, Arg2, bestDifference(Arg0, Arg1)); }
80+
void test_case_6() { string Arr0[] = {
81+
"01000000000000000000000000000000000",
82+
"10100000000000000000000000000000000",
83+
"01010000000000000000000000000000000",
84+
"00101000000000000000000000000000000",
85+
"00010100000000000000000000000000000",
86+
"00001010000000000000000000000000000",
87+
"00000101000000000000000000000000000",
88+
"00000010100000000000000000000000000",
89+
"00000001010000000000000000000000000",
90+
"00000000101000000000000000000000000",
91+
"00000000010100000000000000000000000",
92+
"00000000001010000000000000000000000",
93+
"00000000000101000000000000000000000",
94+
"00000000000010100000000000000000000",
95+
"00000000000001010000000000000000000",
96+
"00000000000000101000000000000000000",
97+
"00000000000000010100000000000000000",
98+
"00000000000000001010000000000000000",
99+
"00000000000000000100000000000000000",
100+
"00000000000000000000000000000000000",
101+
"00000000000000000000010000000000000",
102+
"00000000000000000000101000000000000",
103+
"00000000000000000000010100000000000",
104+
"00000000000000000000001010000000000",
105+
"00000000000000000000000101000000000",
106+
"00000000000000000000000010100000000",
107+
"00000000000000000000000001010000000",
108+
"00000000000000000000000000101000000",
109+
"00000000000000000000000000010100000",
110+
"00000000000000000000000000001010000",
111+
"00000000000000000000000000000101000",
112+
"00000000000000000000000000000010100",
113+
"00000000000000000000000000000001010",
114+
"00000000000000000000000000000000101",
115+
"00000000000000000000000000000000010"
116+
}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {8,15,12,9,12,6,4,6,16,1,15,3,18,15,14,8,6,6,12,13,14,15,17,15,3,8,7,8,3,19,12,9,14,19,9}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 21; verify_case(6, Arg2, bestDifference(Arg0, Arg1)); }
117+
118+
// END CUT HERE
119+
120+
};
121+
122+
// BEGIN CUT HERE
123+
int main()
124+
{
125+
MagicianTour ___test;
126+
___test.run_test(-1);
127+
}
128+
// END CUT HERE

MagicianTour.txt

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
PROBLEM STATEMENT
2+
You are a magician who has two different shows. You are planning a tour of a series of cities. Given the population of each city and the roads between cities, you must plan the tour such that no two cities directly connected by a road are assigned the same show and such that the difference between the number of people who see show 1 and those who see show 2 is minimized. You have to return the difference between the number of people who will see show 1 and those who will see show 2. The roads between the cities are specified in the vector <string> roads. roads[i][j] is '1' if there is a road between the ith and jth cities and is '0' if there is no road. You are guaranteed that you can pull this off. (You can schedule the shows such that no two adjacent cities are assigned the same show.) Keep in mind that each city must be assigned one of the two shows. The population of each city is specified by the vector <int> populations whose ith element is the population of the ith city.
3+
4+
DEFINITION
5+
Class:MagicianTour
6+
Method:bestDifference
7+
Parameters:vector <string>, vector <int>
8+
Returns:int
9+
Method signature:int bestDifference(vector <string> roads, vector <int> populations)
10+
11+
12+
NOTES
13+
-You, the magician, can travel between two cities regardless of whether or not they are connected by a road.
14+
15+
16+
CONSTRAINTS
17+
-populations contains between 1 and 50 elements, inclusive.
18+
-roads contains exactly n elements, each of which has exactly n characters, where n is the number of elements in populations.
19+
-Each element of populations will be between 0 and 20, inclusive.
20+
-Each element of roads will only contain the characters '0' and '1'.
21+
-You are guaranteed that you can schedule the shows such that no two adjacent cities are assigned the same show.
22+
-No city will have a road to itself.
23+
-The graph is undirected. Hence, if there is a road from city a to city b then there has to be a road from city b to city a. As a result, roads[i][j] and roads[j][i] must be the same.
24+
25+
26+
EXAMPLES
27+
28+
0)
29+
{"01","10"}
30+
{15,20}
31+
32+
Returns: 5
33+
34+
There are two cities of populations 15 and 20. Perform show 1 in the first and show 2 in the second or vice versa.
35+
36+
1)
37+
{"0100",
38+
"1000",
39+
"0001",
40+
"0010"}
41+
{2,4,2,4}
42+
43+
Returns: 0
44+
45+
There are four cities of populations 2, 4, 2 and 4. Perform show 1 in the first and fourth cities and perform show 2 in the second and third cities.
46+
47+
2)
48+
{"0010",
49+
"0001",
50+
"1000",
51+
"0100"}
52+
{2,2,2,2}
53+
54+
Returns: 0
55+
56+
3)
57+
{"000",
58+
"000",
59+
"000"}
60+
{6,7,15}
61+
62+
Returns: 2
63+
64+
There are no roads! To keep it balanced, perform show 1 in the first and second cities and show 2 in the third city.
65+
66+
4)
67+
{"0000",
68+
"0010",
69+
"0101",
70+
"0010"}
71+
{8,10,15,10}
72+
73+
Returns: 3
74+
75+
5)
76+
{"010",
77+
"101",
78+
"010"}
79+
{5,1,5}
80+
81+
Returns: 9
82+
83+
6)
84+
{
85+
"01000000000000000000000000000000000",
86+
"10100000000000000000000000000000000",
87+
"01010000000000000000000000000000000",
88+
"00101000000000000000000000000000000",
89+
"00010100000000000000000000000000000",
90+
"00001010000000000000000000000000000",
91+
"00000101000000000000000000000000000",
92+
"00000010100000000000000000000000000",
93+
"00000001010000000000000000000000000",
94+
"00000000101000000000000000000000000",
95+
"00000000010100000000000000000000000",
96+
"00000000001010000000000000000000000",
97+
"00000000000101000000000000000000000",
98+
"00000000000010100000000000000000000",
99+
"00000000000001010000000000000000000",
100+
"00000000000000101000000000000000000",
101+
"00000000000000010100000000000000000",
102+
"00000000000000001010000000000000000",
103+
"00000000000000000100000000000000000",
104+
"00000000000000000000000000000000000",
105+
"00000000000000000000010000000000000",
106+
"00000000000000000000101000000000000",
107+
"00000000000000000000010100000000000",
108+
"00000000000000000000001010000000000",
109+
"00000000000000000000000101000000000",
110+
"00000000000000000000000010100000000",
111+
"00000000000000000000000001010000000",
112+
"00000000000000000000000000101000000",
113+
"00000000000000000000000000010100000",
114+
"00000000000000000000000000001010000",
115+
"00000000000000000000000000000101000",
116+
"00000000000000000000000000000010100",
117+
"00000000000000000000000000000001010",
118+
"00000000000000000000000000000000101",
119+
"00000000000000000000000000000000010"
120+
}
121+
{8,15,12,9,12,6,4,6,16,1,15,3,18,15,14,8,6,6,12,13,14,15,17,15,3,8,7,8,3,19,12,9,14,19,9}
122+
123+
Returns: 21

0 commit comments

Comments
 (0)