传送门:http://codeforces.com/contest/899/problem/C

本题是一个数学问题——集合划分。

将集合{1,2,...,n}划分成两个集合,使得两个集合的元素之和的绝对差值最小。

首先,考虑最简单的操作:

第1步:将元素1和n置入集合A

第2步:将元素2和n-1置入集合B

第3步:将元素3和n-2置入集合A

……

i步:将元素in+1-i,当i为奇数时置入集合A,偶数时置入集合B

……

n/2步:将元素n/2和n/2+1置入集合B

如此,集合A中的元素之和与集合B中的元素之和相等,绝对差为0。

为保证第n/2步将元素置入集合B,则应保证n是4的整数倍。

于是,若n是4的整数倍,则划分的最小绝对差为0,集合A={1,n,3,n-2,...,n/2-1,n/2+2},集合B={2,n-1,4,n-3,...,n/2,n/2+1}。

n不可被4整除,则可以考虑预处理{1,2,...,n}的前若干项构成的集合{1,2,...,x},之后再处理集合{x+1,...,n}。当然,为了使得集合{x+1,...,n}易于处理,应保证其项数n-x是4的整数倍。于是,x可以取n%4。

以下是按照x值分类的预处理过程:

①若x=1,则预处理{1}:将1置入集合A,此时最小绝对差为1;

②若x=2,则预处理{1,2}:将1置入集合A,2置入集合B,此时最小绝对差为1;

③若x=3,则预处理{1,2,3}:将1和2置入集合A,3置入集合B,此时最小绝对差为0。

之后,{x+1,...,n}的处理过程类似于最初提及的操作——可将集合{x+1,...,n}看作{1,...,n-x}中的每一个元素增加x得到的集合,其中n-x是4的整数倍。

参考程序如下:

#include <stdio.h>
#define MAX_N 30010 int a[MAX_N]; int main(void)
{
int n;
scanf("%d", &n);
int x = n % ;
int cnt = ;
int dif;
if (x == ) dif = ;
else if (x == ) {
dif = ;
a[cnt++] = ;
}
else if (x == ) {
dif = ;
a[cnt++] = ;
}
else if (x == ) {
dif = ;
a[cnt++] = ;
a[cnt++] = ;
}
for (int i = x + ; i <= x + (n - x) / ; i += ) {
a[cnt++] = i;
a[cnt++] = n + x + - i;
}
printf("%d\n%d", dif, cnt);
for (int i = ; i < cnt; i++)
printf(" %d", a[i]);
return ;
}

Codeforces 899C - Dividing the numbers的更多相关文章

  1. Codeforces Round #452 (Div. 2)-899A.Splitting in Teams 899B.Months and Years 899C.Dividing the numbers(规律题)

    A. Splitting in Teams time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. Codeforces Round #452 (Div. 2) C. Dividing the numbers(水)

    C. Dividing the numbers Petya has n integers: 1, 2, 3, ..., n. He wants to split these integers in t ...

  3. [codeforces 55]D. Beautiful numbers

    [codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...

  4. Dividing the numbers CodeForces - 899C (构造)

    大意: 求将[1,n]划分成两个集合, 且两集合的和的差尽量小. 和/2为偶数最小差一定为0, 和/2为奇数一定为1. 显然可以通过某个前缀和删去一个数得到. #include <iostrea ...

  5. 【Codeforces Round #452 (Div. 2) C】 Dividing the numbers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] n为偶数. l = 1, r = n (l,r)放在一组 l++,r-- 新的l,r放在另外一组 直到l+1==r 这个时候,判断两 ...

  6. CodeForces - 1245A Good ol' Numbers Coloring (思维)

    Codeforces Round #597 (Div. 2 Consider the set of all nonnegative integers: 0,1,2,-. Given two integ ...

  7. CodeForces 682A Alyona and Numbers (水题)

    Alyona and Numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/A Description After fi ...

  8. Codeforces 449D Jzzhu and Numbers

    http://codeforces.com/problemset/problem/449/D 题意:给n个数,求and起来最后为0的集合方案数有多少 思路:考虑容斥,ans=(-1)^k*num(k) ...

  9. Codeforces899C Dividing the numbers(数论)

    http://codeforces.com/problemset/problem/899/C tot为奇数时,绝对差为1:tot为偶数时,绝对差为0. 难点在于如何输出. #include<io ...

随机推荐

  1. [ACM] POJ 1942 Paths on a Grid (组合)

    Paths on a Grid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21297   Accepted: 5212 ...

  2. uva 11605 - Lights inside a 3d Grid(概率)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=2652" style=""& ...

  3. Why is try {…} finally {…} good; try {…} catch{} bad?

    http://stackoverflow.com/questions/128818/why-is-try-finally-good-try-catch-bad The big difference i ...

  4. 【BZOJ 3790】 神奇项链

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3790 [算法] manacher + 贪心 [代码] #include<bit ...

  5. 动态规划---区间dp

    今天写内网题,连着写了两道区间dp,这里就总结一下. 区间dp思想主要是先枚举f[i][j]中的i,再枚举j,再枚举一个1~j之间的变量k,一般是f[i][j] = max(f[i][j],f[i][ ...

  6. 杂项-TMod:常见错误

    ylbtech-杂项-TMod:常见错误 1.返回顶部 1. 1.1. {Template Error} TypeError: dateDiff is not a function at Array. ...

  7. 单向链表的归并排序——java实现

    在做Coursera上的Algorithms第三周测验练习的时候有一道链表随机排序问题,刚开始没有什么思路,就想着先把单向链表归并排序实现了,再此基础上进行随机排序的改造.于是就结合归并排序算法,实现 ...

  8. 移动App服务端架构设计

      我从事手机app服务端开发现在已经是3个年头,自己也整理出了一套相对好用的服务架构,写出来,跟大家一起分享.如有不足,还请多指教. 一:基础流程图. 其实有一点还需要加上,就是对json的压缩和加 ...

  9. Zeppelin0.6.2+sparkR2.0.2环境搭建

    0.序 先吐槽一下网上旧版本的Zeppelin和R的安装,让我折腾了几个小时. 不过最终还是调通了也不容易,其实我现在一点R都没有学呢,只是刚看了一节课,但是这个工具既然出现在了Spark中,我想它还 ...

  10. 树莓派-解决apt-get upgrade速度慢的方法[更换阿里云源]

    执行 apt-get upgrade 遇到速度慢的原因: 使用国外软件源 解决方法也很简单,将源换为国内环境即可,我选择阿里云 步骤 1.备份为 sources.list sudo cp /etc/a ...