L. Palm Island


L. Palm Island

Toph is playing a card game. She has n𝑛 cards and each card has a unique number of 1,2⋯n1,2⋯𝑛. In this game, Toph can operate the deck of the cards. We may wish to assume that the cards from the top to the bottom of the deck are p1,p2,⋯pn𝑝1,𝑝2,⋯𝑝𝑛 (a permutation), then each operation must be one of the following two cases:

  1. Place the top card at the bottom of the deck, that is, change the order of the deck into p2,p3⋯pn,p1𝑝2,𝑝3⋯𝑝𝑛,𝑝1.
  2. Place the second card from the top at the bottom of the deck, that is, change the order of the deck into p1,p3⋯pn,p2𝑝1,𝑝3⋯𝑝𝑛,𝑝2

Now, you know that the initial order(from top to bottom) of Toph's deck is a1,a2,⋯an𝑎1,𝑎2,⋯𝑎𝑛, and Toph wants to change the order of the deck into b1,b2,⋯bn𝑏1,𝑏2,⋯𝑏𝑛 after some operations. Please construct the operation sequence to help Toph implement the change.

Toph has no patience. So the number of operations should not exceed n2𝑛2.

Input

The first line contains an integer T𝑇 , indicating the number of test cases.

For each test case:

  • The first line contains an integer, n(3≤n≤1000)𝑛(3≤𝑛≤1000), indicating the number of Toph's cards.
  • The second line contains n𝑛 integers a1,a2,⋯an𝑎1,𝑎2,⋯𝑎𝑛, a permutation indicating the order of the deck initially.
  • The third line contains n𝑛 integers b1,b2,⋯bn𝑏1,𝑏2,⋯𝑏𝑛, a permutation indicating the order of the deck want to make.

It is guaranteed that the sum of n𝑛 in T𝑇 test cases is not exceed 10001000.

Output

For each test case:

  • Output a line, which contains a string s1s2…sk(si∈{1,2}, 1≤i≤k)𝑠1𝑠2…𝑠𝑘(𝑠𝑖∈{1,2}, 1≤𝑖≤𝑘) as your operation sequence. The length of the string should not exceed n2𝑛2, or you will get "Wrong Answer".
  • If there are multiple solutions, output any of them.
原题链接:Problem - L - Codeforces

题目大意:本题的大概意思就是给你两个数组,只能进行两个操作1和2(下面会说1,2,的操作分别是什么),问怎么将第一个数组改成第二个数组,将操作1,2写出来。有多个答案只写出一个即可

操作1:将数组第一个元素放到最后一个位置

操作2:将数组的第二个元素放到最后一个位置

观察题目可以知道有很多种转换的方法。我们可以用到冒泡排序的思想来优化这个题。我们可以依照第二个数组为标准对第一个数组进行比较,如果符合题意就不变,否则交换二者的位置。

主要冒泡排序的代码
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<n;j++)
		{
			if (d[a[j]]>d[a[j+1]])//用d数组来存权重,代表用d数组的顺序进行比较
			{
				cout<<"2";	
				swap(a[j],a[j+1]);//如果不一样就直接交换再输出2
			}
			else cout<<"1";//否则输出1
		}
		cout<<"1";//注意这里每次循环完一次都要输出1,可以自己模拟试一试,每次再输出一个1才符合
	}
正确代码
#include <bits/stdc++.h>
#define int long long 
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define PII pair<int,int> 
using namespace std;
const int N =1e5;
int a[N],b[N],d[N];

void solve ()
{
	int n;cin>>n;
	for (int i=1;i<=n;i++) cin>>a[i];
	for (int i=1;i<=n;i++) 
	{
		cin>>b[i];
		d[b[i]]=i;
	}
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<n;j++)
		{
			if (d[a[j]]>d[a[j+1]])
			{
				cout<<"2";	
				swap(a[j],a[j+1]);
			}
			else cout<<"1";
		}
		cout<<"1";
	}
	cout<<'\n';
}

signed main ()
{
	IOS;
	int T=1;
	cin>>T;
	while (T--) solve ();
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值