Codeforces Round #452 (Div. 2) - C. Dividing the numbers (思路)
2017-12-18 22:42
453 查看
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.
题意:
把1到n这些数,分成两堆,让他们的差值最小,且任意输出一组。
POINT:
首先可以想到背包做,但是感觉会tle。
因为数字是连续的:
所以n%4==0时,肯定可以平分, 比如1 2 3 4 ,那么(1,4)(2,3)。比如4 5 6 7 ,那么(4,7)(5,6)
所以讨论余1,2,3时。
余1,就先挑出1,然后让2-n平分,最后任意给一个1.差值就是1
余2,就先挑出1,2,然后让3-n平分,最后每边给一个1或2,差值是1.
余3,一边给3,一边给1 2,差值为0
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <vector>
#include <map>
#include <stdio.h>
#include <algorithm>
using namespace std;
int n;
int main()
{
cin>>n;
if(n%4==0){
printf("0\n");
printf("%d",n/2);
int len=n/4;
for(int i=0;i<len;i++){
printf(" %d %d",1+i,n-i);
}
}else if(n%4==1){
printf("1\n");
printf("%d",n/2);
int len=n/4;
for(int i=0;i<len;i++){
printf(" %d %d",2+i,n-i);
}
}else if(n%4==2){
printf("1\n");
printf("%d",n/2);
int len=n/4;
printf(" 1");
for(int i=0;i<len;i++){
printf(" %d %d",3+i,n-i);
}
}else if(n%4==3){
printf("0\n");
printf("%d",n/2);
int len=n/4;
printf(" 3");
for(int i=0;i<len;i++){
printf(" %d %d",4+i,n-i);
}
}
}
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.
题意:
把1到n这些数,分成两堆,让他们的差值最小,且任意输出一组。
POINT:
首先可以想到背包做,但是感觉会tle。
因为数字是连续的:
所以n%4==0时,肯定可以平分, 比如1 2 3 4 ,那么(1,4)(2,3)。比如4 5 6 7 ,那么(4,7)(5,6)
所以讨论余1,2,3时。
余1,就先挑出1,然后让2-n平分,最后任意给一个1.差值就是1
余2,就先挑出1,2,然后让3-n平分,最后每边给一个1或2,差值是1.
余3,一边给3,一边给1 2,差值为0
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <vector>
#include <map>
#include <stdio.h>
#include <algorithm>
using namespace std;
int n;
int main()
{
cin>>n;
if(n%4==0){
printf("0\n");
printf("%d",n/2);
int len=n/4;
for(int i=0;i<len;i++){
printf(" %d %d",1+i,n-i);
}
}else if(n%4==1){
printf("1\n");
printf("%d",n/2);
int len=n/4;
for(int i=0;i<len;i++){
printf(" %d %d",2+i,n-i);
}
}else if(n%4==2){
printf("1\n");
printf("%d",n/2);
int len=n/4;
printf(" 1");
for(int i=0;i<len;i++){
printf(" %d %d",3+i,n-i);
}
}else if(n%4==3){
printf("0\n");
printf("%d",n/2);
int len=n/4;
printf(" 3");
for(int i=0;i<len;i++){
printf(" %d %d",4+i,n-i);
}
}
}
相关文章推荐
- Codeforces Round #452 (Div. 2) C - Dividing the numbers 推规律
- [Codeforces Round #452 (Div. 2)] Dividing the numbers
- C - Dividing the numbers CodeForces - 899C 【补】
- AIM Tech Round 3 (Div. 2) -- D. Recover the String (思路题目--构造字符串)
- Codeforces Round #215 (Div. 1) C. Sereja and the Arrangement of Numbers(欧拉图)
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) -- A. Checking the Calendar(思路)
- Codeforces Round #435 (Div. 2) A. Mahmoud and Ehab and the MEX(思路)
- Codeforces899C-Dividing the numbers
- Codeforces Round #215_div2_E. Sereja and the Arrangement of Numbers
- 【Codeforces】899 C. Dividing the numbers 贪心
- UVA 11300 Spreading the Wealth (思路题目)
- Codeforces Round #258 (Div. 2/C)/Codeforces451C_Predict Outcome of the Game(枚举)
- 可以用鼠标拖动的DIV实现思路及代码
- Codeforces Round #384(Div. 2)B. Chloe and the sequence【思维+lowbit】
- How to align content of a div to the bottom with css?
- Codeforces Round #384 (Div. 2) B. Chloe and the sequence 找规律
- 点击不在这个区域部分,div消失,jquery代码 - hiding an element on click of anything else on the page
- Codeforces Round #160 (Div. 2) A. Roma and Lucky Numbers
- [Vue warn]: Attribute "id" is ignored on component <div> because the component is a fragment instanc
- Codeforces Round #185 (Div. 2)-The Closest Pair