Codeforces Round #131 (Div. 2)
来源:互联网 发布:lte网络架构图 编辑:程序博客网 时间:2024/06/04 17:46
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.
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.
On a single line print the answer to the problem. If such number does not exist, then you should print -1.
10
0
113 4 5 4 5 3 5 3 4 4 0
5554443330
83 2 5 1 5 2 2 3
-1
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个数字,问用这些数字组成的数模30余0的数字,最大是多少。
大家都知道模3余0的定义是数字之和加起来模3为0,而我们要求的模30余0的数字,所以最后组成的数字必须含0,故不含0的必然为-1;
而最后得到的数字则需要注意前导0就行了
#include<iostream>#include<cmath>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>using namespace std;bool cmp(int a,int b){return a>b;}const int maxn=1000000;int a[maxn];int n;vector<int> one,two,zero;int couts(int x,int y){bool r=0;for(int i=0;i<n;i++){if(i==x||i==y)continue;if(a[i]==0){if(r)cout<<a[i];}else{r=1;cout<<a[i];}}if(!r)cout<<0;cout<<endl;}int main(){while(scanf("%d",&n)!=EOF){for(int i=0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n,cmp);int f=0;int sum=0;for(int i=0;i<n;i++){sum+=a[i];if(a[i]%3==0){if(a[i]==0)f=1;zero.push_back(i);//将数字的序号而不是数字本身存入vector,因为数字一共只有10个 //在后面输出的时候会出现重复,而序列号则不会。 }else if(a[i]%3==1)one.push_back(i);else if(a[i]%3==2)two.push_back(i);}if(f==0){cout<<"-1"<<endl;return 0;}else{if(sum%3==0)couts(-1,-1);else if(sum%3==1){if(one.size()>0)couts(one.back(),-1);else if(two.size()>0)couts(two[two.size()-1],two[two.size()-2]);}else if(sum%3==2){if(two.size()>0)couts(two.back(),-1);else if(one.size()>0)couts(one[one.size()-1],one[one.size()-2]);}}}}
- Codeforces Round #131 (Div. 2)
- Codeforces Round #131 (Div. 2)
- Codeforces Round #131 (Div. 2) 完整题解
- Codeforces Round #131 (Div. 2) A B
- Codeforces Round #131 (Div. 2) A题
- Codeforces Round #131 (Div. 1)
- Codeforces Round #102 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #104 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #107 (Div. 2)
- Codeforces Round #108 (Div. 2)
- Codeforces Round #110 (Div. 2)
- Codeforces Round #122 (Div. 2)
- Codeforces Round #121 (Div. 2)
- Codeforces Round #124 (Div. 2)
- POJ 2318 TOYS
- 别了,恒升
- Java时间
- error C2504: 'COleControlSite' : base class undefined
- Makefile 的使用函数
- Codeforces Round #131 (Div. 2)
- Swing中JRadioButton多选框的用法
- Your branch is ahead of 'origin/master' by 1 commit
- AfxBeginThread 和 CreateThread 区别
- 浅析ANSI-C语言中的中断处理(如有疑问,敬请留言)
- BIN OBJ 区别
- jsp中的request.getContextPath()
- hdoj 4081
- Javascript数组的排序 sort()方法和reverse()方法