【CodeForces - 214B】Hometask (模拟,有坑)
題干:
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.
Examples
Input
1 0Output
0Input
11 3 4 5 4 5 3 5 3 4 4 0Output
5554443330Input
8 3 2 5 1 5 2 2 3Output
-1Note
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.
解題報告:
? ?這題好惡心,明明不難的題,卡了半小時、、因為有前導零啊要特判。合適會出現前導零呢?在輸入全為0時,或者有一個1剩下全是0這樣的,或者兩個2剩下全是0這樣的。。并且不能讀入完數據就特判剛開始寫了if(a[n]==0) {puts("0");return 0;},結果發現不對啊處理不了一個1剩下全是0這樣的情況。然后改成if(sum == 0 || sum==1 || sum==2) {?puts("0");return 0 ;}發現處理不了兩個2剩下全是0的情況。。然后在每一個輸出中都判斷是否全是0這樣、、、因為不判斷就會出現前導零、、
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; int a[MAX],sum,sum1,sum2; int n; bool bk[MAX]; int main() {cin>>n;for(int i = 1; i<=n; i++) scanf("%d",a+i),sum+=a[i];sort(a+1,a+n+1);if(a[1] != 0) {puts("-1");return 0 ;}if(sum == 0 || sum==1 || sum==2) {puts("0");return 0 ;}if(sum%3 == 0) {for(int i = n; i>=1; i--) printf("%d",a[i]);return 0 ;}if(sum%3 == 1) {int cnt1=0;for(int i = 1; i<=n; i++) {if(a[i]%3 == 1 && cnt1 != 1) {bk[i]=1;cnt1++;continue;}sum1+=a[i];}if(sum1 == sum) {int cnt2 = 0;for(int i = 1; i<=n; i++) {if(a[i]%3 == 2 && cnt2 != 2) {bk[i]=1;cnt2++;continue;}sum2+=a[i];}if(cnt2 != 2) {puts("-1");return 0 ;}else {int flag = 0;for(int i = n; i>=1; i--) {if(bk[i] == 0) {printf("%d",a[i]);if(a[i] == 0 && flag == 0) return 0 ;flag=1;}}return 0 ;}}else {int flag = 0;for(int i = n; i>=1; i--) {if(bk[i] == 0) {printf("%d",a[i]);if(a[i] == 0 && flag == 0) return 0 ;flag=1;}}return 0 ;}} //if(sum%3 == 2) {int cnt2=0;for(int i = 1; i<=n; i++) {if(a[i]%3 == 2 && cnt2 != 1) {bk[i]=1;cnt2++;continue;}sum2+=a[i];}if(sum2 == sum) {int cnt1=0;for(int i = 1; i<=n; i++) {if(a[i]%3 == 1 && cnt1 != 2) {bk[i]=1;cnt1++;continue;}sum1+=a[i];}if(cnt1 != 2) {puts("-1");return 0 ;}else {int flag = 0;for(int i = n; i>=1; i--) {if(bk[i] == 0) {printf("%d",a[i]);if(a[i] == 0 && flag == 0) return 0 ;flag=1;}}return 0 ;}}else {int flag=0;for(int i = n; i>=1; i--) {if(bk[i] == 0) {printf("%d",a[i]);if(a[i] == 0 && flag == 0) return 0 ;flag=1;}}return 0 ;}}return 0 ;}/* 11 3 9 9 6 4 3 6 4 9 6 0 Output -1 Answer 999666330 */?
總結
以上是生活随笔為你收集整理的【CodeForces - 214B】Hometask (模拟,有坑)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【51Nod - 1279】 扔盘子(思
- 下一篇: 【qduoj - 纳新题】小明的dp(快