C. 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 integers 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.

 

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.

这场B C过的人数差不多,可是B死于平年的2月写成29天,挣扎N次才发现2333.....

C题用头铁方式过,一点AC乐趣都没- -

题目大意:

1~n个数划分为两个非空集合,求两个集合各自和的差值的最小值.

解题思路:

思路1:

如1 2 3 4 5 6,显然(1,6)和(2,5)能抵消,"3","4"两集合各一个恰好差值为1.显然遵照这个思路,差值最小值不是0就是1。通过不断一对对互相消去,再判断互相消去的对数奇偶来

得出为0还是1.  当差值为1时候按从前往后,从后往前各自输出(i+=2)能抵消对数个数,再讲正中央数字直接输出即可。

而当n为奇数时,我将 (1, 2),(3)提前抵消,剩下的数遵照上面思路继续下去就是了。

由于都只是if else的判断,所以复杂度极低- -

AC(头铁)代码:

#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <ctype.h>
//******************************************************
#define lrt (rt*2)
#define rrt (rt*2+1)
#define LL long long
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
#define exp 1e-8
//***************************************************
#define eps 1e-8
#define inf 0x3f3f3f3f
#define INF 2e18
#define LL long long
#define ULL unsigned long long
#define PI acos(-1.0)
#define pb push_back
#define mk make_pair #define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define SQR(a) ((a)*(a))
#define Unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define min4(a,b,c,d) min(min(a,b),min(c,d))
#define max4(a,b,c,d) max(max(a,b),max(c,d))
#define max5(a,b,c,d,e) max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e) min(min3(a,b,c),min(d,e))
#define Iterator(a) __typeof__(a.begin())
#define rIterator(a) __typeof__(a.rbegin())
#define FastRead ios_base::sync_with_stdio(0);cin.tie(0)
#define CasePrint pc('C'); pc('a'); pc('s'); pc('e'); pc(' '); write(qq++,false); pc(':'); pc(' ')
#define vi vector <int>
#define vL vector <LL>
#define For(I,A,B) for(int I = (A); I < (B); ++I)
#define FOR(I,A,B) for(int I = (A); I <= (B); ++I)
#define rFor(I,A,B) for(int I = (A); I >= (B); --I)
#define Rep(I,N) For(I,0,N)
#define REP(I,N) FOR(I,1,N)
using namespace std;
const int maxn=2e5+;
vector<int>Q[maxn];
int main()
{
int n,ans,num;
while(cin>>num)
{
if(num==)
{
puts(""); printf("1 1");
}
if(num%==&&num>)
{
n=num/;
if(n%==)
{
puts("");
printf("%d",n);
n/=;ans=;
for(int i=;i<=num&&n;i+=)
{
ans++;
printf(" %d",i);
if(ans==n) break;
}
ans=;
for(int i=num;n;i-=)
{
ans++;
printf(" %d",i);
if(ans==n) break;
}
}
else
{
puts("");
printf("%d",n);
n/=;ans=;
for(int i=;i<=num;i+=)
{
ans++;
printf(" %d",i);
if(ans==n) break;
}
ans=;
for(int i=num;;i-=)
{
ans++;
printf(" %d",i);
if(ans==n) break;
}
printf(" %d",num/);
}
}
else if(num%==&&num>)
{ // 5 9
n=(num-)/;
if(n%==)
{
puts("");
printf("%d",n+);
printf("");
n/=;ans=;
for(int i=;i<=num&&n;i+=)
{
ans++;
printf(" %d",i);
if(ans==n) break;
}
ans=;
for(int i=num;n;i-=)
{
ans++;
printf(" %d",i);
if(ans==n) break;
}
}
else
{ // 1 3
puts("");
printf("%d",n+);
printf("");
n/=;ans=;
for(int i=;i<=num&&n;i+=)
{
ans++;
printf(" %d",i);
if(ans==n) break;
}
ans=;
for(int i=num;n;i-=)
{
ans++;
printf(" %d",i);
if(ans==n) break;
}
printf(" %d",(+num)/);
}
}
cout<<endl;
}
return ;
}

思路2:

求出n个数和,直接判断奇偶,得出差值为1还是0,然后除以2得出每个集合的sum应该凑到多少。

n个数两个集合必定分别为m,m+1个数字,或者分别为m,m个数,所以用sum/m得到平均值ave,然后讲ave前后的数字对分别输出,输出m/2对的数即可.

