【题目链接】: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. lightbox2

    http://lokeshdhakar.com/projects/lightbox2/ 简单的demo <html> <head id="head"> &l ...

  2. B1826 [JSOI2010]缓存交换 贪心+离散化+堆

    这个题仔细一想可以直接贪心做,因为队列里下一个出现的早的一定最优.正确性显然.然后我只拿了50,我直接模拟另一个队列暴力修改最后一个点的nxt值,自然会T.但是其实不用修改,直接插入就行了前面的不影响 ...

  3. B4197 [Noi2015]寿司晚宴 状压dp

    这个题一开始想到了唯一分解定理,然后状压.但是显然数组开不下,后来想到每个数(n<500)大于19的素因子只可能有一个,所以直接单独存就行了. 然后正常状压dp就很好搞了. 题干: Descri ...

  4. java—容器学习笔记

    一:迭代器 刚开始学容器,做了个简单的练习题.. import java.util.ArrayList; import java.util.Collection; import java.util.I ...

  5. Maya Calendar

    http://poj.org/problem?id=1008 按第一种记录方法算出总天数,然后按第二种记录方式输出. #include<stdio.h> #include<strin ...

  6. flask中的蓝图(BluePrint)

    蓝图,听起来就是一个很宏伟的东西 在Flask中的蓝图 blueprint 也是非常宏伟的 它的作用就是将 功能 与 主服务 分开怎么理解呢? 比如说,你有一个客户管理系统,最开始的时候,只有一个查看 ...

  7. ACM_寻找第N小序列

    寻找第N小序列 Time Limit: 2000/1000ms (Java/Others) Problem Description: Now our hero finds the door to th ...

  8. 【Leetcode】115. Distinct Subsequences

    Description: Given two string S and T, you need to count the number of T's subsequences appeared in ...

  9. docker容器如何安装vim

    mv /etc/apt/sources.list /etc/apt/sources.list.bak && \     echo "deb http://mirrors.16 ...

  10. Jquery 多行拖拽图片排序 jq优化

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...