codeforces 792 C. 删除尽量少的位数后是3的倍数 (取余问题+分类讨论+模拟吧)
2017-04-16 17:11
483 查看
题意:给出一个正整数n,问用最少的删除操作是它变成美丽数。
美丽数的定义:没有前导0,且是3的倍数。输出任意美丽数,无解输出-1。
题解:是三的倍数,和也是三的倍数。求和mod3之后,有三种情况,
1:和为0,即本身就是美丽数,直接输出。
2:和为1,此时应该删除一个1(mod3为1,下同)或者两个2。
3:和为2,此时应该删除一个2或者两个1。
这里总结了一下,以后有取模的问题,我们要取模后去考虑问题,
这样可以把数据范围降低,方便思考,方便解答!!!!ORZ
A positive integer number n is written on a blackboard. It consists of not more than 105 digits.
You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.
The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are
beautiful numbers, and 00, 03, 122 are
not.
Write a program which for the given n will find a beautiful number such that n can
be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n.
If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.
Input
The first line of input contains n — a positive integer number without leading zeroes (1 ≤ n < 10100000).
Output
Print one number — any beautiful number obtained by erasing as few as possible digits. If there is no answer, print - 1.
Examples
input
output
input
output
input
output
Note
In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a
number with a leading zero. So the minimum number of digits to be erased is two.
美丽数的定义:没有前导0,且是3的倍数。输出任意美丽数,无解输出-1。
题解:是三的倍数,和也是三的倍数。求和mod3之后,有三种情况,
1:和为0,即本身就是美丽数,直接输出。
2:和为1,此时应该删除一个1(mod3为1,下同)或者两个2。
3:和为2,此时应该删除一个2或者两个1。
这里总结了一下,以后有取模的问题,我们要取模后去考虑问题,
这样可以把数据范围降低,方便思考,方便解答!!!!ORZ
A positive integer number n is written on a blackboard. It consists of not more than 105 digits.
You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.
The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are
beautiful numbers, and 00, 03, 122 are
not.
Write a program which for the given n will find a beautiful number such that n can
be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n.
If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.
Input
The first line of input contains n — a positive integer number without leading zeroes (1 ≤ n < 10100000).
Output
Print one number — any beautiful number obtained by erasing as few as possible digits. If there is no answer, print - 1.
Examples
input
1033
output
33
input
10
output
0
input
11
output
-1
Note
In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a
number with a leading zero. So the minimum number of digits to be erased is two.
#include<bits/stdc++.h> using namespace std; const int N = 100000 + 10; const int inf = 0x3f3f3f3f; char s ; int a ,vis ,n; int work(int num,int cnt) { int i,j,ans; ans=cnt; for(i=n-1;i>=0;i--) { if(cnt==0) break; if(a[i]%3==num) { cnt--; vis[i]=1; } } for(i=0;i<n;i++) { if(vis[i]) continue; if(a[i]==0) { ans++; vis[i]=1; } else break; } if(cnt) ans=inf; return ans; } int main() { int i,j,sum,zero=0; int op1,op2; scanf("%s",s); n=strlen(s); sum=0; for(i=0;i<n;i++) { a[i]=s[i]-'0'; if(a[i]==0) zero=1; sum+=a[i]; sum=sum%3; } if(sum==0) { printf("%s",s); } else if(sum==1) { memset(vis,0,sizeof(vis)); op1 = work(1,1); memset(vis,0,sizeof(vis)); op2 = work(2,2); memset(vis,0,sizeof(vis)); if(min(op1,op2)==n) { if(zero) printf("0\n"); else printf("-1\n"); } else if(op1==op2 && op1==inf) printf("-1\n"); else if(op1<op2) { work(1,1); for(i=0;i<n;i++) if(!vis[i]) printf("%d",a[i]); printf("\n"); } else { work(2,2); for(i=0;i<n;i++) if(!vis[i]) printf("%d",a[i]); printf("\n"); } } else { memset(vis,0,sizeof(vis)); op1 = work(1,2); memset(vis,0,sizeof(vis)); op2 = work(2,1); memset(vis,0,sizeof(vis)); if(min(op1,op2)==n) { if(zero) printf("0\n"); else printf("-1\n"); } else if(op1==op2 && op1==inf) printf("-1\n"); else if(op1<op2) { work(1,2); for(i=0;i<n;i++) if(!vis[i]) printf("%d",a[i]); printf("\n"); } else { work(2,1); for(i=0;i<n;i++) if(!vis[i]) printf("%d",a[i]); printf("\n"); } } return 0; }
相关文章推荐
- JZOJ4828. 【GDOI2017模拟10.30】最大值 分类讨论+分治处理子集问题
- javascript中数组元素删除问题的讨论
- JavaScript delete操作符&&javascript中数组元素删除问题的讨论
- Codeforces 577B Module Sum(分类讨论)
- 行为识别笔记:关于行为检测/识别问题的分类及研究进展的讨论
- Codeforces 617D Polyline【思维+分类讨论】
- 关于javascript中数组元素删除问题的讨论
- ACM学习历程—CodeForces 590A Median Smoothing(分类讨论 && 数学)
- 【郝斌数据结构自学笔记】16-23_链表的定义与分类_链表节点插入与删除_每一个链表节点的数据类型该如何表示的问题
- CodeForces 382C Arithmetic Progression (排序+分类讨论)
- javascript中数组元素删除问题的讨论
- Codeforces 460D Little Victor and Set --分类讨论+构造
- 求数组中出现1,2次数问题的讨论 分类: 算法 2014-12-06 09:28 81人阅读 评论(0) 收藏
- POJ1008 Maya Calendar(模拟)&&整除和取余的边界问题
- zoj 3480 Duck Typing(模拟,分类讨论,读题)
- 关于javascript中数组元素删除问题的讨论
- Codeforces 2B The least round way 动态规划(分类讨论)
- Codeforces 398D Instant Messanger 轻重点分类 模拟
- 将ubuntu14.04 从mysql从5.5删除之后安装5.7遇到的一些问题(本篇不讨论热升级)
- 51nod1597 有限背包计数问题[DP][分类讨论][前缀和]