ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces 1294E Obtain a Permutation

2020-01-23 17:42:05  阅读:255  来源: 互联网

标签:数组 int Codeforces 1294E 序列 && Permutation rcd now


题目链接:

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;
}
Yuhan の Blog 发布了299 篇原创文章 · 获赞 8 · 访问量 7443 私信 关注

标签:数组,int,Codeforces,1294E,序列,&&,Permutation,rcd,now
来源: https://blog.csdn.net/qq_45228537/article/details/104076744

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有