CF 131 div2 B
2013-07-22 21:59
232 查看
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 digits 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
output
input
output
input
output
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.
这题看似简单,非常容易漏掉情况没有考虑。如果余1,可以删一个余数1的,或者两个余数2的!如果余2,可以删一个余数2的,或两个余数1的。最后注意!如果只有0出现的话,最后只输出一个0!所以要特判。
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 digits 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.
这题看似简单,非常容易漏掉情况没有考虑。如果余1,可以删一个余数1的,或者两个余数2的!如果余2,可以删一个余数2的,或两个余数1的。最后注意!如果只有0出现的话,最后只输出一个0!所以要特判。
#include<cstdio> #include<cstring> #include<queue> #include<vector> #include<algorithm> #include<iostream> #include<string> #include<map> using namespace std; typedef long long LL; const int maxn = 100 + 5; const int INF = 1000000000; int num[10]; int main(){ int n; while(cin >> n){ memset(num,0,sizeof(num)); int total = 0; for(int i = 0;i < n;i++){ int tem; cin >> tem; num[tem]++; total += tem; } total = total % 3; int tag = 1; if(total == 1){ tag = 0; for(int i = 0;i < 10;i++){ if(i%3 == 1 && num[i] > 0){ tag = 1; num[i]--; break; } } int cnt = 2; if(tag == 0){ for(int i = 0;i < 10;i++){ while(i%3 == 2 && num[i] > 0 && cnt > 0){ num[i]--; cnt--; } } if(cnt == 0) tag = 1; } } else if(total == 2){ int cnt = 2; tag = 0; for(int i = 0;i < 10;i++){ if(i%3 == 2 && num[i] > 0){ num[i]--; tag = 1; break; } } if(tag == 0){ for(int i = 0;i < 10;i++){ while(i%3 == 1 && num[i] > 0 && cnt > 0){ num[i]--; cnt--; } } if(cnt == 0) tag = 1; } } if(tag == 0 || num[0] == 0){ cout << -1 << endl; continue; } else{ int flag = 0; for(int i = 9;i >= 0;i--){ if(i == 0 && flag == 0){ cout << 0 << endl; break; } while(num[i] > 0){ if(i != 0) flag = 1; num[i]--; cout << i; } } cout << endl; } } return 0; }
相关文章推荐
- cf 131 DIV2 B
- [cf2015ICLFinalsDiv1J]Ceizenpok’s formula
- CF #211 (Div. 2)--D. Renting Bikes
- cf 163 div2 解题报告
- CF 162-div2 D 数学+dp
- Cf Round #360 (Div. 1) 687D Dividing Kingdom II
- CF 305 div2 D. Mike and Feet (递推)
- CF 209 Div2 (C) 快速幂 求分子(一堆数相加)和分母的最大公约数
- cf(#div1 A. Dreamoon and Sums)(数论)
- CF 171 (div2)C
- [Codeforces Round #373 DIV1E (CF718E)] Matvey's Birthday
- CF 277 div2 A. Calculating Function
- CF_#277.5_DIV2_A_B_C
- CF#277.5 (Div. 2) A.(技巧题,类似动规)
- CF - div2 - 460 - D.Substring(拓扑排序 + DP)
- cf_369_div 2_E
- cf 80 div1 B(graph)
- cf div_124_a(简单博弈)
- CF#195(div2) A:Vasily the Bear and Triangle
- 【Codeforces #131 Div2】Solutions