Submission #51325

# Submission time Handle Problem Language Result Execution time Memory
51325 2018-06-17T13:29:49 Z KieranHorgan Orchard (NOI14_orchard) C++17
20 / 25
1000 ms 8532 KB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long seed;
  asm("rdtsc" : "=A"(seed));
  srand(seed);

  int n, m;
  cin >> n >> m;

  if(n == 1) {
    vector<int> grid(m+1, 0);
    int totalOnes = 0;
    for(int i = 1; i <= m; i++) {
      cin >> grid[i];
      if(grid[i])
        totalOnes++;
    }

    int ans = 1ll<<29;
    int carried = totalOnes;
    for(int i = 1; i <= m; i++) {
      if(grid[i] == 1) {
        carried--;
        carried = min(carried, totalOnes-1);
        ans = min(ans, carried);
      } else {
        carried++;
        carried = min(carried, totalOnes+1);
        ans = min(ans, carried);
      }
    }
    cout << ans << endl;
  } else if(n == 2) {
    vector<int> g1(m+1, 0);
    vector<int> g2(m+1, 0);
    int totalOnes = 0;
    int ones1 = 0;
    int ones2 = 0;
    for(int i = 1; i <= m; i++) {
      cin >> g1[i];
      if(g1[i])
        totalOnes++,
        ones1++;
    }
    for(int i = 1; i <= m; i++) {
      cin >> g2[i];
      if(g2[i])
        totalOnes++,
        ones2++;
    }


    int ans = 1ll<<29;
    int carried = ones1;
    for(int i = 1; i <= m; i++) {
      if(g1[i] == 1) {
        carried--;
        carried = min(carried, ones1-1);
        ans = min(ans, carried+ones2);
      } else {
        carried++;
        carried = min(carried, ones1+1);
        ans = min(ans, carried+ones2);
      }
    }
    carried = ones2;
    for(int i = 1; i <= m; i++) {
      if(g2[i] == 1) {
        carried--;
        carried = min(carried, ones2-1);
        ans = min(ans, carried+ones1);
      } else {
        carried++;
        carried = min(carried, ones2+1);
        ans = min(ans, carried+ones1);
      }
    }

    for(int i = 1; i <= m; i++) {
      if(g1[i]==0) g1[i] = -1;
      if(g2[i]==0) g2[i] = -1;
      g1[i] += g2[i];
    }

    carried = totalOnes;
    for(int i = 1; i <= m; i++) {
      if(g1[i] >= 0) {
        carried -= g1[i];
        carried = min(carried, totalOnes-g1[i]);
        ans = min(ans, carried);
      } else {
        carried -= g1[i];
        carried = min(carried, totalOnes-g1[i]);
        ans = min(ans, carried);
      }
    }
    cout << ans << endl;
  } else {
    vector<vector<int>> grid(n+1, vector<int>(m+1, 0));
    int totalOnes = 0;
    for(int i = 1; i <= n; i++) {
      for(int j = 1; j <= m; j++) {
        cin >> grid[i][j];
        if(grid[i][j])
          totalOnes++;
        grid[i][j] = !grid[i][j];
        grid[i][j] -= grid[i-1][j-1];
        grid[i][j] += grid[i][j-1];
        grid[i][j] += grid[i-1][j];
      }
    }

    int ans = 1ll<<29;
    for(int a = 0; a < n; a++) {
      for(int b = 0; b < m; b++) {
        for(int c = a+1; c <= n; c++) {
          for(int d = b+1; d <= m; d++) {
            int sz = (c-a)*(d-b);
            int zeroes = grid[c][d] - grid[c][b] - grid[a][d] + grid[a][b];
            ans = min(ans, zeroes + totalOnes-(sz-zeroes));
          }
        }
      }
    }
    cout << ans << endl;
  }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 432 KB Output is correct
4 Correct 2 ms 448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 500 KB Output is correct
2 Correct 4 ms 632 KB Output is correct
3 Correct 3 ms 764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 8368 KB Output is correct
2 Correct 95 ms 8408 KB Output is correct
3 Correct 92 ms 8532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 8532 KB Output is correct
2 Correct 30 ms 8532 KB Output is correct
3 Correct 31 ms 8532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 8532 KB Output is correct
2 Correct 242 ms 8532 KB Output is correct
3 Correct 260 ms 8532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1084 ms 8532 KB Time limit exceeded
2 Halted 0 ms 0 KB -