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
  • 相关阅读:
    ISAG协议中彩信支持的几种附件格式(河南电信)
    河南电信ISAG短信下行数据格式
    SQL中varchar和nvarchar有什么区别?
    通过一个很实用的例子让你学会TSQL编程的基本语法和思想
    在读取或者保存word时,程序捕获到word异常“word无法启动转换器mswrd632 wpc”
    工作基本搞定等待周五入职
    ClickOnce发布时,资源文件添加问题
    访问IIS元数据库失败
    一个随机产生中文简体字的一个类
    QQ抢车位外挂(续)
  • 原文地址:https://www.cnblogs.com/zhulei2/p/8118725.html
Copyright © 2011-2022 走看看