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. phpstudy后门复现(9.29第十五天)

    本人转自:https://www.cnblogs.com/yuanshu/p/11613796.html 一.漏洞位置 程序自带的PHP的php_xmlrpc.dll模块中有隐藏后门,受影响的版本有p ...

  2. 【LeetCode】最长公共子序列

    [问题]给定两个字符串A和B,长度分别为m和n,要求找出它们最长的公共子串,并返回其长度.例如:A = "HelloWorld"B = "loop"则A与B的最 ...

  3. MySLQ排序后标记排行

    查询排行及所有(表名.*) 1. ; AS top, customer.* FROM customer 2. AS top, customer.* ) r, customer ORDER BY cus ...

  4. Python Learning Day7

    破解极验滑动验证 博客园登录url: https://account.cnblogs.com/signin?returnUrl=https%3A%2F%2Fwww.cnblogs.com%2F 代码逻 ...

  5. 了解OOM

    1)什么是OOM? OOM,全称“Out Of Memory”,翻译成中文就是“内存用完了”,来源于java.lang.OutOfMemoryError.看下关于的官方说明: Thrown when ...

  6. 18 12 29 css background

    background属性 属性解释 background属性是css中应用比较多,且比较重要的一个属性,它是负责给盒子设置背景图片和背景颜色的,background是一个复合属性,它可以分解成如下几个 ...

  7. js 关联数组

    踩得坑: JS ,通过 new Array()创建了一个数组: var param =  new Array();param["key1"] = value1;param[&quo ...

  8. 墙壁涂色(DP)

    蒜头君觉得白色的墙面好单调,他决定给房间的墙面涂上颜色. 他买了 3 种颜料分别是红.黄.蓝,然后把房间的墙壁竖直地划分成 n 个部分,蒜头希望每个相邻的部分颜色不能相同. 他想知道一共有多少种给房间 ...

  9. vivado下创建基本时序周期约束

    创建基本时钟周期约束.(验证我们的设计能否在期望的频率上运行) (学习记录,晚一点会做实验传上来的.) 时钟基本概念:https://blog.csdn.net/wordwarwordwar/arti ...

  10. 池ThreadPoolExecutor使用简介

    public static void main(String[] args) { //guava 创建线程池 //https://blog.csdn.net/chinabestchina/articl ...