【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 [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- TeX中的引号
#include <stdio.h> #include <math.h> // 算法竞赛的目标是编程对任意输入均得到正确的结果. // 请先独立完成,如果有困难可以翻阅本书代码 ...
- 8.3 TCPIP协议族
接下来我们要学习的内容是TCP/IP协议族.TCP/IP协议族在网络系统中是非常重要的.这一个协议族当中牵涉到许许多多的我们平常所用到的协议.TCP/IP呢它也有分层模型.然后我们讲到的就是三方面的内 ...
- 3最短路的几种解法 ------例题< 最短路 >
点击进入例题 最短路 我知道的有三种方法 1 : 深搜 每次 每次有更小的路径时 就更新 , 2 : Dijkstra 3 : floyd 前两种 是 单源 最短路径 ...
- Codeforces 766E
题意:给一棵树(1e5),每个节点上有对应权值(0<=ai<=1e6)定义树上两个节点间的距离为路径中节点的异或,求所有节点对间的距离和(包括节点自身也作为节点对,距离为节点权值). 解题 ...
- MVC系列学习(二)-初步了解ORM框架-EF
1.新建 一个控制台项目 2.添加一个数据项 a.选择数据库 注:数据库中的表如下: b.选择EF版本 c.选择表 3.初步了解EF框架 看到了多了一个以 edmx后缀的文件 在edmx文件上,右击打 ...
- wampserver修改mysql数据库密码的简单方式
刚装好的wampserver的数据库是没有密码的,所以可以直接登录,要设置密码,一种简单的方式如下: 打开phpMyadmin 初始状态没有密码,可以直接登录 登录之后,点击账户 点击修改权限,设置你 ...
- VS2013支持多字节
使用插件 下载地址:https://www.microsoft.com/zh-cn/search/DownloadResults.aspx?rf=sp&q=mbcs
- openstack——horizon篇
一.horizon 介绍: 理解 horizon Horizon 为 Openstack 提供一个 WEB 前端的管理界面 (UI 服务 )通过 Horizone 所提供的 DashB ...
- 【特 性】Attribute
1 AttributeUsage [AttributeUsageAttribute(AttributeTargets.All, AllowMultiple = true, Inherited = tr ...
- Web 常用
System.Web.Hosting.HostingEnvironment.MapPath(); HttpUtility.UrlEncode();