zoukankan      html  css  js  c++  java
  • Codeforces 1294E Obtain a Permutation

    题目链接:

    Codeforces 1294E Obtain a Permutation

    思路:

    对于每一列的数组,我们分开考虑,设当前为第j列(j1开始);
    对于在第i位置的数(i0开始),它应该为i * m + j,因此如果一个数n是在我们目标序列中的,它的位置应该为(n - j) / m
    遍历我们当前的序列,计算这个数是否在目标序列中,如果在就计算当前应该挪多少次才能到它应该在的位置;
    设立数组rcd[]rcd[i]的意思就是挪i次可以使rcd[i]个数复原;
    遍历数组rcd[],计算挪i次的情况一共需要的步骤,即i + n - rcd[i],取所有值的最小值;
    将每一列计算得出的最小值相加即是结果;

    代码:

    #include<bits/stdc++.h>
    
    using namespace std;
    
    inline int read() {
    	int x = 0; char c;
    	while(c = getchar(), c < '0' || c > '9');
    	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    	return x;
    }
    
    const int maxn = 2e5 + 5;
    vector<int> mat[maxn];
    inline int get(vector<int> & now, int & c, int & m) {
    	int n = now.size(), ans = 1 << 30;
    	vector<int> rcd(n + 1);
    	for(int & x : rcd) x = 0;
    	for(int i = 0; i < n; i++) {
    		int x = now[i] - c, y = x / m;
    		if(x % m == 0 && y >= 0 && y <= n - 1) ++rcd[(i - y + n) % n];  // the range of y should be noticed
    	}
    	for(int i = 0; i < n + 1; i++) ans = min(ans, i + n - rcd[i]);
    	return ans;
    }
    
    int main() {
    #ifdef MyTest
    	freopen("Sakura.txt", "r", stdin);
    #endif	
    	int n = read(), m = read();
    	for(int i = 0; i < n; i++) {
    		mat[i].push_back(0);
    		for(int j = 1; j <= m; j++) mat[i].push_back(read());
    	}
    	int ans = 0;
    	for(int j = 1; j <= m; j++) {
    		vector<int> now;
    		for(int i = 0; i < n; i++) now.push_back(mat[i][j]);
    		ans += get(now, j, m);	
    	}
    	cout << ans;
    	return 0;
    }
    
  • 相关阅读:
    树莓派_GPIO
    Python_PyQt_基本使用
    python_静态,组合,继承
    Linux_操作
    Pyqt5+eric6安装教程
    树莓派操作
    python_爬虫_requests
    HTML的基本操作
    python_pandas_numpy_json
    python_模块_sys_time_random_os
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12308645.html
Copyright © 2011-2022 走看看