Sunday, December 18, 2011

SRM 527 Div 1 Medium

Let's call the two given matrices A and B. A is a mask of the secret matrix. B is another mask, but with columns messed up. We want to find the lexicographically smallest secret matrix. A straightforward way to do this is as follows:


    find the first '?' in A
    set it to '0' if we can, otherwise set it to '1'
    repeat until no '?''s are left in A

The tricky part is how do we check whether we can set a ? to 0. Suppose you have set some ? to 0 in A. How do you check that A is still compatible with B? Think about the columns in A and in B. They should match. This is a matching problem! We are matching columns of A to columns of B. And if we have a matching subroutine pre-coded, we can get a lot of points for this 450-point problem.

Code follows. LIB_getMatchingSize is my library function that returns the maximum matching size.

vector<string> rows, cols; 
int r,c; 
bool works() { 
  vector<vector< int> > adj(c, vector<int>(c,0));
  for (int i = 0; i < c; i++) { 
    for (int j = 0; j < c; j++) { 
      int good = 1; 
      for (int k = 0; k < r; k++) { 
        if (rows[k][i] != '?' && cols[j][k] != '?' && rows[k][i] != cols[j][k]) 
          good = 0; 
      } 
      adj[i][j] = good; 
    } 
  } 
   
  return LIB_getMatchingSize(adj)==c; 
} 

struct P8XMatrixRecovery { 
  vector<string> solve(vector<string> _rows, vector<string> _cols) { 
    rows = _rows; 
    cols = _cols; 
    r = rows.size(); 
    c = cols.size(); 
    for (int i = 0; i < r; i++) { 
      for (int j = 0; j < c; j++) { 
        if (rows[i][j] == '?') { 
          rows[i][j] = '0'; 
          if (!works()) 
            rows[i][j] = '1'; 
        } 
      } 
    } 
    return rows; 
  } 
};

SRM 527 Div 1 Easy

(rephrasing one of the editorial solutions)

Suppose we are growing the tree as follows. On each step, there are 3 groups of nodes: Black, Gray, and White. The Black nodes are those whose degrees are known. Each Gray node is connected to exactly one of the Black nodes. Each White node is not connected to anything yet. On each step, we grow the tree by selecting a set S of 0 or more White nodes and connecting them all to one of the Gray nodes which we will call X. We mark X as Black and S as Gray.

The solution is to write a recurrent relation f(numberOfGray, numberOfWhite). Note that one of the nodes in the optimal tree will have degree one. Suppose we select that node as the root. Then the answer to the whole problem is score[0] + f(1, n-2). That is, we start with 1 Black, 1 Gray, and N-2 White nodes. The Black node has degree 1, thus we add score[0] to our answer. f(1,n-2) will account for the degrees of the Gray and White nodes as they turn Black. The recurrent relation is f(nGray, nWhite) = max_k score[k]+f(nGray-1+k, nWhite-k).

Code:

int solve(vector sco) {
int n = sco.size()+1;


int f[55][55];


for (int sum = 1; sum <= n-1; sum++) {
for (int u = 0; u <= sum; u++) {
int r = sum - u;


int& ret = f[u][r] = -1;
if (u==0) {
ret = (r==0 ? 0 : -1);
continue;
}

if (r==0) {
ret = sco[0]*u;
continue;
}

for (int x = 0; x <= r; x++) {
int z = f[u-1+x][r-x];
if (z >= 0)
ret = max(ret, z + sco[x]);
}
}
}

return f[1][n-2]+sco[0];
}