Skip to content

Commit 520b347

Browse files
committed
srm 584 div 1 250
1 parent 71b28d0 commit 520b347

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed

Egalitarianism.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <vector>
2+
#include <string>
3+
#include <numeric>
4+
#include <queue>
5+
using namespace std;
6+
vector<string> M;
7+
int visited[55], n;
8+
9+
class Egalitarianism {
10+
11+
int bfs(int s){
12+
13+
queue<pair<int, int> > Q;
14+
Q.push(make_pair(s, 0));
15+
int maxl = 0;
16+
17+
while(!Q.empty()){
18+
pair<int, int> p = Q.front();
19+
Q.pop();
20+
int i = p.first, l = p.second;
21+
maxl = max(l, maxl);
22+
for(int j = 0; j < n; j++)
23+
if(!visited[j] && M[i][j] == 'Y'){
24+
visited[j] = 1;
25+
Q.push(make_pair(j, l+1));
26+
}
27+
}
28+
return maxl;
29+
}
30+
31+
public:
32+
int maxDifference(vector<string> const &isFriend,
33+
int d) {
34+
M = isFriend;
35+
n = M.size();
36+
int maxv = 0;
37+
for(int i = 0; i < n; i++){
38+
fill(visited, visited+50, 0);
39+
visited[i] = 1;
40+
maxv = max(maxv, bfs(i));
41+
int count = accumulate(visited, visited+n, 0);
42+
if(count < n) return -1;
43+
}
44+
if(maxv == 0) return -1;
45+
return maxv*d;
46+
}
47+
};