或者用sum%(m+1)后,其他数输出为(1,n),(2,n-1)这样的数字对即可,感觉这个思路复杂度更低,AC代码就是这个思路- -

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[60000];
int main()
{
int n, sum, p, s=0, q;
scanf("%d",&n);
if(n%2==0) sum=n/2*(n+1);
else sum=(n+1)/2*n;
if(sum%2==0) printf("0\n");
else printf("1\n");
p=sum/2;q=p%(n+1);
if(q!=0) a[s++]=q;
p-=q;
for(int i=1; i<=n&&p!=0; i++)
{
if(i!=q&&(n+1-i)!=q)
{
a[s++]=i;
a[s++]=n+1-i;
p-=n+1;
}
}
printf("%d", s);
for(int i=0; i<s; i++)
printf(" %d", a[i]);
printf("\n");
return 0;
}

Codeforces Round #452 (Div. 2) C. Dividing the numbers(水)的更多相关文章

  1. Codeforces Round #358 (Div. 2) A. Alyona and Numbers 水题

    A. Alyona and Numbers 题目连接: http://www.codeforces.com/contest/682/problem/A Description After finish ...

  2. Codeforces Round #452 (Div. 2) A B C

    Codeforces Round #452 (Div. 2) A Splitting in Teams 题目链接: http://codeforces.com/contest/899/problem/ ...

  3. Codeforces Round #452 (Div. 2) 899E E. Segments Removal

    题 OvO http://codeforces.com/contest/899/problem/E Codeforces Round #452 (Div. 2) - e 899E 解 用两个并查集(记 ...

  4. Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题

    Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  5. Codeforces Round #396 (Div. 2) A B C D 水 trick dp 并查集

    A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...

  6. 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 ...

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

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

  8. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集

    D. Dividing Kingdom II 题目连接: http://www.codeforces.com/contest/687/problem/D Description Long time a ...

  9. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环

    D. Dividing Kingdom II   Long time ago, there was a great kingdom and it was being ruled by The Grea ...

随机推荐

  1. PHP在win7安装Phalcon框架

    我的环境是64位的 Win7. 安装 Phalcon 也极其简单,只需要下载一个文件(php_phalcon.dll), 要以 phpinfo() 里面“Architecture”属性为准! 下载地址 ...

  2. Linux dkpg命令

    一.简介 dpkg 是Debian Package 的简写,是Debian系列系统下的一个软件安装.更新及移除工具. 二.常用指令 1.查询功能 查看软件包信息: dpkg -info xxx.deb ...

  3. CF 1023D Array Restoration - 线段树

    题解 非常容易想到的线段树, 还可以用并查集来. 还有一位大神用了$O(n)$ 就过了Orz 要判断是否能染色出输入给出的序列,必须满足两个条件: 1. 序列中必须存在一个$q$ 2. 两个相同的数$ ...

  4. POJ1180 Batch Scheduling -斜率优化DP

    题解 将费用提前计算可以得到状态转移方程: $F_i = \min(F_j + sumT_i * (sumC_i - sumC_j) + S \times (sumC_N - sumC_j)$ 把方程 ...

  5. vue cli+axios踩坑记录+拦截器使用,代理跨域proxy(更新)

    16319 1.首先axios不支持vue.use()方式声明使用,看了所有近乎相同的axios文档都没有提到这一点建议方式 在main.js中如下声明使用 import axios from 'ax ...

  6. Android无线调试_adbWireless

    NC的ADB驱动是个很让人头疼的问题,纵使老玩家有时候也是反复装装不上,有时候就算装上了,换一个ROM就又不行了,真是让人扣心扣肺,欲哭无泪,欲罢不能啊...现在好了,有了adbWireless不但可 ...

  7. Eclipse 中 Could not find *.apk的解决方案

    Eclipse 中 Could not find *.apk的解决方案 有时候debug的时候出现Could not find *.apk 特别是导入别人的例子的时候 1.选择properties-& ...

  8. 2018.10.19 NOIP训练 桌子(快速幂优化dp)

    传送门 勉强算一道dp好题. 显然第kkk列和第k+nk+nk+n列放的棋子数是相同的. 因此只需要统计出前nnn列的选法数. 对于前mmm%nnn列,一共有(m−1)/n+1(m-1)/n+1(m− ...

  9. hdu-1107(模拟题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1107 注意:1.路线是要反向的,走不通就反向: 2.输入输出全部是整形 3.攻击力不断变化 #incl ...

  10. origin里用c语言编程

    学习自白东升老师的origin8.0课程. 其实是originC语言.origin中大多绘图和处理功能都是originC语言完成的,可以同时按下ctrl和shift然后点击相应的功能,就会出现每个按钮 ...