传送门: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. 转:分布式事务之TCC服务设计和实现注意事项

    由公司微服务培训引起的一丢丢对TCC的好奇 原文:https://yq.aliyun.com/articles/609854 一.TCC简介 TCC是一种比较成熟的分布式事务解决方案,可用于解决跨库操 ...

  2. selenium获取弹窗提示

    1.点击保存给的提示是几秒钟,遮罩显示 2. 其他弹窗处理方法 http://blog.csdn.net/Real_Tino/article/details/59068827

  3. 【Android】开发优化之——调优工具:TrackView,Method Profiling

    Android SDK自带的tool TrackView 位于 sdk的tools文件夹下.使用方法为:进入到tools下,执行 traceview e:\loginActivityTracing.t ...

  4. 一条SQL语句求每月连续低温时间

    近期为某个项目写存储过程做统计.其中有个是这样的: 求每个月至少连续3个小时低于某个温度值的时间(小时). 假设有个全年温度表: CREATE TABLE #t(m INT, h INT ,t DEC ...

  5. 5200 fqy的难题----2的疯狂幂

    5200 fqy的难题----2的疯狂幂  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description ...

  6. dynamic关键字的使用

    https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConvert.htm 在使用DeserializeObject函数进行 ...

  7. ListView实现简单列表

    ListView实现简单列表 效果图: 啥也没干的ListView张这样: fry.Activity01 package fry; import com.example.ListView.R; imp ...

  8. 【BZOJ 1598】 牛跑步

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1598 [算法] A*求k短路 [代码] #include<bits/stdc+ ...

  9. BZOJ1067 [SCOI2007]降雨量 RMQ???

    求救!!!神犇帮我瞅瞅呗...未完...调了2个半小时线段树,没调出来,大家帮帮我啊!!! 小詹用st表写. 我的思路就是把中间空着的年份设为无限,然后一点点特判就行了...然而没出来... [SCO ...

  10. C++ this指针 全部

    在每一个成员函数中都包含一个特殊的指针,这个指针的名字是固定的.叫做this.它是指向本类对象的指针,它的值是当前被调用的成员函数所在的对象的起      始地址.例如:当调用成员函数a.volume ...