codeforces 214B Hometask
2017-07-23 11:22
204 查看
B. Hometask
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?
You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without
a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.
Each digit is allowed to occur in the number the same number of times it occurs in the set.
Input
A single line contains a single integer n (1 ≤ n ≤ 100000) —
the number of
4000digits in the set. The second line contains n digits, the digits are separated by a single space.
Output
On a single line print the answer to the problem. If such number does not exist, then you should print -1.
Sample test(s)
input
1 0
output
0
input
11
3 4 5 4 5 3 5 3 4 4 0
output
5554443330
input
8 3 2 5 1 5 2 2 3
output
-1
Note
In the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
题意:给n个数,求出用这n个数组成的最大的数,要求该数能被2,3,5整除,可以不用完所有的数,但不能有前导0
首先考虑被2,3,5整除数的特征,末位肯定是0,所以数字和能整除3,n个数的和对3取模只会有三种情况:
0 直接输出所有数,但是要注意有多个0时只输出一个
1 要想使剩下的数字和能整除3,要么去掉一个1(模3得1的 数),要么去掉2个2(模3得2的数),当然优先考虑去掉1个1的情况,如果都不满足直接输出3,6,9,0对应的个数,如果只剩下0要注意只输出一个0
2 和上面的一样,要么去掉1个2,要么去掉2个1,剩下和上面一样
代码较挫
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
#define pi acos(-1.0)
#define eps 1e-10#define pf printf
#define sf scanf
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define e tree[rt]
#define _s second
#define _f first
#define all(x) (x).begin,(x).end
#define mem(i,a) memset(i,a,sizeof i)
#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)
#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)
#define mi ((l+r)>>1)
const int inf=0x3f3f3f3f;
const int Max=1e5+1;
int a[1100],m,n,b[110],d;
int main()
{
while(~sf("%d",&m))
{
mem(a,0);
int tag=0;
int sum=0;
for1(i,m)
sf("%d",&d),a[d]++,sum+=d;//完全不需要排序,开个大小为10的数组计数即可
if(!a[0])
{
pf("-1\n");
continue;
}
b[1]=a[1]+a[4]+a[7];
b[2]=a[2]+a[5]+a[8];
int p=sum%3;
if(p==1)//余数为1的情况
{
int x=2;
if(b[1]>=1)
for(int i=0;i<3;i++)
{
if(a[1+i*3])
{
a[1+i*3]--;
break;
}
}
else if(b[2]>=2)
{
for(int i=0;i<3;i++)
while(a[2+3*i])
{
if(!x)
break;
a[2+3*i]--;
x--;
}
}
else//两种都不满足,直接将不是3倍数的计数器归0{
for(int i=1;i<=9;i++)
if(i%3)
a[i]=0;
}
}
else if(p==2)
{
int x=2;
if(b[2]>=1)
for(int i=0;i<3;i++)
{
if(a[2+i*3])
{
a[2+i*3]--;
break;
}
}
else if(b[1]>=2)
for(int i=0;i<3;i++)
while(a[1+3*i])
{
if(!x)break;
a[1+3*i]--;
x--;
}
else
{
for(int i=1;i<=9;i++)
if(i%3)
a[i]=0;
}
}
int q=0;
for(int i=1;i<=9;i++)
{
q+=a[i];
}
if(!q)//判断是不是只剩下0{
pf("0\n");
continue;
}
for(int i=9;i>=0;i--)//从大到小输出
while(a[i]--)
pf("%d",i);
pf("\n");
}
return 0;
}
相关文章推荐
- CodeForces 214B Hometask
- Codeforces 214B Hometask
- Hometask--codeForces 214B
- codeforces 214B - Hometask
- Codeforces 155C Hometask【思维+Dp】
- CodeForces 214B
- Codeforces 154A Hometask
- 【CodeForces 155C Hometask】白濑肆×字符串+DP——果然是字符串处理什么的好讨厌啊尤其是换行符的处理看来不用CIN不行了呢DP的转移真心不会啊水到家了怎么办!【1.1%达成】
- Codeforces 155 C. Hometask
- CodeForces 154A Hometask dp
- codeforces 214B
- codeforces 154A Hometask 贪心 dp
- CodeForces - 214B
- CodeForces 445A DZY Loves Chessboard
- 【CodeForces 719E】【线段树+矩阵快速幂】 Sasha and Array
- Codeforces 913A Modular Exponentiation
- ecjtu-summer training #1 A(CodeForces 651A)
- CodeForces - 845B Luba And The Ticket
- CodeForces 137A
- Codeforces 549E Sasha Circle