暑假集训日记--8.3--搜索

今天依旧是搜索……但是今天干的事情好像并不多……

上午看了看hdoj上的题……并没有A出一个……可能是水平还不够……于是看了会书。

下午是一场练习赛……上来第一道题就搜索。。。搜索啥……根本不是搜索……最近做搜索做疯了吗……那道水题就不说了。今天总结一下第二道水题吧……

题目如下:

Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.

On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with nmeteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bnrespectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 ≤ i ≤ n) exists, such that ai ≠ bi holds.

Well, she almost had it all — each of the sequences a and b matched exactly n - 1elements in Sengoku's permutation. In other words, there is exactly one i (1 ≤ i ≤ n) such that ai ≠ pi, and exactly one j (1 ≤ j ≤ n) such that bj ≠ pj.

For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.

Input

The first line of input contains a positive integer n (2 ≤ n ≤ 1 000) — the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.

The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of colours in the first meteor outburst.

The third line contains n space-separated integers b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of colours in the second meteor outburst. At least one i (1 ≤ i ≤ n) exists, such that ai ≠ bi holds.

Output

Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.

Input guarantees that such permutation exists.

Example
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4

题目大意:给一个n和两个数组的数字,每个数组包含n个数,要求输出的数列和第一个第二个数列有且各仅有一个数不同。

错误思路:这里刚开始就想用暴力。直接推……用很多条件……写了很长的代码……就是先确定相同的数字,然后再判断不相同的数字填几。在练习赛的过程中读错题了。忽略了输出结果也只能在1-n之间取值……

正确思路:先确定好相同的数。然后将不相同的位置进行标记,将相同位置的数字进行标记。然后分类讨论。

1、如果只有一个位置不相同,那么从1-n中找出没有标记过的那个数,填上,输出。

2、如果有两个位置不相同,则需要判断。这里可以利用对角线的数字。

源代码:

#include <iostream>

#include <algorithm>

#include <cstring>

#include <string>

#include <stdio.h>

using namespace std;

int num[1005],num1[1005],num2[1005];

int flag1[1005],num3[1005];

int main()

{

    int n;

    while(cin >> n)

    {

        memset(flag1,0,sizeof(flag1));

        memset(num,0,sizeof(num));

        memset(num1,0,sizeof(num1));

        memset(num2,0,sizeof(num2));

        int flag = 0;

    for (int i = 1; i <= n; i++)

        cin >> num1[i];

    for (int i = 1; i <= n; i++)

    {

        cin >> num2[i];

        if (num1[i] == num2[i])

        {

            num[i] = num1[i];

            flag1[num[i]] = 1;

        }

        else

        {

            num3[flag] = i;

            flag++;

        }

    }

    if (flag == 1)

    {

        for (int i = 1; i <= n; i++)

        {

            if (flag1[i] != 1)

            {

                num[num3[0]] = i;

                break;

            }

        }

    }

    else

    {

        if (flag1[num1[num3[0]]] == 0 && flag1[num2[num3[1]]] == 0)

        {

            num[num3[0]] = num1[num3[0]];

            num[num3[1]] = num2[num3[1]];

        }

        else

        {

            num[num3[0]] = num2[num3[0]];

            num[num3[1]] = num1[num3[1]];

        }

    }

    for (int p = 1; p <= n; p++)

        cout << num[p] << " " ;

    cout << endl;

    }

    return 0;

}



今天做的题也不多。看了会书。总的来说知识上收获不大。但是通过今天的练习赛,也教育了我,在看到题后不能想当然的就开始写。这是一个很大的毛病,对于我来说也是今天得到的最大的教训。拿到题后一定要仔仔细细想清楚,用什么方法比较好做。然后再去尝试。总的来说还是做题太少……还是要努力做题。

另外也该好好看看书了……看看资料,多复习一下,学习一下别人做题的经验。


希望明天可以做的更好。比今天收获大一些。

继续加油吧!




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值