璟雯院

珺璟如晔,雯华若锦

用户工具

站点工具


icpc:problems:usaco20feb_swapity_swapity_swap_s
problems
名称Swapity Swapity Swap S
题目编号USACO20FEB_S1
题目链接luogu.com.cn/…
来源USACO
算法分类线性代数, 思维, 快速幂
难易程度一般般

Swapity Swapity Swap S

想法

如果将一次置换理解为一次矩阵乘法,那k次置换就可以使用矩阵乘法的结合律来进行。

代码实现

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
 
const int N = 1e5+1;
 
int line[N], temp[N], ans[N];
 
int main() {
	int n, m, k;
	scanf("%d %d %d", &n, &m, &k);
	for (int i = 1; i <= n; ++i)
	{
		line[i] = i;
		temp[i] = i;
		ans[i] = i;
	}
	for (int i = 1; i <= m; ++i) {
		int l, r;
		scanf("%d %d", &l, &r);
		for (int j = 0; j < (r - l + 1) / 2; j++) {
			swap(line[l+j], line[r-j]);
		}
	}
 
	while(k) {
		if(k & 1) {
			for (int i = 1; i <= n; ++i) {
				temp[i] = ans[line[i]];
			}
			memcpy(ans, temp, sizeof(ans));
		}
		for (int i = 1; i <= n; ++i) {
			temp[i] = line[line[i]];
		}
		memcpy(line, temp, sizeof(line));
		k >>= 1;
	}
 
	for (int i = 1; i <= n; ++i) {
		printf("%d\n", ans[i]);
	}
	return 0;
}
/app/www/public/data/pages/icpc/problems/usaco20feb_swapity_swapity_swap_s.txt · 最后更改: 2023/06/03 15:02 由 温婕莺