CodeForces 1005D Polycarp and Div 3(思维、贪心、dp)
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)的更多相关文章
- 『ACM C++』 Codeforces | 1005D - Polycarp and Div 3
今天佛了,魔鬼周一,在线教学,有点小累,但还好,今天AC了一道,每日一道,还好达成目标,还以为今天完不成了,最近任务越来越多,如何高效完成该好好思考一下了~最重要的还是学业的复习和预习. 今日兴趣新闻 ...
- CF1005D Polycarp and Div 3 思维
Polycarp and Div 3 time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
- Codeforces 1208F Bits And Pieces 位运算 + 贪心 + dp
题意:给你一个序列a, 问a[i] ^ (a[j] & a[k])的最大值,其中i < j < k. 思路:我们考虑对于每个a[i]求出它的最优解.因为是异或运算,所以我们从高位向 ...
- Sorted Adjacent Differences(CodeForces - 1339B)【思维+贪心】
B - Sorted Adjacent Differences(CodeForces - 1339B) 题目链接 算法 思维+贪心 时间复杂度O(nlogn) 1.这道题的题意主要就是让你对一个数组进 ...
- Educational Codeforces Round 61 F 思维 + 区间dp
https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...
- 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 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
随机推荐
- ZOJ - 3870 Team Formation(异或)
题意:给定N个数,求这N个数中满足A ⊕ B > max{A, B})的AB有多少对.(A,B是N中的某两个数) 分析: 1.异或,首先想到转化为二进制. eg:110011(A)和 1(B)- ...
- windows内核安全编程书籍
windows internals 的中文译本 windows内核原理与实现 版权声明:本文为博主原创文章,未经博主允许不得转载.
- 一、SAP中添加一个模块到收藏夹后,显示事务代码
一.在SAP中,如果添加一个模块到收藏夹,默认是看不到事务代码的,如图: 二.我们在附件->设置中勾选显示技术名称 三.保存之后,就会显示出事务代码,如图所示: 不忘初心,如果您认为这篇文章有价 ...
- [XSS防御]HttpOnly之四两拨千斤
今天看了<白帽子讲web安全>一书,顺便记录一下,HttpOnly的设置 httponly的设置值为 TRUE 时,使得Javascript无法获取到该值,有效地防御了XSS打管理员的 c ...
- Python 使用print实现进度
import time print("0%",end='') time.sleep(2) print("\r1%",end='') time.sleep(2) ...
- CF940F Machine Learning(带修莫队)
首先显然应该把数组离散化,然后发现是个带修莫队裸题,但是求mex比较讨厌,怎么办?其实可以这样求:记录每个数出现的次数,以及出现次数的出现次数.至于求mex,直接暴力扫最小的出现次数的出现次数为0的正 ...
- Q7:Reverse Integer
7. Reverse Integer 官方的链接:7. Reverse Integer Description : Given a 32-bit signed integer, reverse dig ...
- Map 查找表操作
package seday13; import java.util.HashMap; import java.util.Map; /** * @author xingsir * java.util.M ...
- js 关联数组
踩得坑: JS ,通过 new Array()创建了一个数组: var param = new Array();param["key1"] = value1;param[&quo ...
- 应用层上的协议HTTP
HTTP http://www.runoob.com/http/http-tutorial.html https://www.cnblogs.com/houfee/articles/9161847.h ...