【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 [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- JSP页面规格化
http://doc.okbase.net/%E4%BA%BA%E7%94%9F%E9%9A%BE%E5%BE%97%E7%B3%8A%E6%B6%82/archive/123084.html htt ...
- 对于Mobile模块化的概念认知(小白)
最近刚刚学习了Mobile的一些基础知识,把它整理一下方便自己的学习 那什么是Mobile呢? 自己的理解是将一个项目中共同的部分抽出来,这样就形成了Mobile模块. 为什么要使用Mobile呢? ...
- Java 中数组的遍历方式
数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同. Java 语言中提供的数组是用来存储固定大小的同类型元素. 今天我们就来说一下在java中遍历数组都有哪几 ...
- 关于C++ const 变量
const 的全局变量是储存在一个只读数据段中,虽然你可以定义一个指向它的指针,却会在运行时,在对该地址赋值的时候发生运行错误,而局部的const变量是储存在栈中的,离开作用域后同样会被释放,并且可以 ...
- BZOJ 1208 set
思路: 开俩set 模拟一下 就好了 //By SiriusRen #include <bits/stdc++.h> using namespace std; int n,xx,yy; s ...
- Highcharts Highstock 学习笔记 第一篇 Highcharts配置
Highcharts是一款纯javascript编写的图表库,能够很简单便捷的在Web网站或Web应用中添加交互性的图表,Highcharts目前支持直线图.曲线图.面积图.柱状图.饼图.散点图等多达 ...
- DAO模式详解
DAO模式 数据访问层(DAO): 数据的增.删.改.查操作: 业务逻辑层(service): 业务来往的操作,需要调用数据访问层则调用数据访问层,传递数据: 表现层(UI): 呈现数据,用户交互. ...
- jQuery实现页面锚点滚动效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- c指针之内存释放
// 1.正常使用包含指针的结构体 // 2.正常使用元素类型为指针的vector #include<string.h> #include<stdio.h> #include& ...
- 【译】x86程序员手册12-4.2系统指令
4.2 Systems Instructions 系统指令 Systems instructions deal with such functions as: 系统指令具有以下功能: Verifica ...