Egalitarianism.html

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<html>
2+
<head>
3+
<title>Egalitarianism</title>
4+
</head>
5+
<body>
6+
<h1><a href="http://community.topcoder.com/tc?module=ProblemDetail&amp;rd=15696&amp;pm=12613">Egalitarianism</a></h1>
7+
8+
<p><em>Single Round Match 584 Round 1 - Division I, Level One</em></p>
9+
10+
<h2>Statement</h2>
11+
12+
<p>A kingdom has n citizens. Each one has some amount of money, a number of dollars denoted by a non-negative integer.</p>
13+
14+
<p>Citizens are numbered from 0 to n-1. Some citizens have friends. Their friendship network is described by a String[] called <em>isFriend</em>, such that if <em>isFriend</em>[i][j] == 'Y', the citizens numbered i and j are friends, and if <em>isFriend</em>[i][j] == 'N', these citizens are not friends.</p>
15+
16+
<p>The king decrees the following:</p>
17+
18+
<p>Each citizen's amount of money must be within <em>d</em> dollars of the amount of money belonging to any of his friends. In other words, a citizen with x dollars must not have any friends with less than x-<em>d</em> dollars or more than x+<em>d</em> dollars.</p>
19+
20+
<p>Given the number of citizens and their friendship network, what is the greatest possible money difference between any two people (not necessarily friends) in this kingdom? If there is a finite answer, return it. Otherwise, return -1.</p>
21+
22+
<h2>Definitions</h2>
23+
24+
<ul>
25+
<li><em>Class</em>: <code>Egalitarianism</code></li>
26+
<li><em>Method</em>: <code>maxDifference</code></li>
27+
<li><em>Parameters</em>: <code>String[], int</code></li>
28+
<li><em>Returns</em>: <code>int</code></li>
29+
<li><em>Method signature</em>: <code>int maxDifference(String[] isFriend, int d)</code></li>
30+
</ul>
31+
32+
33+
<h2>Constraints</h2>
34+
35+
<ul>
36+
<li>n will be between 2 and 50, inclusive.</li>
37+
<li><em>d</em> will be between 0 and 1,000, inclusive.</li>
38+
<li><em>isFriend</em> will contain exactly n elements.</li>
39+
<li>Each element of <em>isFriend</em> will contain exactly n characters, each of which is either 'Y' or 'N'.</li>
40+
<li>For any i, <em>isFriend</em>[i][i] = 'N'.</li>
41+
<li>For any i and j, <em>isFriend</em>[i][j] = <em>isFriend</em>[j][i].</li>
42+
</ul>
43+
44+
45+
<h2>Examples</h2>
46+
47+
<h3>Example 1</h3>
48+
49+
<h4>Input</h4>
50+
51+
<p><c>["NYN",<br /> "YNY",<br /> "NYN"],<br />10</c></p>
52+
53+
<h4>Output</h4>
54+
55+
<p><c>20</c></p>
56+
57+
<h4>Reason</h4>
58+
59+
<p>The kingdom has three citizens. Citizens 0 and 1 are friends. Also, citizens 1 and 2 are friends. The greatest possible money difference between any two citizens is $20, as in the following configuration: citizen 0 has $100; citizen 1 has $110; citizen 2 has $120.</p>
60+
61+
<h3>Example 2</h3>
62+
63+
<h4>Input</h4>
64+
65+
<p><c>["NN",<br /> "NN"],<br />1</c></p>
66+
67+
<h4>Output</h4>
68+
69+
<p><c>-1</c></p>
70+
71+
<h4>Reason</h4>
72+
73+
<p>Since citizens 0 and 1 are not friends, there are no constraints between them.</p>
74+
75+
<h3>Example 3</h3>
76+
77+
<h4>Input</h4>
78+
79+
<p><c>["NNYNNN",<br /> "NNYNNN",<br /> "YYNYNN",<br /> "NNYNYY",<br /> "NNNYNN",<br /> "NNNYNN"],<br />1000</c></p>
80+
81+
<h4>Output</h4>
82+
83+
<p><c>3000</c></p>
84+
85+
<h3>Example 4</h3>
86+
87+
<h4>Input</h4>
88+
89+
<p><c>["NNYN",<br /> "NNNY",<br /> "YNNN",<br /> "NYNN"],<br />584</c></p>
90+
91+
<h4>Output</h4>
92+
93+
<p><c>-1</c></p>
94+
95+
<h3>Example 5</h3>
96+
97+
<h4>Input</h4>
98+
99+
<p><c>["NYNYYYN",<br /> "YNNYYYN",<br /> "NNNNYNN",<br /> "YYNNYYN",<br /> "YYYYNNN",<br /> "YYNYNNY",<br /> "NNNNNYN"],<br />5</c></p>
100+
101+
<h4>Output</h4>
102+
103+
<p><c>20</c></p>
104+
105+
<h3>Example 6</h3>
106+
107+
<h4>Input</h4>
108+
109+
<p><c>["NYYNNNNYYYYNNNN",<br /> "YNNNYNNNNNNYYNN",<br /> "YNNYNYNNNNYNNNN",<br /> "NNYNNYNNNNNNNNN",<br /> "NYNNNNYNNYNNNNN",<br /> "NNYYNNYNNYNNNYN",<br /> "NNNNYYNNYNNNNNN",<br /> "YNNNNNNNNNYNNNN",<br /> "YNNNNNYNNNNNYNN",<br /> "YNNNYYNNNNNNNNY",<br /> "YNYNNNNYNNNNNNN",<br /> "NYNNNNNNNNNNNNY",<br /> "NYNNNNNNYNNNNYN",<br /> "NNNNNYNNNNNNYNN",<br /> "NNNNNNNNNYNYNNN"],<br />747</c></p>
110+
111+
<h4>Output</h4>
112+
113+
<p><c>2988</c></p>
114+
115+
<h3>Example 7</h3>
116+
117+
<h4>Input</h4>
118+
119+
<p><c>["NY",<br /> "YN"],<br />0</c></p>
120+
121+
<h4>Output</h4>
122+
123+
<p><c>0</c></p>
124+
125+
</body>
126+
</html>

0 commit comments

Comments
 (0)