Skip to content

Commit 600309c

Browse files
committed
SRM 627
1 parent fd579c5 commit 600309c

File tree

8 files changed

+505
-0
lines changed

8 files changed

+505
-0
lines changed

TopCoder/SRM/627/1.png

83.1 KB
Loading

TopCoder/SRM/627/2.png

85.7 KB
Loading

TopCoder/SRM/627/3.png

85.3 KB
Loading
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
// BEGIN CUT HERE
2+
3+
// END CUT HERE
4+
#include <algorithm>
5+
#include <iostream>
6+
#include <sstream>
7+
#include <string>
8+
#include <vector>
9+
#include <queue>
10+
#include <set>
11+
#include <map>
12+
#include <cstdio>
13+
#include <cstdlib>
14+
#include <cctype>
15+
#include <cmath>
16+
#include <string>
17+
#include <cstring>
18+
using namespace std;
19+
20+
// BEGIN CUT HERE
21+
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
22+
23+
template<typename T> void print( T a ) {
24+
cerr << a;
25+
}
26+
static void print( long long a ) {
27+
cerr << a << "L";
28+
}
29+
static void print( string a ) {
30+
cerr << '"' << a << '"';
31+
}
32+
template<typename T> void print( vector<T> a ) {
33+
cerr << "{";
34+
for ( int i = 0 ; i != a.size() ; i++ ) {
35+
if ( i != 0 ) cerr << ", ";
36+
print( a[i] );
37+
}
38+
cerr << "}" << endl;
39+
}
40+
template<typename T> void eq( int n, T have, T need ) {
41+
if ( have == need ) {
42+
cerr << "Case " << n << " passed." << endl;
43+
} else {
44+
cerr << "Case " << n << " failed: expected ";
45+
print( need );
46+
cerr << " received ";
47+
print( have );
48+
cerr << "." << endl;
49+
}
50+
}
51+
template<typename T> void eq( int n, vector<T> have, vector<T> need ) {
52+
if( have.size() != need.size() ) {
53+
cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements.";
54+
print( have );
55+
print( need );
56+
return;
57+
}
58+
for( int i= 0; i < have.size(); i++ ) {
59+
if( have[i] != need[i] ) {
60+
cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << ".";
61+
print( have );
62+
print( need );
63+
return;
64+
}
65+
}
66+
cerr << "Case " << n << " passed." << endl;
67+
}
68+
static void eq( int n, string have, string need ) {
69+
if ( have == need ) {
70+
cerr << "Case " << n << " passed." << endl;
71+
} else {
72+
cerr << "Case " << n << " failed: expected ";
73+
print( need );
74+
cerr << " received ";
75+
print( have );
76+
cerr << "." << endl;
77+
}
78+
}
79+
// END CUT HERE
80+
81+
#define REP(i,n) for(int i=0;i<(n);++i)
82+
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
83+
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
84+
#define CLR(x) memset((x),0,sizeof((x)))
85+
#define SORT(x) sort(x.begin(), x.end())
86+
#define SZ(x) x.size()
87+
#define PB push_back
88+
typedef long long LL;
89+
typedef vector<int> VI;
90+
typedef vector<vector<int> > VVI;
91+
92+
// BEGIN CUT HERE
93+
vector<string> split( const string& s, const string& delim =" " ) {
94+
vector<string> res;
95+
string t;
96+
for ( int i = 0 ; i != s.size() ; i++ ) {
97+
if ( delim.find( s[i] ) != string::npos ) {
98+
if ( !t.empty() ) {
99+
res.push_back( t );
100+
t = "";
101+
}
102+
} else {
103+
t += s[i];
104+
}
105+
}
106+
if ( !t.empty() ) {
107+
res.push_back(t);
108+
}
109+
return res;
110+
}
111+
112+
vector<int> splitInt( const string& s, const string& delim =" " ) {
113+
vector<string> tok = split( s, delim );
114+
vector<int> res;
115+
for ( int i = 0 ; i != tok.size(); i++ )
116+
res.push_back( atoi( tok[i].c_str() ) );
117+
return res;
118+
}
119+
// END CUT HERE
120+
121+
// BEGIN CUT HERE
122+
int s2i(string s) {
123+
stringstream ss;
124+
ss << s;
125+
int res;
126+
ss >> res;
127+
return res;
128+
}
129+
130+
string i2s(int n) {
131+
stringstream ss;
132+
ss << n;
133+
string res;
134+
ss >> res;
135+
return res;
136+
}
137+
// END CUT HERE
138+
139+
#define INF 1000000000
140+
141+
int n, k, res;
142+
VVI mm;
143+
VI val;
144+
int used[1005], que[1005];
145+
146+
class GraphInversions {
147+
public:
148+
void dfs(int cur, int num, int sum) {
149+
if (sum >= res) return;
150+
if (num >= k) {
151+
res = min(res, sum);
152+
return;
153+
}
154+
REP(i,SZ(mm[cur])) {
155+
int next = mm[cur][i];
156+
if (used[next] == 1) continue;
157+
used[next] = 1;
158+
que[num] = next;
159+
int add = 0;
160+
REP(j,num) {
161+
if (val[next] < val[que[j]]) ++add;
162+
}
163+
dfs(next, num + 1, sum + add);
164+
used[next] = 0;
165+
}
166+
}
167+
int getMinimumInversions(vector <int> A, vector <int> B, vector <int> V, int K) {
168+
val = V;
169+
k = K;
170+
n = SZ(A);
171+
mm.assign(n, VI());
172+
REP(i,n) {
173+
int a = A[i], b = B[i];
174+
mm[a].PB(b);
175+
mm[b].PB(a);
176+
}
177+
res = INF;
178+
REP(i,n) {
179+
CLR(used);
180+
que[0] = i;
181+
used[i] = 1;
182+
dfs(i, 1, 0);
183+
}
184+
if (res == INF) return -1;
185+
return res;
186+
}
187+
};
188+
// BEGIN CUT HERE
189+
int main() {
190+
{
191+
int AARRAY[] = {0,1,2};
192+
vector <int> A( AARRAY, AARRAY+ARRSIZE(AARRAY) );
193+
int BARRAY[] = {1,2,0};
194+
vector <int> B( BARRAY, BARRAY+ARRSIZE(BARRAY) );
195+
int VARRAY[] = {40,50,60};
196+
vector <int> V( VARRAY, VARRAY+ARRSIZE(VARRAY) );
197+
GraphInversions theObject;
198+
eq(0, theObject.getMinimumInversions(A, B, V, 3),0);
199+
}
200+
{
201+
int AARRAY[] = {0,1,2,3};
202+
vector <int> A( AARRAY, AARRAY+ARRSIZE(AARRAY) );
203+
int BARRAY[] = {1,2,3,0};
204+
vector <int> B( BARRAY, BARRAY+ARRSIZE(BARRAY) );
205+
int VARRAY[] = {60,40,50,30};
206+
vector <int> V( VARRAY, VARRAY+ARRSIZE(VARRAY) );
207+
GraphInversions theObject;
208+
eq(1, theObject.getMinimumInversions(A, B, V, 3),1);
209+
}
210+
{
211+
int AARRAY[] = {0,1,2,3,0};
212+
vector <int> A( AARRAY, AARRAY+ARRSIZE(AARRAY) );
213+
int BARRAY[] = {1,2,3,0,4};
214+
vector <int> B( BARRAY, BARRAY+ARRSIZE(BARRAY) );
215+
int VARRAY[] = {10,10,10,5,5};
216+
vector <int> V( VARRAY, VARRAY+ARRSIZE(VARRAY) );
217+
GraphInversions theObject;
218+
eq(2, theObject.getMinimumInversions(A, B, V, 5),1);
219+
}
220+
{
221+
int AARRAY[] = {0,1,2,3,0,2};
222+
vector <int> A( AARRAY, AARRAY+ARRSIZE(AARRAY) );
223+
int BARRAY[] = {1,2,3,0,4,5};
224+
vector <int> B( BARRAY, BARRAY+ARRSIZE(BARRAY) );
225+
int VARRAY[] = {10,2,5,3,7,1};
226+
vector <int> V( VARRAY, VARRAY+ARRSIZE(VARRAY) );
227+
GraphInversions theObject;
228+
eq(3, theObject.getMinimumInversions(A, B, V, 6),-1);
229+
}
230+
{
231+
int AARRAY[] = {5,7,7,5,5,7,6,4};
232+
vector <int> A( AARRAY, AARRAY+ARRSIZE(AARRAY) );
233+
int BARRAY[] = {2,0,2,0,1,4,7,3};
234+
vector <int> B( BARRAY, BARRAY+ARRSIZE(BARRAY) );
235+
int VARRAY[] = {15,10,5,30,22,10,5,2};
236+
vector <int> V( VARRAY, VARRAY+ARRSIZE(VARRAY) );
237+
GraphInversions theObject;
238+
eq(4, theObject.getMinimumInversions(A, B, V, 6),3);
239+
}
240+
return 0;
241+
}
242+
// END CUT HERE
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><p>You are given a connected undirected graph with N vertices.
2+
The vertices are numbered 0 through N-1.
3+
The graph is special: the number of edges is exactly equal to the number of vertices.
4+
You are given the description of the graph as three vector &lt;int&gt;s: <b>A</b>, <b>B</b>, and <b>V</b>.
5+
Each of these vector &lt;int&gt;s has N elements.
6+
For each valid i, there's an edge between vertices <b>A</b>[i] and <b>B</b>[i].
7+
There are no self-loops and no duplicate edges.
8+
For each valid i, we associate the value <b>V</b>[i] with the vertex i.</p>
9+
10+
<p>We will be interested in some simple paths in this graph.
11+
A simple path is a sequence of vertices such that no vertex is used twice, and each pair of consecutive vertices is connected by an edge.
12+
For any simple path, we can write down a sequence of integers: the values associated with the vertices on the path, in order of appearance.
13+
We will call such a sequence the <i>value list</i> of the given simple path.</p>
14+
15+
<p>An inversion in a sequence S is a pair of valid indices (i,j) into the sequence S such that i &lt; j but S[i] &gt; S[j].
16+
For example, the sequence S = {10, 30, 20, 20} has two inversions: (1,2) and (1,3).
17+
(The indices are 0-based.)</p>
18+
19+
<p>You are given a graph G in the format described above, and an int <b>K</b>.
20+
In the graph G, consider all simple paths with <b>K</b> or more vertices.
21+
If there is no such simple path, return -1.
22+
Otherwise, return the smallest number of inversions in the value list of such path.</p>
23+
</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>GraphInversions</td></tr><tr><td>Method:</td><td>getMinimumInversions</td></tr><tr><td>Parameters:</td><td>vector &lt;int&gt;, vector &lt;int&gt;, vector &lt;int&gt;, int</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int getMinimumInversions(vector &lt;int&gt; A, vector &lt;int&gt; B, vector &lt;int&gt; V, int K)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>256</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td>N will be between 3 and 1000, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>A</b>, <b>B</b>, and <b>V</b> will each have N elements.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>A</b> and <b>B</b> will be between 0 and N-1, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>For all valid i, <b>A</b>[i] will not be equal to <b>B</b>[i]. (I.e., there are no self loops.)</td></tr><tr><td align="center" valign="top">-</td><td>No two edges will connect the same pair of vertices.</td></tr><tr><td align="center" valign="top">-</td><td>The graph described by <b>A</b> and <b>B</b> will be connected.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>V</b> will be between 1 and 1000, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>K</b> will be between 1 and N, inclusive.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{0,1,2}</pre></td></tr><tr><td><pre>{1,2,0}</pre></td></tr><tr><td><pre>{40,50,60}</pre></td></tr><tr><td><pre>3</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2">The best path is the path {0, 1, 2}. Its value list is the sequence {40, 50, 60}, and there are 0 inversions in this sequence.
24+
25+
</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{0,1,2,3}</pre></td></tr><tr><td><pre>{1,2,3,0}</pre></td></tr><tr><td><pre>{60,40,50,30}</pre></td></tr><tr><td><pre>3</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</pre></td></tr><tr><td><table><tr><td colspan="2">As <b>K</b>=3, we are interested in simple paths of lengths 3 or more.
26+
Each simple path of length 3 or more gives us at least one inversion.
27+
The path {3, 2, 1} is an example of an optimal path.
28+
Its value list is {30, 50, 40}.
29+
There is 1 inversion: the 50 occurring before the 40.
30+
</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{0,1,2,3,0}</pre></td></tr><tr><td><pre>{1,2,3,0,4}</pre></td></tr><tr><td><pre>{10,10,10,5,5}</pre></td></tr><tr><td><pre>5</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</pre></td></tr><tr><td><table><tr><td colspan="2">Two or more nodes can have the same associated value.
31+
</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{0,1,2,3,0,2}</pre></td></tr><tr><td><pre>{1,2,3,0,4,5}</pre></td></tr><tr><td><pre>{10,2,5,3,7,1}</pre></td></tr><tr><td><pre>6</pre></td></tr></table></td></tr><tr><td><pre>Returns: -1</pre></td></tr><tr><td><table><tr><td colspan="2">There are no simple paths with length 6.
32+
</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{5,7,7,5,5,7,6,4}</pre></td></tr><tr><td><pre>{2,0,2,0,1,4,7,3}</pre></td></tr><tr><td><pre>{15,10,5,30,22,10,5,2}</pre></td></tr><tr><td><pre>6</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>

0 commit comments

Comments
 (0)