[Codeforces Round #452 (Div. 2)] Dividing the numbers

探讨如何将1至n的整数等分为两组,使得两组内整数之和的绝对差值最小,并给出具体算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 integers should be exactly in one group.

Input
The first line contains a single integer n (2n60000) — 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.
In the second example there are only two integers, and since both groups should be non-empty, you have to put one integer in the first group and one in the second. This way the absolute difference of sums of integers in each group is 1.

题目大意
1n分成两组,询问两组和的差值的最小值,并打印一组中元素。

思路
如果n4的整数倍的话,那么可以将1n分到一组,将2n1分到另一组;将3n2分到一组,将4n3分到另一组……将n/21n/2+2分到一组,将n/2n/2+1分到另一组。
如果n%4=1,那么将1分到某一组,然后剩下的按上面的分法分。
如果n%4=2,那么将1分到某一组,将2分到另一组,然后剩下的按上面的方法分。
如果n%4=3,那么将12分到同一组,将3分到另一组,然后剩下的还是按上面的方法分。
这样的话所有的情况都讨论完了。

代码

#include <cstdio>

const int maxn=60000;

int n;

int main()
{
  scanf("%d",&n);
  if(n%4==0)
    {
      printf("0\n%d ",n/2);
      for(register int i=1; i<=n/2; i+=2)
        {
          printf("%d %d ",i,n-i+1);
        }
    }
  else if(n%4==1)
    {
      printf("1\n%d ",n/2);
      for(register int i=2; i<=n/2+1; i+=2)
        {
          printf("%d %d ",i,n-i+2);
        }
    }
  else if(n%4==2)
    {
      printf("1\n%d 1 ",n/2);
      for(register int i=3; i<=n/2+1; i+=2)
        {
          printf("%d %d ",i,n-i+3);
        }
    }
  else
    {
      printf("0\n%d 3 ",n/2);
      for(register int i=4; i<=n/2+2; i+=2)
        {
          printf("%d %d ",i,n-i+4);
        }
    }
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值