bingxuan9112

Untitled

Jan 22nd, 2020
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. /*
  2. --------------              |   /
  3.       |                     |  /
  4.       |                     | /
  5.       |             *       |/          |    |         ------            *
  6.       |                     |           |    |        /      \
  7.       |             |       |\          |    |       |       |\          |
  8.    \  |             |       | \         |    |       |       | \         |
  9.     \ |             |       |  \        |    |        \     /   \        |
  10.      V              |       |   \        \__/|         -----     \       |
  11. */
  12. #include <bits/stdc++.h>
  13. using namespace std;
  14.  
  15. #define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(NULL);
  16. #define endl '\n'
  17. #define mem(i,j) memset(i,j,sizeof i);
  18. #define F first
  19. #define S second
  20. #define pb push_back
  21. #define mp make_pair
  22. #define all(a) a.begin(), a.end()
  23. #define bit(s,i) (((s)>>(i))&1LL)
  24. #define lowbit(x) (x&-x)
  25. #define siz(v) (long long)v.size()
  26. typedef int64_t ll;
  27. typedef uint64_t ull;
  28. typedef pair<int,int> pii;
  29. typedef pair<ll, ll> pll;
  30. const double EPS  = 1e-8;
  31. const int INF     = 0x3F3F3F3F;
  32. const ll LINF     = 4611686018427387903;
  33. const int MOD     = 1e9+7;
  34. const int MAXN    = 2e5+9;
  35. /*-----------------------------------------------------------------------------------------------------*/
  36.  
  37. signed main() {
  38.     EmiliaMyWife
  39.  
  40.     int n, m;
  41.     cin >> n >> m;
  42.     vector<vector<int>> arr(n, vector<int>(m));
  43.     for(int i =  0; i < n; i++) {
  44.         for(int j = 0; j < m; j++) {
  45.             cin >> arr[i][j];
  46.         }
  47.     }
  48.     vector<vector<int>> cnt(n*m+1);
  49.     for(int j = 0; j < m; j++) {
  50.         for(int i = 0; i < n; i++) {
  51.             if(((arr[i][j]%m)==(j+1)) && (arr[i][j]<=n*m)) cnt[arr[i][j]].pb(i);
  52.         }
  53.     }
  54.     vector<int> ccnt(n*m+1);
  55.     for(int i = 1; i <= n*m; i++) {
  56.         if(cnt[i].empty()) ccnt[i]=INF;
  57.         else {
  58.             // 沒有要再用cnt了所以可以改掉,如要再用的話可以複製一份改值
  59.             for(int &x: cnt[i]) x = min(abs(x-(i-1)/m), n-abs((i-1)/m-x));
  60.             ccnt[i] = *min_element(all(cnt[i]));
  61.         }
  62.     }
  63.     int ans=0;
  64.     for(int j = 0; j < m; j++) {
  65.         int opp = INF;
  66.         for(int i = 0; i < n; i++) {
  67.             int x = ccnt[i*m+j+1];
  68.             if(abs(opp) > n-abs(i-x)) opp = n - abs(x - i);
  69.             if(abs(opp) > abs(i-x)) opp = i - x;
  70.         }
  71.         if(opp==INF) opp=0;
  72.         int cnt1=n, cnt2=n;
  73.         opp %= n;
  74.         for(int i = 0; i < n; i++) {
  75.             if(arr[(ccnt[i*m+j+1]+opp  )%n][j]==(i*m+j+1)) cnt1--;
  76.             if(arr[(ccnt[i*m+j+1]-opp+n)%n][j]==(i*m+j+1)) cnt2--;
  77.         }
  78.         ans += min(cnt1, cnt2);
  79.         if(opp) ans++;
  80.     }
  81.     cout << ans;
  82.  
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment