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
  • 相关阅读:
    织梦CMS如何在首页调用指定的文章 idlist
    织梦列表页5行加横线的实现方法
    在织梦dedecms中实现“文章标题-栏目名称-网站名”导航
    织梦dedecms中修改标题与简略标题长度的方法
    织梦dedecms如何显示所有文章列表
    织梦dedecms页面中增加二维码功能的实现方法
    dedecms中去除首页index.html的方法
    概率期望学习笔记
    NOIP2013货车运输
    牛客网暑期ACM多校训练营(第三场)
  • 原文地址:https://www.cnblogs.com/zhulei2/p/8118725.html
Copyright © 2011-2022 走看看