Codeforces - 814B - An express train to reveries - 构造

http://codeforces.com/problemset/problem/814/B

构造题烦死人,一开始我还记录一大堆信息来构造p数列,其实因为s数列只有两项相等,也正好缺了一项,那就把两种情况构造出来暴力验证对不对就行了。

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

int n;
int s[1005];
int us[1005];
int t[1005];
int ut[1005];
int a[1005];

int main(){
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&s[i]);
        us[s[i]]++;
    }
    for(int i=0;i<n;i++){
        scanf("%d",&t[i]);
        ut[t[i]]++;
    }

    int t1=0;
    for(int i=1;i<=n;i++){
        if(us[i]==0){
            t1=i;
        }
    }

    vector<int> dif;
    for(int i=0;i<n;i++){
        a[i]=s[i];
        if(us[s[i]]==2){
            dif.push_back(i);
            //printf("i=%d\n",i);
        }
    }

    int tmp;
    for(auto i:dif){
        tmp=a[i];
        a[i]=t1;
        int cnt=0;
        for(int j=0;j<n;j++){
            if(a[j]!=t[j])
                cnt++;
        }
        if(cnt==1){
            for(int j=0;j<n;j++){
                printf("%d%c",a[j],(" \n"[j==n-1]));
            }
            return 0;
        }
        a[i]=tmp;
    }

}

原文地址:https://www.cnblogs.com/Yinku/p/10327676.html

时间: 2024-07-31 22:27:47

Codeforces - 814B - An express train to reveries - 构造的相关文章

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

CF814B An express train to reveries

思路: 模拟,枚举. 实现: 1 #include <iostream> 2 using namespace std; 3 4 const int N = 1005; 5 6 int a[N], b[N], cnt[N], n, x, y; 7 8 int main() 9 { 10 cin >> n; 11 for (int i = 0; i < n; i++) cin >> a[i], cnt[a[i]]++; 12 for (int i = 0; i <

Codeforces Gym101341I:Matrix God(随机化构造矩阵降维)***

http://codeforces.com/gym/101341/problem/I 题意:给三个N*N的矩阵,问a*b是否等于c. 思路:之前遇到过差不多的题目,当时是随机行(点),然后验证,不满足就退出.还有暴力弄的(当时的数据是500).也提到过这样的解法,当时没用这种做法做一遍. 就是构造多一个矩阵d. 由于矩阵乘法满足结合律:a * (b * d) = c * d. d是一个n*1的矩阵,b * d之后会得到一个n * 1的矩阵,因此只需要O(n^2)就可以验证是否正确. 1 #inc

Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉

Problem H. Hard TestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description Andrew is having a hard time preparing his 239-th contest for Petrozavodsk. This time the solution to the problem is based on Di

Codeforces Beta Round #8A Train and Peter (string的运用)

Train and Peter Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on CodeForces. Original ID: 8A 64-bit integer IO format: %I64d      Java class name: (Any) Prev Submit Status Statistics Discuss Next Peter likes to travel by train.

Codeforces 1163E Magical Permutation [线性基,构造]

codeforces 思路 我顺着图论的标签点进去的,却没想到-- 可以发现排列内每一个数都是集合里的数异或出来的. 考虑答案的上界是多少.如果能用小于\(2^k\)的数构造出\([0,2^k-1]\)内所有的数,那么答案就对这个\(k\)取\(\max\).很显然这一定是上界. 考虑能不能构造出一组解.把\([1,2^k-1]\)的数拎出来插入线性基里得到一组极大线性无关组,那么显然它的\(size\)就是\(k\).由于它线性无关,所以任意选取一个子集得到的异或和都不会相同,所以考虑把\(0

Codeforces Round #276 (Div. 2)C. Bits(构造法)

这道题直接去构造答案即可. 对于l的二进制表示,从右到左一位一位的使其变为1,当不能再变了(再变l就大于r了)时,答案就是l. 这种方法既可以保证答案大于等于l且小于等于r,也可以保证二进制表示时的1最多. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include&l

CodeForces 658C Bear and Forgotten Tree 3 (构造)

题意:构造出一个 n 个结点,直径为 m,高度为 h 的树. 析:先构造高度,然后再构造直径,都全了,多余的边放到叶子上,注意直径为1的情况. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <io

codeforces 509 D. Restoring Numbers(数学+构造)

题目链接:http://codeforces.com/problemset/problem/509/D 题意:题目给出公式w[i][j]= (a[i] + b[j])% k; 给出w,要求是否存在这样的数列,若存在则求出a,b 和k 题解:如果有符合条件的点的话那么a[i],b[j]可以任意转换比如说a[i],b[j]可以转化为a[i]-p,b[i]+p.所以只要存在 符合条件的解的a[i]可以为任意值.也就是说a[i]可以先赋值为0然后其他就都可以推出来了,当然开始推出后会发现 有负的,没事,