【codeforces】A. Array(思维水题)
2017-08-02 22:29
549 查看
点击打开题目
A. Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets
so as the following conditions hold:
The product of all numbers in the first set is less than zero ( < 0).
The product of all numbers in the second set is greater than zero ( > 0).
The product of all numbers in the third set is equal to zero.
Each number from the initial array must occur in exactly one set.
Help Vitaly. Divide the given array.
Input
The first line of the input contains integer n (3 ≤ n ≤ 100).
The second line contains n space-separated distinct integers a1, a2, ..., an (|ai| ≤ 103) —
the array elements.
Output
In the first line print integer n1 (n1 > 0) —
the number of elements in the first set. Then print n1 numbers
— the elements that got to the first set.
In the next line print integer n2 (n2 > 0) —
the number of elements in the second set. Then print n2 numbers
— the elements that got to the second set.
In the next line print integer n3 (n3 > 0) —
the number of elements in the third set. Then print n3 numbers
— the elements that got to the third set.
The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them.
Examples
input
output
input
output
题意:把输入的数字分割成三种数组,第一种元素之积为负,第二种为正,第三种为0‘
题解:第一种负数的个数为奇数个,多余的给第二个,第二个数组负数为偶数,第三个有0就行。
代码:
A. Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets
so as the following conditions hold:
The product of all numbers in the first set is less than zero ( < 0).
The product of all numbers in the second set is greater than zero ( > 0).
The product of all numbers in the third set is equal to zero.
Each number from the initial array must occur in exactly one set.
Help Vitaly. Divide the given array.
Input
The first line of the input contains integer n (3 ≤ n ≤ 100).
The second line contains n space-separated distinct integers a1, a2, ..., an (|ai| ≤ 103) —
the array elements.
Output
In the first line print integer n1 (n1 > 0) —
the number of elements in the first set. Then print n1 numbers
— the elements that got to the first set.
In the next line print integer n2 (n2 > 0) —
the number of elements in the second set. Then print n2 numbers
— the elements that got to the second set.
In the next line print integer n3 (n3 > 0) —
the number of elements in the third set. Then print n3 numbers
— the elements that got to the third set.
The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them.
Examples
input
3 -1 2 0
output
1 -1 1 2 1 0
input
4 -1 -2 -3 0
output
1 -1 2 -3 -2 1 0
题意:把输入的数字分割成三种数组,第一种元素之积为负,第二种为正,第三种为0‘
题解:第一种负数的个数为奇数个,多余的给第二个,第二个数组负数为偶数,第三个有0就行。
代码:
#include<iostream> #include<cstring> #include<algorithm> using namespace std; int main(){ int n; while(cin>>n){ int a1[110],a2[110],a3[110]; int k1=0,k2=0,k3=0; int temp; memset(a1,0,sizeof(a1)); memset(a2,0,sizeof(a2)); memset(a3,0,sizeof(a3)); for(int i=0;i<n;i++){ cin>>temp; if(temp<0) a1[k1++]=temp; else if(temp==0) a2[k2++]=temp; else a3[k3++]=temp; } int flag1,flag2,flag3; flag1=flag2=flag3=0; int m1=k1; int m2=k2; int m3=k3; if(!m3){//正数是否为空 a3[0]=a1[m1-1]; a3[1]=a1[m1-2]; m1-=2; m3+=2; } if(m1&1){//判断是否为奇数个负数 cout<<m1<<" "; for(int i=0;i<m1-1;i++) cout<<a1[i]<<" ";cout<<a1[m1-1]<<endl; } else{//否则的话,减掉一个 a2[m2]=a1[m1-1]; m2++; m1-=1; cout<<m1<<" "; for(int i=0;i<m1-1;i++) cout<<a1[i]<<" ";cout<<a1[m1-1]<<endl; } cout<<m3<<" "; for(int i=0;i<m3-1;i++) cout<<a3[i]<<" ";cout<<a3[m3-1]<<endl; cout<<m2<<" "; for(int i=0;i<m2-1;i++) cout<<a2[i]<<" ";cout<<a2[m2-1]<<endl; } return 0; }
相关文章推荐
- CodeForces - 361C A - Levko and Array Recovery 思维
- codeforces- 949B - A Leapfrog in the Array(思维)
- 【codeforces】A. The Text Splitting(思维题-水题)
- CodeForces 721D. Maxim and Array(贪心,思维题)
- Codeforces 660A:Co-prime Array(水题+思维)
- codeforces 558B B. Amr and The Large Array(水题)
- codeforces 181.div2 300A --Array 思维问题
- Codeforces 554B:Ohana Cleans Up(思维+水题)
- 【CodeForces】300A - Array(思维)
- Codeforces 845C Two TVs【思维】水题
- CodeForces 604C 【思维水题】`
- codeforces 804A Find Amir 思维/水题
- CodeForces 360A - Levko and Array Recovery (思维)
- Codeforces Round #210 (Div. 2) C. Levko and Array Recovery && Codeforces 360 A 详解(思维+维护理论值)
- Codeforces 421D Bug in Code(思维)
- Codeforces 672D Robin Hood【思维+二分】这题思路有点劲啊
- codeforces 702C C. Cellular Network(水题)
- Codeforces-916B:Jamie and Binary Sequence (changed after round)(思维)
- CodeForces 560B Gerald is into Art【水题】
- Codeforces 320E Kalila and Dimna in the Logging Industry【思维+贪心+斜率优化Dp】好题!