Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import static java.lang.Math.abs;
- public class pairs_in_matrix_ar
- {
- static int MAXN = 81;
- static int MAXT = MAXN * MAXN;
- static int MAXK = MAXT * 8;
- static int INF = (int)1e+9 + 1;
- static int a[][] = new int[MAXN][MAXN];
- static int next[] = new int[MAXK];
- static int dst[] = new int[MAXK];
- static int src[] = new int[MAXK];
- static int flow[] = new int[MAXK];
- static int dist[] = new int[MAXK];
- static int last[] = new int[MAXT];
- static int phi[] = new int[MAXT];
- static int d[] = new int[MAXT];
- static int p[] = new int[MAXT];
- static int frs[] = new int[MAXK];
- static int lst[] = new int[MAXK];
- static int nxt[] = new int[MAXK];
- static int val[] = new int[MAXK];
- static int n, m, k, t, c = 0;
- static int ans;
- static int cnt;
- public static void adde(int x, int y, int f, int c)
- {
- k += 1;
- src[k] = x;
- dst[k] = y;
- flow[k] = f;
- dist[k] = c;
- next[k] = last[x];
- last[x] = k;
- }
- public static void adde(int x, int y, int c)
- {
- adde(x, y, 1, c);
- adde(y, x, 0, -c);
- }
- public static void eadd(int i, int v)
- {
- c += 1;
- if (frs[i] == 0) frs[i] = c;
- if (lst[i] != 0) nxt[lst[i]] = c;
- val[c] = v;
- lst[i] = c;
- }
- public static int dijkstra(int st)
- {
- for (int i = 1; i <= t; i++)
- {
- d[i] = INF;
- p[i] = -1;
- }
- for (int i = 0; i <= c; i++)
- {
- frs[i] = 0;
- lst[i] = 0;
- nxt[i] = 0;
- }
- c = 0;
- p[st] = d[st] = 0;
- eadd(0, st);
- for (int l = 0; l < t; l++)
- for (int j = frs[l]; j > 0; j = nxt[j])
- {
- int x = val[j];
- if (d[x] != l)
- continue;
- for (int i = last[x]; i > 0; i = next[i])
- {
- cnt += 1;
- if (flow[i] == 0)
- continue;
- int y = dst[i];
- int v = l + dist[i] + phi[x] - phi[y];
- if (v < d[y])
- {
- d[y] = v;
- p[y] = i;
- eadd(v, y);
- }
- }
- }
- int res = d[t] + phi[t] - phi[st];
- for(int i = 1; i <= t; ++i)
- phi[i] += p[i] != -1? d[i] : d[t];
- return res;
- }
- public static void main(String[] args) throws FileNotFoundException
- {
- InputStream inputStream = System.in;
- OutputStream outputStream = System.out;
- InputReader in = new InputReader(inputStream);
- OutputWriter out = new OutputWriter(outputStream);
- int n = in.readInt();
- int m = in.readInt();
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= m; j++)
- a[i][j] = in.readInt();
- k = 1;
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= m; j++)
- if (((i + j) & 1) != 0)
- for (int dx = -1; dx <= 1; dx += 1)
- {
- for (int dy = dx == 0? -1 : 0; dy <= 1; dy += 2)
- {
- int x = (i - 1) * m + j;
- int y = (i + dx - 1) * m + j + dy;
- i += dx;
- j += dy;
- if (1 <= i && i <= n && 1 <= j && j <= m)
- {
- int c = a[i - dx][j - dy] != a[i][j]? 1 : 0;
- adde(x, y, c);
- }
- i -= dx;
- j -= dy;
- }
- }
- t = n * m + 2;
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= m; j++)
- if (((i + j) & 1) != 0)
- adde(t - 1, (i - 1) * m + j, 0);
- else
- adde((i - 1) * m + j, t, 0);
- for (int i = 1; i <= n * m / 2; i++)
- {
- int v = dijkstra(t - 1);
- // cout << v << endl;
- ans += v;
- int x = t;
- while (x != t - 1)
- {
- flow[p[x] ^ 0] -= 1;
- flow[p[x] ^ 1] += 1;
- x = src[p[x]];
- }
- }
- out.printLine(ans);
- out.close();
- }
- }
- class InputReader {
- private InputStream stream;
- private byte[] buf = new byte[1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
- public InputReader(InputStream stream) {
- this.stream = stream;
- }
- public int read() {
- if (numChars == -1)
- throw new InputMismatchException();
- if (curChar >= numChars) {
- curChar = 0;
- try {
- numChars = stream.read(buf);
- } catch (IOException e) {
- throw new InputMismatchException();
- }
- if (numChars <= 0)
- return -1;
- }
- return buf[curChar++];
- }
- public int readInt() {
- int c = read();
- while (isSpaceChar(c))
- c = read();
- int sgn = 1;
- if (c == '-') {
- sgn = -1;
- c = read();
- }
- int res = 0;
- do {
- if (c < '0' || c > '9')
- throw new InputMismatchException();
- res *= 10;
- res += c - '0';
- c = read();
- } while (!isSpaceChar(c));
- return res * sgn;
- }
- public long readLong() {
- int c = read();
- while (isSpaceChar(c))
- c = read();
- int sgn = 1;
- if (c == '-') {
- sgn = -1;
- c = read();
- }
- long res = 0;
- do {
- if (c < '0' || c > '9')
- throw new InputMismatchException();
- res *= 10;
- res += c - '0';
- c = read();
- } while (!isSpaceChar(c));
- return res * sgn;
- }
- public boolean isSpaceChar(int c) {
- if (filter != null)
- return filter.isSpaceChar(c);
- return isWhitespace(c);
- }
- public static boolean isWhitespace(int c) {
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
- public interface SpaceCharFilter {
- public boolean isSpaceChar(int ch);
- }
- }
- class OutputWriter {
- private final PrintWriter writer;
- public OutputWriter(OutputStream outputStream) {
- writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
- }
- public OutputWriter(Writer writer) {
- this.writer = new PrintWriter(writer);
- }
- public void print(Object...objects) {
- for (int i = 0; i < objects.length; i++) {
- if (i != 0)
- writer.print(' ');
- writer.print(objects[i]);
- }
- }
- public void printLine(Object...objects) {
- print(objects);
- writer.println();
- }
- public void close() {
- writer.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement