zoukankan      html  css  js  c++  java
  • codeforces 899C Dividing the numbers

    Petya has n integers: 1, 2, 3, ..., n. He wants to split these integers in two non-empty groups in such a way that the absolute difference of sums of integers in each group is as small as possible.

    Help Petya to split the integers. Each of n integer

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 int a[60000];
     6 int main()
     7 {
     8     int n, sum, p, s=0, q;
     9     scanf("%d",&n);
    10     if(n%2==0)
    11     sum=n/2*(n+1);
    12     else
    13         sum=(n+1)/2*n;
    14     if(sum%2==0)
    15         printf("0
    ");
    16     else
    17         printf("1
    ");
    18     p=sum/2;
    19     q=p%(n+1);
    20     if(q!=0)
    21     a[s++]=q;
    22     p-=q;
    23     for(int i=1; i<=n&&p!=0; i++)
    24     {
    25         if(i!=q&&(n+1-i)!=q)
    26         {
    27             a[s++]=i;
    28             a[s++]=n+1-i;
    29             p-=n+1;
    30         }
    31     }
    32     printf("%d", s);
    33     for(int i=0; i<s; i++)
    34         printf(" %d", a[i]);
    35     printf("
    ");
    36 

    should be exactly in one group.

    Input

    The first line contains a single integer n (2 ≤ n ≤ 60 000) — the number of integers Petya has.

    Output

    Print the smallest possible absolute difference in the first line.

    In the second line print the size of the first group, followed by the integers in that group. You can print these integers in arbitrary order. If there are multiple answers, print any of them.

    Examples
    input
    4
    output
    0
    2 1 4
    input
    2
    output
    1
    1 1
    Note

    In the first example you have to put integers 1 and 4 in the first group, and 2 and 3 in the second. This way the sum in each group is 5, and the absolute difference is 0.

    题目大意,输入一个数字n,有一个集合包含从1到n的每个数字,现在把集合拆分成两个,要求使两个集合元素和的差的绝对值尽可能小。输出这个值和其中一个集合元素

    两个集合差很容易分析:当sum为偶数,绝对差值为0,奇数为1(sum=(n+1)*n/2);关键是输出集合元素,分析其中一个集合元素和为sum/2,先输出sum/2%(n+1),剩下的为n+1的倍数,直接以n,1;n-1,2,,,,,,等输出即可

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 int a[60000];
     6 int main()
     7 {
     8     int n, sum, p, s=0, q;
     9     scanf("%d",&n);
    10     if(n%2==0)
    11     sum=n/2*(n+1);
    12     else
    13         sum=(n+1)/2*n;
    14     if(sum%2==0)
    15         printf("0
    ");
    16     else
    17         printf("1
    ");
    18     p=sum/2;
    19     q=p%(n+1);
    20     if(q!=0)
    21     a[s++]=q;
    22     p-=q;
    23     for(int i=1; i<=n&&p!=0; i++)
    24     {
    25         if(i!=q&&(n+1-i)!=q)
    26         {
    27             a[s++]=i;
    28             a[s++]=n+1-i;
    29             p-=n+1;
    30         }
    31     }
    32     printf("%d", s);
    33     for(int i=0; i<s; i++)
    34         printf(" %d", a[i]);
    35     printf("
    ");
    36     return 0;
    37 }
    View Code
  • 相关阅读:
    python爬虫实例--爬取拉勾网
    面试时,如何回答你还有什么想要了解的?
    深入理解三次握手四次挥手以及使用scapy实现ddos雏形
    解决socket粘包的两种low版模式 os.popen()和struct模块
    浅谈osi模型 三次握手 四次挥手 ddos攻击原理
    渲染相关书籍
    unity 场景编辑器物体加图标
    音乐模拟器
    3d服装制作软件
    uv投影插值
  • 原文地址:https://www.cnblogs.com/zhulei2/p/8118725.html
Copyright © 2011-2022 走看看