Codeforces 899C - Dividing the numbers
传送门: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步:将元素i和n+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的更多相关文章
- 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 ...
- 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 ...
- [codeforces 55]D. Beautiful numbers
[codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...
- Dividing the numbers CodeForces - 899C (构造)
大意: 求将[1,n]划分成两个集合, 且两集合的和的差尽量小. 和/2为偶数最小差一定为0, 和/2为奇数一定为1. 显然可以通过某个前缀和删去一个数得到. #include <iostrea ...
- 【Codeforces Round #452 (Div. 2) C】 Dividing the numbers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] n为偶数. l = 1, r = n (l,r)放在一组 l++,r-- 新的l,r放在另外一组 直到l+1==r 这个时候,判断两 ...
- CodeForces - 1245A Good ol' Numbers Coloring (思维)
Codeforces Round #597 (Div. 2 Consider the set of all nonnegative integers: 0,1,2,-. Given two integ ...
- CodeForces 682A Alyona and Numbers (水题)
Alyona and Numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/A Description After fi ...
- Codeforces 449D Jzzhu and Numbers
http://codeforces.com/problemset/problem/449/D 题意:给n个数,求and起来最后为0的集合方案数有多少 思路:考虑容斥,ans=(-1)^k*num(k) ...
- Codeforces899C Dividing the numbers(数论)
http://codeforces.com/problemset/problem/899/C tot为奇数时,绝对差为1:tot为偶数时,绝对差为0. 难点在于如何输出. #include<io ...
随机推荐
- ExtJs4.1布局具体解释
Border布局: Ext.onReady(function(){ Ext.QuickTips.init(); Ext.create('Ext.container.Viewport', ...
- effective C++ 读书笔记 条款11
条款11: 在operator= 中处理"自我赋值" 在实现operator=时考虑自我赋值是必要的就像 x=y .我们不知道变量x与y代表的值是否为同一个值(把x和y说成是一个指 ...
- bzoj1116 [POI2008]CLO——并查集找环
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1116 分析性质,只要有环,那么给环定一下向就满足了条件: 环上点的其他边可以指向外面,所以两 ...
- 444D
分类 首先我们要对询问分类,如果相差log级别就第一种询问,否则第二种. 第一种直接暴力lower_bound,复杂度玄学 第二种归并,复杂度玄学 但是就是过了.感觉很容易卡. #include< ...
- vim gvim技巧大全(9)(转载)
vim gvim技巧大全(9) 2 用命令}移动到这个段落的底部,标记为b3 输入命令:'a,'b move来移动文本.老版本的Vi编辑器不能很好的来处理多文件.但是Vim在处理多文件上却显得优秀得多 ...
- 最短路径问题(floyd)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1867 #include<stdio.h& ...
- php导出mysql源码
件名:db_backup.php 源代码如下: 复制代码 代码如下: <?php ini_set("max_execution_time", "180") ...
- 洛谷P3047 [USACO12FEB]Nearby Cows(树形dp)
P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...
- 关于函数提升在if语句中的表现
函数声明创建的函数在现代浏览器,在if语句中函数的声明不会提升,但是在老的IE版本中,if语句中的函数声明会提升 函数表达式在不同浏览器中函数声明都不会被提升,解决了不同浏览器的兼容性问题 关于函数提 ...
- [CodeForces522B] Photo to Remember
某一天,n个朋友在一起聚会,他们已经很久没见了,于是他们决定拍照留念. 简单的说,拍照的时候,每个人有一个高度和宽度,第i个的高度和宽度分别是hi和wi.这些人排成一条直线,照片的最小的面积必须包含所 ...