Codeforces Round #418 B

An express train to reveries

题意:给2长度为n的数列a b,a b中至少存在一个i使得ai!=bi,构造一个数列p,使得恰好存在一个i使得ai!=pi,且恰好存在一个j使得aj!=pj,且p里面的数为1-n且无重复,输出p,保证有解

思路:题目保证有解,可知,最多存在2个i使得ai!=bi,最少一个,如果只有一个,那么好办,令p=a,将pi换成1-n中没有出现的那个数就可以了,如果有存在2个不等的数,那么令p=a(除开两个ai!=bi的位置),找出未出现的2个数,将2个数分别放在ai!=bi的位置上,判断一下是否满足要求(使得恰好存在一个i使得ai!=pi,且恰好存在一个j使得aj!=pj),若满足输出答案,若不满足,则调换2个数的位置,此时一定满足,输出答案

AC代码:

#include "iostream"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "set"
#include "map"
#include "algorithm"
#include "stdio.h"
#include "math.h"
#define ll long long
#define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const int N=1e5+100;
int a[1005],b[1005],p[1005],mp[1005],n,mk1,mk2,k1,k2,k;
int main(){
    cin>>n;
    for(int i=1; i<=n; ++i){
        cin>>a[i];
    }
    for(int i=1; i<=n; ++i){
        cin>>b[i];
        if(a[i]!=b[i] && !mk1){
            mk1=mk2=i;
            k++;
        }
        else if(a[i]!=b[i]){
            mk2=i;
            k++;
        }
        else p[i]=a[i],mp[p[i]]++;
    }
    if(k==1){
        for(int i=1; i<=n; ++i){
            if(!mp[i]){
                p[mk1]=i;
                break;
            }
        }
    }
    else{
        for(int i=1; i<=n; ++i){
            if(!mp[i] && k1==0){
                k1=i;
                k--;
            }
            else if(!mp[i]){
                k2=i;
                k--;
            }
            if(!k) break;
        }
        p[mk1]=k1,p[mk2]=k2;
        if((p[mk1]!=a[mk1] && p[mk1]!=b[mk1]) || (p[mk2]!=a[mk2] && p[mk2]!=b[mk2])){
            p[mk1]=k2,p[mk2]=k1;
        }
    }
    for(int i=1; i<=n; ++i){
        cout<<p[i]<<" ";
    }
    return 0;
}
/*
5
1 2 3 4 3
1 2 5 4 5
5
4 4 2 3 1
5 4 5 3 1
4
1 1 3 4
1 4 3 4
*/
时间: 2024-10-08 09:29:37

Codeforces Round #418 B的相关文章

loj #6091. 「Codeforces Round #418」幻想特快

#6091. 「Codeforces Round #418」幻想特快 1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio> 5 #include<queue> 6 using namespace std; 7 #define maxn 10000 8 int n,a[maxn],b[maxn],p[maxn],nxt1,nxt2,tot,

Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection(双指针)

题目链接:Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection 题意: 给你一个字符串,有q个询问,每个询问一个x和一个字符 o. 现在让你在原来的字符串上最多改变x个字符,问能构成最长的o子串的长度. 题解: 一共有26*1500种状态,对于每个状态用双指针滚一滚就行了. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=(a);i<

Codeforces Round #418 C

C An impassioned circulation of affection 题意:给一个长度为n的字符串,q个询问,询问若将任意m个字符替换成任意字符,可以得到的最长的连续的c是多少 思路:询问20w,字符串长度1500,其实只有1500*26个询问是有效的,其他询问都是重复的,所以预处理出每个字符1-n的答案,询问时直接输出就可以了,预处理用尺取法,用t表示当前答案,u表示当前还可以替换几个字符,每次往前尺取,若当前字符为c则t++,否则若u>0 则 t++ 且 u--(使用了一次替换

Codeforces Round #418 (Div. 2)D

给n个圆要么包含,要么相分离,没有两个公共点,当成一棵树,把包含的面积大的放在上面 如图最上面的par记为-1,level记为0,当par==-1||level==1时就加否则减, 就是第一,二层先加,第三层减,然后后面的一直交替就行了 #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #includ

codeforces round 418 div2 补题 CF 814 A-E

A An abandoned sentiment from past 水题 #include<bits/stdc++.h> using namespace std; int a[300],b[300],n,k; bool cmp(int a,int b) { return a>b; } int main() {//freopen("t.txt","r",stdin); scanf("%d%d",&n,&k); f

Codeforces Round #418 (Div. 2)

A - An abandoned sentiment from past 由于所有的数组B内所有的数都不同,因此当k > 1是就可以使该序列不递增 当k = 1是,带入B[0],判断序列A是否递增就可以啦 #include <bits/stdc++.h> using namespace std; const int MAXN = 100008; int a[MAXN], b[MAXN]; int main() { int n, k; scanf("%d%d", &am

Codeforces Round #418 (Div. 2) C

Description Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it! Still unsatisfied with the garland, Nadeko

loj #6090. 「Codeforces Round #418」尘封思绪

https://loj.ac/problem/6090 1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio> 5 #include<cmath> 6 #include<queue> 7 using namespace std; 8 #define maxn 10900 9 int n,k,a[maxn],b[maxn],cnt

Codeforces Round #418 B--An express train to reveries

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 nig