Educational Codeforces Round 133 (Rated for Div. 2)A.B

A. 2-3 Moves

题目链接:

Problem - A - Codeforces

题面:

题意:

你一开始站在0的位置,你可以向左/右移动2格或者3格,问你到n的位置至少需要几秒

思路:

n一定是大于0的,如果n是1的我们需要2步:-2 +3

一次走两步,那么走到n就是n/2步,

一次走三步,如果n是3的倍数就是n/3,如果不是3的倍数就是n/3+1

两个取最小值即可

代码:

#include<bits/stdc++.h>
using namespace std;


int main(){
	int t;
	cin >> t;
	while(t--){
		int n;
		cin >> n;
		if(n == 1){
			cout << 2 << endl;
			continue;
		}
		int ans = n / 2;
		int cnt = n / 3;
		if(n % 3 != 0){
			cnt++;
		}
		cout << min(ans, cnt) << endl;
	}
	return 0;
}

B. Permutation Chain

题目链接:

Problem - B - Codeforces

题面:

题意:

一个排列p(每个数只能出现一次),这个排列的不变性就是i == pi的个数,现在需要构造一个排列链,要求排列链是有效的(后面的排列的不变性恒小于前面那个,第一个排列的不变性一定是n),问最长的排列链的长度,以及输出这个排列链。

思路:

排列链的最长长度一定是n

第一个排列一定是1-n

第二个排列可以随意交换2个

后面可以随意选择一个满足i != pi的和一个满足i == pi的交换即可

代码:

#include<bits/stdc++.h>
using namespace std;

int arr[105];

int main(){
	int t;
	cin >> t;
	while(t--){
		int n;
		cin >> n;
		cout << n << endl;
		for(int i = 1; i <= n; i++){
			if(i != 1){
				cout << " ";
			}
			cout << i; 
		}
		cout << endl;
		arr[1] = n;
		for(int i = 2; i < n; i++){
			arr[i] = i;
		}
		arr[n] = 1;
		for(int i = 1; i <= n; i++){
			if(i != 1){
				cout << " ";
			}
			cout << arr[i]; 
		}
		cout << endl;
		for(int j = 1; j <= n - 2; j++){
			swap(arr[j], arr[j + 1]);
			for(int i = 1; i <= n; i++){
				if(i != 1){
					cout << " ";
				}
				cout << arr[i]; 
			}
			cout << endl;
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值