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. FMDB 加入数据库版本信息

    //创建数据库版本表 if (![db tableExists:@"tb_dbInfo"]) { result = [db executeUpdate:@"CREATE ...

  2. oracle 的存储过程

    -----推荐视频    https://ke.qq.com/webcourse/index.html#course_id=292495&term_id=100346599&taid= ...

  3. JQ+AJAX 发送异步请求

    1. load() ; 作用:通过ajax 请求从服务器加载数据,并添加到符合要求的节点上 用法:$node.load(请求地址,请求参数) 请求参数写法: --"username=admi ...

  4. k8s资源pod yaml文件分析

    apiVersion: v1 kind: Pod_name metadata name: pod_name #描述Pod的名字 namespace: default #描述Pod所在命名空间,如果不设 ...

  5. bzoj 3876: [Ahoi2014]支线剧情

    就是加一个1的下界就好了. #include<bits/stdc++.h> #define N 100005 #define LL long long #define inf 0x3f3f ...

  6. 项目进度02-Day3

    ①今天做了什么? 数据库数据的重置.之前的用户类字段的补充.简单的平台信息查询 ②明天要做什么?   分类浏览和综合查询功能. ③遇到了什么问题? 出现问题:Parameter index out o ...

  7. 备份 分区表 mbr

    备份方法:   1.备份分区表信息 sudo fdisk -l >hda.txt  #分区表信息重定向输出到文件中 2.备份MBR linux@linux-desktop:~/ex$ sudo ...

  8. Mac Outlook 2016 无法打开会议室日历

    问题:Mac Outlook 2016 无法打开会议室日历信息,报错截图如下: 解决方案: Set-MailboxFolderPermission -Identity XXX@xxx.com:\日历 ...

  9. ES系列之Promise async 和 await

    概述 promise是异步编程的一种解决方案,比传统的解决方案—回调函数和事件—更合理更强大. 所谓的promise就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作的结果). Pro ...

  10. 寒假day12

    今天写了一点论文,刷了一些算法题