|
|
#include<bits/stdc++.h> |
|
|
#include "testlib.h" |
|
|
using namespace std; |
|
|
|
|
|
int n, m, ref_ops; |
|
|
vector<vector<int>> initial_poles; |
|
|
vector<vector<int>> current_poles; |
|
|
|
|
|
bool isValidFinalState() { |
|
|
|
|
|
map<int, int> color_to_pole; |
|
|
|
|
|
for (int i = 0; i <= n; i++) { |
|
|
set<int> colors_on_pole; |
|
|
for (int ball : current_poles[i]) { |
|
|
colors_on_pole.insert(ball); |
|
|
|
|
|
if (color_to_pole.find(ball) == color_to_pole.end()) { |
|
|
color_to_pole[ball] = i; |
|
|
} else if (color_to_pole[ball] != i) { |
|
|
|
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (colors_on_pole.size() > 1) { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
return true; |
|
|
} |
|
|
|
|
|
int main(int argc, char* argv[]) { |
|
|
registerTestlibCmd(argc, argv); |
|
|
|
|
|
|
|
|
n = inf.readInt(); |
|
|
m = inf.readInt(); |
|
|
|
|
|
initial_poles.resize(n + 1); |
|
|
for (int i = 0; i < n; i++) { |
|
|
initial_poles[i].resize(m); |
|
|
for (int j = 0; j < m; j++) { |
|
|
initial_poles[i][j] = inf.readInt(); |
|
|
if (initial_poles[i][j] < 1 || initial_poles[i][j] > n) { |
|
|
quitf(_fail, "Invalid color at pole %d: %d", i+1, initial_poles[i][j]); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
ref_ops = ans.readInt(); |
|
|
|
|
|
|
|
|
current_poles = initial_poles; |
|
|
|
|
|
|
|
|
if (ouf.seekEof()) { |
|
|
quitf(_wa, "No output provided"); |
|
|
} |
|
|
|
|
|
int participant_ops = ouf.readInt(); |
|
|
|
|
|
if (participant_ops < 0) { |
|
|
quitf(_wa, "Number of operations cannot be negative: %d", participant_ops); |
|
|
} |
|
|
|
|
|
if (participant_ops > 2000000) { |
|
|
quitf(_wa, "Number of operations exceeds limit: %d > 2,000,000", participant_ops); |
|
|
} |
|
|
|
|
|
|
|
|
for (int op = 0; op < participant_ops; op++) { |
|
|
if (ouf.seekEof()) { |
|
|
quitf(_wa, "Expected %d operations, but only found %d", participant_ops, op); |
|
|
} |
|
|
|
|
|
int x = ouf.readInt(); |
|
|
int y = ouf.readInt(); |
|
|
|
|
|
|
|
|
if (x < 1 || x > n + 1 || y < 1 || y > n + 1) { |
|
|
quitf(_wa, "Invalid operation %d: must be between 1 and %d", op+1, n+1); |
|
|
} |
|
|
|
|
|
if (x == y) { |
|
|
quitf(_wa, "Invalid operation %d: x and y must be different", op+1); |
|
|
} |
|
|
|
|
|
|
|
|
x--; y--; |
|
|
|
|
|
|
|
|
if (current_poles[x].empty()) { |
|
|
quitf(_wa, "Invalid operation %d: pole %d has no balls", op+1, x+1); |
|
|
} |
|
|
|
|
|
|
|
|
if ((int)current_poles[y].size() >= m) { |
|
|
quitf(_wa, "Invalid operation %d: pole %d is full", op+1, y+1); |
|
|
} |
|
|
|
|
|
|
|
|
int ball = current_poles[x].back(); |
|
|
current_poles[x].pop_back(); |
|
|
current_poles[y].push_back(ball); |
|
|
} |
|
|
|
|
|
if (!ouf.seekEof()) { |
|
|
quitf(_wa, "Extra output found after %d operations", participant_ops); |
|
|
} |
|
|
|
|
|
|
|
|
if (!isValidFinalState()) { |
|
|
quitf(_wa, "Final state is invalid: not all balls of same color are grouped"); |
|
|
} |
|
|
|
|
|
|
|
|
double score_ratio = (double)(ref_ops + 1) / (participant_ops + 1); |
|
|
double unbounded_ratio = score_ratio; |
|
|
score_ratio = min(1.0, score_ratio); |
|
|
|
|
|
quitp(score_ratio, "Operations: %d. Ratio: %.6f, RatioUnbounded: %.6f", participant_ops, score_ratio, unbounded_ratio); |
|
|
|
|
|
return 0; |
|
|
} |