【codeforces 792C】Divide by Three
【题目链接】:http://codeforces.com/contest/792/problem/C
【题意】
让你删掉最少的数字使得剩下的数字%3==0
【题解】
看代码。。内置题解了现在。
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;
char s[N];
int a[N],sum=0,len;
bool bo[N];
void out(){
rep1(i,1,len)
if (bo[i])
cout << a[i];
exit(0);
}
int main(){
//freopen("F:\\rush.txt","r",stdin);
memset(bo,true,sizeof bo);
scanf("%s",s+1);
len = strlen(s+1);
rep1(i,1,len) {
a[i] = s[i]-'0';
sum=(sum+a[i])%3;
}
if (sum==0) out();//直接就符合题意了
if (len==1) return puts("-1"),0;//接下来要删除1个数字了 所以如果刚好一个数字是不合法的
rep1(i,2,len)//从第二个数字开始删就不会存在不合法的了
if (a[i]%3==sum)
bo[i] = false,out();
if (a[1]%3==sum && (a[2]!=0||(a[2]==0&&len==2))) bo[1] = false,out();
//第一个数字的话要求第二个数字不是0
//或者长度为2,那么也只要删除一个数字;
if (len<=2) return puts("-1"),0;
//接下来要至少删除两个数字了,所以小于等于2个数字都不合法了,从后往前删
//这样能保证先删除的两个数字它们是靠后的;
//如果有删除第一个和前缀的0更优的话,a[1]%3==sum,不可能是删掉的两个数字之一,所以不会有删除多余的0
//所以直接删两个和为sum肯定更优(或者不会差).
//最后再考虑a[1]%3==sum和有前缀0的情况
int d =0;//判断有没有两个可以删的了;
rep2(i,len,1)
if ((a[i]%3)==(3-sum)){//这是两个合起来删掉能变成%3为0的情况
if (d){
bo[i]=bo[d] = false;//删掉这两个数字
int fir=0;//有没有找到第一个非0数字;
rep1(j,1,len)
if (j!=i&&j!=d){
if (a[j]){
fir = 1;
break;
}
else//前面还没找到非0数字呢;
bo[j] = false;//是前缀0要删掉
}
if (fir){//找到了前缀非0
out();
}
else{//没找到
//已经确定大于两个数字了
return puts("0"),0;
}
}
else{
d = i;
}
}
//最后在再考虑删掉第一个数虽然可以但要删除前缀0的情况;
if (a[1]%3==sum){
bo[1] = false;
int j =1;
while (j+1<=len-1 && a[j+1]==0) j++,bo[j] = false;//要留一个0
out();
}
puts("-1");
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 792C】Divide by Three的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【23.39%】【codeforces 558C】Amr and Chemistry
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 546C】Soldier and Cards
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 752F】Santa Clauses and a Soccer Championship
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- mysql sql执行错误#1436 Thread stack overrun
1.mysql调用存储过程 call proc1() 时报错:Thread stack overrun: 6656 bytes used of a 8496 byte stack, and 1280 ...
- Linux学习大纲
- An problem about date 根据年月日计算 星期几
/W = (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7(1.2月需要看作上一年的13.14月) #include<stdio.h> #include& ...
- ACM_圆的面积
圆的面积 Time Limit: 2000/1000ms (Java/Others) Problem Description: AB是圆O的一条直径,CD.EF是两条垂直于AB的弦,并且以CD为直径的 ...
- Mac上随时切换PYTHON版本
在MAC上,默认安装了python2.*,自己又安装了python3.*:假如我们需要在终端上随时切换python控制台到需要的版本,可以采用下面的方法. 1.用命令 sudo vi ~/.bas ...
- [转]linux之top命令
转自:http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316399.html 简介 top命令是Linux下常用的性能分析工具,能够实时显示系 ...
- Python随笔-函数
在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. def GetMax(v1, v2):#注 ...
- 浅谈Java中的hashcode方法以及equals方法
哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: public native int hashCode(); 根据这个 ...
- T-SQL语句以及几个数据库引擎
创建表 注意事项: A.自增长 B.数据库引擎, ISAM 是一个定义明确且历经时间考验的数据表格管理方法,它在设计之时就考虑到数据库被查询的次数要远大于更新的次数.因此,IS ...
- JavaWeb项目中引入spring框架
主要步骤有以下3步: 1:下载spring的jar包2:在项目中web.xml中添加spring配置3:bean配置文件-applicationContext.xml 1:引入包,这个就不说了,官网下 ...