CodeForces 792C - Divide by Three [ 分类讨论 ]
删除最少的数位和前缀0,使得剩下的数能被3整除
等价于各数位数字之和能被3整除。
当前数位和可能是 0, 1, 2(mod 3)
0: 直接处理
1: 删除一个a[i]%3 == 1 或者 两个a[i]%3 == 2
2: 同1
对于删完的数列,去掉前置0(只剩前置0就当作0)
若删啥都不满足,则判断原数列中有没有0,不然就输出-1
#include <bits/stdc++.h>
using namespace std;
const int MAXN = ;
char s[MAXN];
int a[MAXN], n;
string ans, tmp;
int b[MAXN], m;
void Update()
{
tmp.clear();
int i;
for (i = ; i <= m && !b[i]; i++);
if (i < m+)
{
for (i; i <= m; i++)
tmp += b[i] + '';
}
else if (m)
tmp += '';
if (ans.length() < tmp.length())
ans = tmp;
}
int main()
{
scanf("%s", s);
n = strlen(s);
for (int i = ; i <= n; i++)
a[i] = s[i-] - '';
int sum = ;
for (int i = ; i <= n; i++)
sum = (sum+a[i]%) % ;
if (!sum)
{
m = ;
for (int i = ; i <= n; i++)
b[++m] = a[i];
Update();
}
else
{
int p1 = , p2 = ;
for (int i = n; i > && !p1; i--)
if (a[i]% == sum) p1 = i;
if (p1)
{
m = ;
for (int i = ; i <= n; i++)
{
if (i == p1) continue;
b[++m] = a[i];
}
Update();
}
p1 = p2 = ;
for (int i = n; i > && (!p1 || !p2) ; i--)
if (a[i]% +sum == ) p1 ? p2 = i : p1 = i;
if (p1 && p2)
{
m = ;
for (int i = ; i <= n; i++)
{
if (i == p1 || i == p2) continue;
b[++m] = a[i];
}
Update();
}
}
if (!ans.length())
{
bool flag = ;
for (int i = ; i <= n; i++)
if (!a[i]) flag = ;
puts(flag? "": "-1");
}
else cout << ans << endl;
}
CodeForces 792C - Divide by Three [ 分类讨论 ]的更多相关文章
- codeforces 792C. Divide by Three
题目链接:codeforces 792C. Divide by Three 今天队友翻了个大神的代码来问,我又想了遍这题,感觉很好,这代码除了有点长,思路还是清晰易懂,我就加点注释存一下...分类吧. ...
- Codeforces 685C - Optimal Point(分类讨论+乱搞)
Codeforces 题面传送门 & 洛谷题面传送门 分类讨论神题. 首先看到最大值最小,一眼二分答案,于是问题转化为判定性问题,即是否 \(\exists x_0,y_0,z_0\) 满足 ...
- Codeforces 1513F - Swapping Problem(分类讨论+乱搞)
Codeforces 题目传送门 & 洛谷题目传送门 简单题,难度 *2500 的 D2F,就当调节一下一模炸裂了的自闭的心情,稍微写写吧. 首先我看到这题的第一反应是分类讨论+数据结构,即枚 ...
- Codeforces 1461F - Mathematical Expression(分类讨论+找性质+dp)
现场 1 小时 44 分钟过掉此题,祭之 大力分类讨论. 如果 \(|s|=1\),那么显然所有位置都只能填上这个字符,因为你只能这么填. scanf("%d",&n);m ...
- Codeforces 870F - Path(数论+分类讨论+正难则反)
Codeforces 题目传送门 & 洛谷题目传送门 首先考虑 \(d(u,v)\) 是个什么东西,分情况讨论: \(u\not\perp v\),\(d(u,v)=1\) \(u\perp ...
- CodeForces - 792C Divide by Three (DP做法)
C. Divide by Three time limit per test: 1 second memory limit per test: 256 megabytes input: standar ...
- ACM学习历程—CodeForces 590A Median Smoothing(分类讨论 && 数学)
题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给一个串,头和尾每次变换保持不变. 中间的a[i]变成a[i-1],a[i],a[i+ ...
- CodeForces 382C Arithmetic Progression (排序+分类讨论)
题意:给出一个长度为n的序列,表示有n张卡片,上面的数字,现在还有一张卡片,上面没有数字,问说可以写几种数字在这张卡片上面, 使得n+1张卡片上的数字可以排列成一个等差数列,有无限多种时输出-1. 析 ...
- CodeForces - 1221E Game With String 分类讨论
首先分析A能获胜的情况 A能获胜 当且仅当A拿完后所有剩下的都<b 所以一旦存在一个大小为X的 且 b<=X<a 则必是后手赢 当X为 a<=x<2*b 的时候 无论A或 ...
随机推荐
- Redis(1.12)Redis cluster搭建常见错误
[1]gem install redis 报错 redis-cluster安装需要通过gem install redis来安装相关依赖.否则报错.通过gem install redis执行后会出现两个 ...
- 在Hadoop中ResourceManager是干什么的?
[学习笔记] 1)ResourceManager:马克-to-win @ 马克java社区:防盗版实名手机尾号:73203.当应用程序对集群资源需求时,ResourceManager是Yarn集群主控 ...
- Yii源码分享-底层+view层1
文件:https://files.cnblogs.com/files/cwlife/YII%E7%BB%A7%E6%89%BF%E6%A0%91.xmind.zip 视屏:https://v.qq.c ...
- js 中的 number 为何很怪异
js 中的 number 为何很怪异 声明:需要读者对二进制有一定的了解 对于 JavaScript 开发者来说,或多或少都遇到过 js 在处理数字上的奇怪现象,比如: > 0.1 + 0.2 ...
- Dreamoon and Strings CodeForces - 477C (字符串dp)
大意: 给定字符串$s$, $p$, 对于$0\le x\le |s|$, 求$s$删除$x$个字符后, $p$在$s$中的最大出现次数. 显然答案是先递增后递减的, 那么问题就转化求最大出现次数为$ ...
- spring jpa 学习笔记(一) 之集成
一.pom 配置 <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apa ...
- gperftools cpp wrapper
gperftools cpp wrapper // Compile command : ${CXX} -o test_profiler.elf -DUSE_GPERFTOOLS -DDEBUG -D_ ...
- QT编译Mysql驱动问题及解决方案
默认情况下,qt 并没有自带mysql的数据库插件,需要自己编译先安装mysql server ,运行setup.exe时选择自定义安装,安装目录设为"D:\mysqldev"不要 ...
- Python字符串、组合数据类型练习
一.Python字符串练习 1.http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html 取得校园新闻的编号. (这个方法就很多了,一般方 ...
- 解决'androidx.arch.core:core-runtime' has different version for the compile (2.0.0) and runtime (2.0.1)
先说原因,我们引用的包版本不同产生了冲突,所以编译不通过.解决的办法是在引用的时候排除一个版本,只留一个版本. 解决过程: 先找出哪些库引用了相同的库,仅仅是版本不同. gradle app:depe ...