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 ...
随机推荐
- P 1034 有理数四则运算
转跳点:
- vnpy交易学习接口(2)
#来源于github下载vnpy版本 20180413 11.多投资标的情况下,该如何修改? 10.stop和limit报单有什么区别呢? 在交易时用得最多的是二类定单,第一类是市价单(Market ...
- 大数据高可用集群环境安装与配置(10)——安装Kafka高可用集群
1. 获取安装包下载链接 访问https://kafka.apache.org/downloads 找到kafka对应版本 需要与服务器安装的scala版本一致(运行spark-shell可以看到当前 ...
- Abstract抽象类 && Interface接口
# 抽象类 ## 将相同的但是不确定的动作提取出来,抽象化,抽象类的意义在于,在子类中进行实现. ## 抽象类可以被继承,子类继承抽象类时,需要对抽象方法进行完全实现. ## 抽象方法不能有方法体. ...
- Sequence Models Week 1 Improvise a Jazz Solo with an LSTM Network
Improvise a Jazz Solo with an LSTM Network Welcome to your final programming assignment of this week ...
- trove database功能总结
我曾经以为trove只负责数据库(datastore)的部署,最近才发现trove可以进行数据库(database)的创建. 首先是列出某个实例上(instance)数据库(datastrore)上的 ...
- 18 12 23 html 基本学习
一个html的基本结构如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- VUE- 访问服务器端数据 Vue-resource
VUE- 访问服务器端数据 Vue-resource 1. 安装 vue-resource cnpm install vue-resource --save 安装完毕后,在main.js中导入,如下所 ...
- HDU 3484 Matrix Game 枚举暴力
上次周赛碰到这个题目,居然都没思路,真是不应该啊,起码也应该想到枚举法. 因为题目只允许每一row进行reverse操作,而每两列可以进行交换操作,所以首先把row的变化固定下来,即枚举第一列与第1- ...
- 干货 | 用Serverless快速在APP中构建调研问卷
Serverless 计算将会成为云时代默认的计算范式,并取代 Serverful (传统云)计算模式,因此也就意味着服务器 -- 客户端模式的终结. ------<简化云端编程:伯克利视角下的 ...