zoukankan      html  css  js  c++  java
  • D. Toy Sum(cf)

    http://codeforces.com/problemset/problem/405/D

    题意:已知集合S={1,2,3......1000000},s=1000000,从集合S中选择n个数,X={x1,x2,x3,...xn},计算sum=x1-1+x2-1+....+xn-1;从剩下的元素Y={y1,y2,y3,...}中选择一些数,使得s-y1+s-y2+...=sum,输出yi的个数及yi。

    思路:可以根据元素的对称性来找yi。因为x-1=s-y,则y=s-x+1,即对于每一个x,对应的可选择一个y=s-x+1,如果y不在X集合中,则直接输出y。如果y也在X集合中,由于(x-1)+(y-1)=x-1+s-x+1-1=s-1,此时只要在Y集合中选两个对称的数输出即可。因为(s-y1)+(s-y2) = 2s-(y1+y2) = 2s-(s+1) = s-1,两者的最终结果是相同的,故可以互相替代。所以可以找出符合条件的n个y元素。

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 const int s = 1000000;
     5 int vis[s];
     6 
     7 int main()
     8 {
     9     int n,x;
    10     while(~scanf("%d",&n))
    11     {
    12 
    13         memset(vis,0,sizeof(vis));
    14         for (int i = 0; i < n; i++)
    15         {
    16             scanf("%d",&x);
    17             vis[x] = 1;
    18         }
    19         int cnt = 0;
    20         printf("%d
    ",n);
    21         for (int i = 1; i <= s; i++)
    22         {
    23             if (vis[i]&&!vis[s-i+1])
    24             {
    25                 printf("%d ",s-i+1);
    26                 cnt++;
    27             }
    28         }
    29         for (int i = 1; i <= s&&cnt!=n; i++)
    30         {
    31             if (!vis[i]&&!vis[s-i+1])
    32             {
    33                 printf("%d %d ",i,s-i+1);
    34                 cnt+=2;
    35             }
    36         }
    37         puts("");
    38     }
    39     return 0;
    40 }
    View Code
  • 相关阅读:
    [leetcode-91-Decode Ways]
    [leetcode-72-Edit Distance]
    [leetcode-67-Add Binary]
    [leetcode-137-Single Number II]
    [leetcode-60-Permutation Sequence]
    [leetcode-55-Jump Game]
    [leetcode-18-4Sum]
    [leetcode-15-3Sum]
    [leetcode-47-Permutations II]
    easyui tabs update 强制刷新页面
  • 原文地址:https://www.cnblogs.com/lahblogs/p/3621602.html
Copyright © 2011-2022 走看看