【CF Educational Codeforces Round 133 Div2】A-C

A. 2-3 Moves

题目

分析

给一个数,从加减2或3,直到最后的值等于这个数,求最少操作数。

操作数最小,每一步加的尽可能大,用3尽可能多,最后就会剩下差1和差2,差2直接用一个2,差1就少用一个3用两个2,所以用3不够的情况都是n/3+1,特判n=1的时候。

代码

#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define guanliu ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
const ll maxn=2e5+7;
const ll mod=1e9+7;
const ll INF=0x3f3f3f3f;
const double pi=acos(-1);

int main() 
{
	guanliu;
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		int ans=0;
		if(n%3==0)
		{
			ans=n/3;
		}
		else ans=n/3+1;
		if(n==1) ans=2;
		cout<<ans<<endl;
	} 
	return 0; 
}

B. Permutation Chain

题目

分析

用n个数字的全排列,组成一个排列链,这个排列链的稳定性必须越来越差,也就是越来越乱,在原位的数字越来越少。

要使不在原位的数字越来越少,稳定性就只能是0,2 ,3,4、、、n(因为交换后两个位置都改变,所以没有1)所以就只需要把前0、2、3、4、、、n的位置交换,输出就可以了

代码

#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define guanliu ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
const ll maxn=100+7;
const ll mod=1e9+7;
const ll INF=0x3f3f3f3f;
const double pi=acos(-1);
int a[maxn];

int main() 
{
	guanliu;
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			a[i]=i;
		}
		cout<<n<<endl;
		int j=1;
		for(int i=1;i<=n;i++)
		{
			if(i==1)
			{
				for(int j=1;j<=n;j++)
				{
					cout<<a[j]<<" ";
				}
				cout<<endl;
			}
			else{
				int tmp=a[j];
				a[j]=a[j+1];
				a[j+1]=tmp;
				for(int k=1;k<=n;k++)
				{
					cout<<a[k]<<" ";
				}
				cout<<endl;
				j++;
			}
		}
	} 
	return 0; 
}

C. Robot in a Hallway

题目

分析

一个2*m的网格,除左上第一个格外,其余格都被锁住了,等到了固定的时间后,才可以解锁,每一秒可以上下左右移动一格,或者原地不动。每个格子只能经过一遍,,求到达最后一个格子的最短时间。

因为每个格子只能走一边,所以只有蛇走和走一个圈,并且走圈之后不能再蛇走,所以先预处理出每个格走圈的时间,然后计算从哪个格开始从蛇形变成走圈,再蛇走和圈中选一个小值。

代码

#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define guanliu ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
const ll maxn=2e5+7;
const ll mod=1e9+7;
const ll INF=0x3f3f3f3f;
const double pi=acos(-1);
int a[2][maxn],b[2][maxn];

int main() 
{
	guanliu;
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n; 
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<n;j++)
			{
				cin>>a[i][j];
			}
		}
		a[0][0]=-1;
		b[0][n]=b[1][n]=0;
		for(int i=n-1;i>=0;i--)
		{
			for(int j=0;j<2;j++)
			{
				b[j][i]=max(max(a[1-j][i]+1,a[j][i]+2*(n-i)),b[j][i+1]+1);
			}
		}
		int ans=2e9;
		int now=0;
		for(int i=0;i<n;i++)
		{
			int j=i&1;
			ans=min(ans,max(now,b[j][i]));
			now=max(now,a[j][i]+2*(n-i));
			now=max(now,a[1-j][i]+2*(n-i)-1);
		}
		cout<<ans<<endl;
	} 
	return 0; 
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值