P4415 [COCI2006-2007#2] KOLONE 题解

题目

链接

https://www.luogu.com.cn/problem/P4415

字面描述

题面翻译

当蚂蚁移动时,蚂蚁排成一行,每个蚂蚁除了第一个蚂蚁都在另一个蚂蚁后面。众所周知,当两排蚂蚁在相反的方向上移动时,在一条太窄的通道中,两排都不能通过。一种理论认为,在这种情况下,蚂蚁会跳过对方。每隔一段时间,每一个蚂蚁都会跳过(或跳过去,就像他们同意的一样)蚂蚁在自己前面,这样两个蚂蚁交换位置,但是只有当另一只蚂蚁朝相反的方向移动时。在T秒后找到蚂蚁的顺序。
输入格式:
第一行包含两个整数N1和N2,分别在第一行和第二行中的蚂蚁的数量。接下来的两行包含第一行和第二行中的蚂蚁顺序(第一到最后一行)。每一个蚂蚁都是由一个大写字母(英文字母)唯一决定的(这两个字母在两行之间是唯一的)。输入的最后一行包含整数T(T小于50)。
输出格式:
在一条线上输出T秒后蚂蚁的顺序。我们的观点是,第一排蚂蚁来自我们的左侧,另一个来自我们的右侧。

感谢@心之所爱 提供的翻译

题目描述

When moving, ants form rows so that each ant except the first is behind another ant. It is not widely known what happens when two rows of ants moving in opposite directions run into each other in a passage too narrow for both rows to pass through. One theory says that, in that situation, ants will jump over each other.
From the moment the rows meet, each second every ant jumps over (or gets jumped over, as they agree upon) the ant in front of himself so that the two ants swap places, but only if the other ant is moving in the opposite direction. Find the order of the ants after T seconds.

输入格式

The first line contains two integers N1 and N2, the numbers of ants in the first and second rows, respectively.
The next two rows contain the orders of ants in the first and second row (first to last). Each ant is uniquely determined by an uppercase letter of the English alphabet (this letter is unique between both rows).
The last line of input contains the integer T (0 ≤ T ≤ 50).

输出格式

Output the order of the ants after T seconds on a single line. Our viewpoint is such that the first row of ants comes from our left side and the other one from our right side.

样例 #1

样例输入 #1
3 3
ABC
DEF
0
样例输出 #1
CBADEF

样例 #2

样例输入 #2
3 3
ABC
DEF
2
样例输出 #2
CDBEAF

样例 #3

样例输入 #3
3 4
JLA
CRUO
3
样例输出 #3
CARLUJO

思路

此题操作数t上限为50
每次操作只需要 2 ∗ 1 0 5 每次操作只需要2*10^5 每次操作只需要2105
所以时间复杂度是可以的

本人构建思路为下:
若题目给出a,b队列长度,和要求操作次数t
那最终操作次数为 m i n ( l e n a + l e n b − 1 , t ) 那最终操作次数为min(lena+lenb-1,t) 那最终操作次数为min(lena+lenb1,t)
在一开始将a,b队列合并;
循环操作
每次操作如下:
将队列从右往左搜,若找到一个属于b队列的,且他的右边是a队列,连个交换,在搜下一个属于b队列de

代码实现

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

const int maxn=2e5+10;
int n,m,t;
struct node{
	char s;
	int op;
	inline void input(){
		scanf(" %c",&s);
	}
	inline void output(){
		printf("%c",s);
	}
}a[maxn];
int main(){
	//freopen("B.in","r",stdin);
	//freopen("B.out","w",stdout);
	scanf("%d%d",&n,&m);
	//a数组读入,并标记
	for(int i=1;i<=n;i++){
		a[i].input();
		a[i].op=1;
	}
	//反转a数组
	reverse(a+1,a+n+1);
	//b数组读入,并标记
	for(int i=n+1;i<=n+m;i++){
		a[i].input();
		a[i].op=2;
	}
	//算出最终操作次数
	scanf("%d",&t);
	t=min(t,n+m-1);
	//操作
	for(int i=1;i<=t;i++){
	//从右往左一次遍历
		for(int j=n+m;j>=2;j){
		//符合条件
			if(a[j].op==2&&a[j-1].op==1){
				swap(a[j],a[j-1]);
				j-=2;
			}
			else j--;
		}
	}
	for(int i=1;i<=n+m;i++)a[i].output();
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Yxz_

我只是一名ssfoier

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值