|
| 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 |
0 commit comments