zoukankan      html  css  js  c++  java
  • Codeforces Round #238 (Div. 2) D. Toy Sum

    D. Toy Sum
     
    time limit per test:1 second
    memory limit per test:256 megabytes
    input:standard input
    output:standard output

    Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to solve more problems, so he decided to play a trick on Chris.

    There are exactly s blocks in Chris's set, each block has a unique number from 1 to s. Chris's teacher picks a subset of blocks X and keeps it to himself. He will give them back only if Chris can pick such a non-empty subset Y from the remaining blocks, that the equality holds:

    "Are you kidding me?", asks Chris.

    For example, consider a case where s = 8 and Chris's teacher took the blocks with numbers 1, 4 and 5. One way for Chris to choose a set is to pick the blocks with numbers 3 and 6, see figure. Then the required sums would be equal: (1 - 1) + (4 - 1) + (5 - 1) = (8 - 3) + (8 - 6) = 7.

    However, now Chris has exactly s = 106 blocks. Given the set X of blocks his teacher chooses, help Chris to find the required set Y!

    Input

    The first line of input contains a single integer n (1 ≤ n ≤ 5·105), the number of blocks in the set X. The next line contains n distinct space-separated integers x1, x2, ..., xn (1 ≤ xi ≤ 106), the numbers of the blocks in X.

    Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.

    Output

    In the first line of output print a single integer m (1 ≤ m ≤ 106 - n), the number of blocks in the set Y. In the next line output m distinct space-separated integers y1, y2, ..., ym (1 ≤ yi ≤ 106), such that the required equality holds. The sets X and Y should not intersect, i.e. xi ≠ yj for all i, j (1 ≤ i ≤ n; 1 ≤ j ≤ m). It is guaranteed that at least one solution always exists. If there are multiple solutions, output any of them.

    Sample test(s)
    Input
    3
    1 4 5
    Output
    2
    999993 1000000
    Input
    1
    1
    Output
    1
    1000000
    讲解:题目大意是说,范围为大于等于 1 ,小于等于 1000000 ;首先从中选出 n 个数,每个数减去 1 ,假设和为 ans ;
    然后需要你从中取出 m 个数,且不能与给的数重复,用1000000减去你选出的每个数,然后求和,和也为 ans ;
    然后就寻找吧,唉,这题咋这么绕呢,看着简单,好难写啊;
     1 #include <set>
     2 #include <cstdio>
     3 #include <string>
     4 #include <vector>
     5 #include <cstring>
     6 #include <iostream>
     7 #include <algorithm>
     8 using namespace std;
     9 const int N = 1000100;
    10 int a[N];
    11 bool mark[N];
    12 int main(){
    13     int n;
    14     cin>>n;
    15     int s= 1000000;
    16     set<int>y;
    17     vector<int>notexist;
    18     for(int i=0;i<n;i++){
    19         scanf("%d",&a[i]);
    20         mark[a[i]] = true;
    21     }
    22     for(int i=1;i<=s;i++){
    23         if(mark[i]&&!mark[s+1-i])  //已经标记过了,并且差没出现过,则存入容器;
    24         {
    25             y.insert(s+1-i);
    26         }
    27         if(!mark[i]&&!mark[s+1-i]) //都没出现过;
    28            notexist.push_back(i);
    29     }
    30     int j = 0;
    31     for(int i=0; i<=s/2; i++){
    32         if(mark[i] && mark[s+1-i])//说明,需要重新插入连个没有被标记的数,固定的和为 s+1 ;
    33         {
    34             y.insert(notexist[j]);
    35             y.insert(s+1-notexist[j]);
    36             j++;
    37         }
    38     }
    39     cout<<n<<endl;
    40     set<int>::iterator it = y.begin();
    41     while(it!=y.end())
    42         {
    43           printf("%d ",*it);
    44           it++;
    45        }
    46        return 0;
    47 }
  • 相关阅读:
    HashMap源码解读(转)
    使用javascript开发2048
    程序猿接私活经验总结,来自csdn论坛语录
    Cocos2d-x实例:设置背景音乐与音效- AppDelegate实现
    Jenkins(二)
    SVN高速新手教程
    Visio Premium 2010密钥+破解激活方法
    oracle中LAG()和LEAD()等分析统计函数的使用方法(统计月增长率)
    shell语法简单介绍
    memset函数具体说明
  • 原文地址:https://www.cnblogs.com/lovychen/p/3685916.html
Copyright © 2011-2022 走看看