http://codeforces.com/problemset/problem/1005/D

 题意:

给一个仅包含数字的字符串,将字符串分割成多个片段(无前导0),求这些片段里最多有多少是3的倍数

思路一(贪心):

from:https://blog.csdn.net/islittlehappy/article/details/81006849

一个数是3的倍数,则各位的和能被3整除。

对于单独的一个数字,如果是3的倍数,则ans++

否则,考虑连续的两个数字,如果是,则ans++

如果第三个数本身是3的倍数,则ans++

如果第三个数不是3的倍数,则对于前两个数的和对3取余,结果为[1,1]或者[2,2](如果为[1,2],[2,1],则这两个数字能够被3整除)

对于第三个数对3取余,结果为0,1,2

0:第三个数本身能被3整除ans++

1:[1,1,1]是3的倍数取全部,[2,2,1]取后两个   ans++

2:[1,1,2]取后两个 [2,2,2]是3的倍数,取全部  ans++

所以 对于n=3 一定可以找到

 #include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 2e5+; char str[maxn]; int main()
{
scanf("%s",str);
int t=,sum=,ans=,n=;
for(int i=;i<strlen(str);i++)
{
t=(str[i]-'')%;
sum+=t;
n++;
if(t==||sum%==||n==)
{
ans++;
n=sum=;
}
}
printf("%d\n",ans);
return ;
}

思路二(dp):

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
#define Bug cout<<"---------------------"<<endl
const int maxm=1e6+;
const int maxn=1e6+;
using namespace std; char str[maxn];
LL sum[maxn];//前缀和
int dp[maxn];//dp[i]表示前i个数的答案 int main()
{
scanf("%s",str+);
for(int i=;str[i];i++)
{
if(i==)
sum[i]=str[i]-'';
else
sum[i]=sum[i-]+str[i]-'';
}
int num=;
if((str[]-'')%==)
dp[]=;
for(int i=;str[i];i++)
{
if((str[i]-'')%==)
dp[i]=dp[i-]+;
else
{
for(int j=i-;j>;j--)
{
if((sum[i]-sum[j-])%==)
{
dp[i]=max(dp[i],dp[j-]+);
break;
}
else
dp[i]=dp[i-];
}
}
}
printf("%d\n",dp[strlen(str+)]);
return ;
}

CodeForces 1005D Polycarp and Div 3(思维、贪心、dp)的更多相关文章

  1. 『ACM C++』 Codeforces | 1005D - Polycarp and Div 3

    今天佛了,魔鬼周一,在线教学,有点小累,但还好,今天AC了一道,每日一道,还好达成目标,还以为今天完不成了,最近任务越来越多,如何高效完成该好好思考一下了~最重要的还是学业的复习和预习. 今日兴趣新闻 ...

  2. CF1005D Polycarp and Div 3 思维

    Polycarp and Div 3 time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  3. 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

  4. Codeforces 1208F Bits And Pieces 位运算 + 贪心 + dp

    题意:给你一个序列a, 问a[i] ^ (a[j] & a[k])的最大值,其中i < j < k. 思路:我们考虑对于每个a[i]求出它的最优解.因为是异或运算,所以我们从高位向 ...

  5. Sorted Adjacent Differences(CodeForces - 1339B)【思维+贪心】

    B - Sorted Adjacent Differences(CodeForces - 1339B) 题目链接 算法 思维+贪心 时间复杂度O(nlogn) 1.这道题的题意主要就是让你对一个数组进 ...

  6. Educational Codeforces Round 61 F 思维 + 区间dp

    https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...

  7. Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找

    The link to problem:Problem - D - Codeforces   D. Range and Partition  time limit per test: 2 second ...

  8. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  9. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

随机推荐

  1. Loading PDSC Debug Description Failed for STMicroelectronics STM32Lxxxxxxx”

    今天在调程序的时候遇到这个问题 解决办法:将安装在MDK下面的文件属性由只读去掉: 成功!可以下载.

  2. 【MySQL基础打卡(一)】查询语句项目作业

    文章目录 1.查找email表中重复的电子邮箱 1.1 创建email数据表 1.2 找出重复Email 2.查找大国家 2.1 创建数据表 2.2 查找大国家 对于安装MySQL比较恐惧,所以想在虚 ...

  3. zset底层数据结构

    redis zset底层数据结构 https://www.jianshu.com/p/fb7547369655 跳跃列表(Skip List)与其在Redis中的实现详解 https://www.ji ...

  4. spring boot2 运行环境

    1.springboot个版本系统需求 spring boot maven jdk 内置tomcat 内置jetty servlet 2.0.x 3.2+ 8或9 8.5(3.1) 9.4(3.1) ...

  5. JS/JQUERY函数库

    1. 判断字符串是否为空 function isEmptyString(str) { return str == undefined || str == "" || str == ...

  6. Neo4j--节点的增删查改基本用法

    注 node-name 和  label-name node-name 有点句柄的味道. 从面向对象来理解,label-name相当于一个类,node-name相当于这个类的对象. 类比关系型数据库的 ...

  7. NBU For Windows 更新后无法启动Java Console

    系统环境:Win Server 2016 备份软件:NBU 8.1   Case :Windows系统默认自动安装系统补丁,重启过程中自动进行了Update,打上了最新的补丁后,NBU Java Co ...

  8. jstl中遍历Map

    在jstl中遍历Map和遍历List与数组一样,都是使用forEach标签. 例子: <%@ page import="java.util.Map" %> <%@ ...

  9. ab工具压接口的时候post传参问题

    ab  -n 10000 -c 40 -p  [参数所在文件] -T 'application/json'  http://xxx 以上命令, 压测需要post json格式的参数的api时, 一定注 ...

  10. P3370 【模板】字符串哈希 题解

    地址:https://www.luogu.org/problem/P3370 求不同字符串的数量 这题用set也可以过,但是hash更高大上嘛. 哈希其实就是将一个字符串映射成一个值,并且要让这些值不 ...