Codeforces899C-Dividing the numbers
2017-12-21 09:27
225 查看
C. Dividing the numbers
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output
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.
Examples
Input
Output
Input
Output
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.
题解:比较经典的一道题,只是数据范围比较大。我的方法,似乎是找规律。
Code;
#include<bits/stdc++.h>
using namespace std;
int a[100100];
int main()
{
int n;
scanf("%d",&n);
for(int
4000
j=1;j<=n;j++)a[j]=j;
long long s1=0,s2=0;
if(n==2)
{
puts("1");
puts("1 1");
}else
if(n==3)
{
puts("0");
puts("2 1 2");
}else
{
if(n%2==1)
{
if(n/2%2==0)
{
puts("1");
printf("%d ",(n+1)/2);
printf("1 ");
for(int j=2;j<=n/2;j+=2)
printf("%d %d ",j,n-j+2);
}else
{
puts("0");
printf("%d ",(n+1)/2);
for(int j=1;j<=n/2;j+=2)
printf("%d %d ",j,n-j);
}
}else
{
if(n/2%2==0)
{
puts("0");
printf("%d ",n/2);
for(int j=1;j<n/2;j+=2)
printf("%d %d ",a[j],a[n-j+1]);
}else
{
puts("1");
printf("%d ",n/2);
for(int j=1;j<n/2;j=j+2)
printf("%d %d ",a[j],a[n-j+1]);
printf("%d",n/2);
}
}
}
return 0;
}
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output
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.
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.
题解:比较经典的一道题,只是数据范围比较大。我的方法,似乎是找规律。
Code;
#include<bits/stdc++.h>
using namespace std;
int a[100100];
int main()
{
int n;
scanf("%d",&n);
for(int
4000
j=1;j<=n;j++)a[j]=j;
long long s1=0,s2=0;
if(n==2)
{
puts("1");
puts("1 1");
}else
if(n==3)
{
puts("0");
puts("2 1 2");
}else
{
if(n%2==1)
{
if(n/2%2==0)
{
puts("1");
printf("%d ",(n+1)/2);
printf("1 ");
for(int j=2;j<=n/2;j+=2)
printf("%d %d ",j,n-j+2);
}else
{
puts("0");
printf("%d ",(n+1)/2);
for(int j=1;j<=n/2;j+=2)
printf("%d %d ",j,n-j);
}
}else
{
if(n/2%2==0)
{
puts("0");
printf("%d ",n/2);
for(int j=1;j<n/2;j+=2)
printf("%d %d ",a[j],a[n-j+1]);
}else
{
puts("1");
printf("%d ",n/2);
for(int j=1;j<n/2;j=j+2)
printf("%d %d ",a[j],a[n-j+1]);
printf("%d",n/2);
}
}
}
return 0;
}
相关文章推荐
- C - Dividing the numbers CodeForces - 899C 【补】
- [Codeforces Round #452 (Div. 2)] Dividing the numbers
- 【Codeforces】899 C. Dividing the numbers 贪心
- Codeforces Round #452 (Div. 2) - C. Dividing the numbers (思路)
- Codeforces Round #452 (Div. 2) C - Dividing the numbers 推规律
- 1069. The Black Hole of Numbers (20)
- Print the numbers of form 2^i.5^j in increasing order -- Google
- 1069. The Black Hole of Numbers (20)
- [Google]Find numbers of nodes in a BST in the range [low,high]
- PAT 甲级 1069. The Black Hole of Numbers (20)
- cdoj Dividing Numbers 乱搞记忆化搜索
- codechef Partition the numbers(思维)
- The reversed sum of two reversed numbers
- UVa:11481 Arrange the Numbers
- PAT 1069. The Black Hole of Numbers (20)(数字黑洞)
- 【12】Adds two numbers represented by a linked list and returns the sum as a linked list
- 1069. The Black Hole of Numbers (20)
- PAT (Advanced Level) 1069. The Black Hole of Numbers (20)
- 1069. The Black Hole of Numbers (20)
- PAT (Advanced Level) Practise 1069 The Black Hole of Numbers (20)