【题目链接】: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的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【23.39%】【codeforces 558C】Amr and Chemistry

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【codeforces 546C】Soldier and Cards

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【codeforces 752F】Santa Clauses and a Soccer Championship

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  6. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  7. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  8. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  9. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

随机推荐

  1. Bing Maps进阶系列八:在Bing Maps中集成OpenStreetMap地图

    Bing Maps进阶系列八:在Bing Maps中集成OpenStreetMap地图 OSM(OpenStreetMap-开放街道地图)服务就是一种发布自己地图数据图片为服务的一种实现类型,开放街道 ...

  2. imagebutton 设置点击和按压效果

    实现 点击 时 有按压效果 更换颜色 <ImageButton android:id="@+id/mediacontroller_Fullscreen_or_not" and ...

  3. [置顶] Snow的追寻

    题目描述 Snow终于得知母亲是谁,他现在要出发寻找母亲. 王国中的路由于某种特殊原因,成为了一棵有n个节点的根节点为1的树,但由于"Birds are everywhere.", ...

  4. 常见的几种异常类型Exception

    转自:https://blog.csdn.net/niceworkgogogo/article/details/71746208 算数异常类:ArithmeticExecption 空指针异常类型:N ...

  5. 72. js EXTJS grid renderer用法

    转自:https://blog.csdn.net/shancunxiaoyazhi/article/details/22156083 renderer : Function (可选的)该函数用于加工单 ...

  6. FPC报价模块配置 UpdateCommand影响了预期 1 条记录中的 0 条 解决办法

    今天在增加P4厂 FPC报价模块配置,增加刚挠信息节点,在保存时报错:UpdateCommand影响了预期 1 条记录中的 0 保存时使用:SqlDataAdapter批量更新DataTable,怎么 ...

  7. 数值分析常见算法C++实现

    1.1-有效数字丢失现象观察 #include<bits./stdc++.h> using namespace std; double f1(double x) { )-sqrt(x)); ...

  8. 个人作业 - Alpha 项目测试

    写在前面 课程链接:https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 作业要求:https://edu.cnblogs.com ...

  9. [Luogu 1052] noip 05 过河

    [Luogu 1052] noip 05 过河 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是 ...

  10. 了解MySQL的字符集

    在数据库中,字符乱码属于常见.多发问题.鉴于本人水平顶多只能归于不入流之类,写这篇文章时内心诚惶诚恐,实在担心误导大家.内容仅供参考,若有错误,请各位及时指出,我也好学习提高! MySQL的字符集有4 ...