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 一定可以找到

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 const int maxn = 2e5+5;
 5
 6 char str[maxn];
 7
 8 int main()
 9 {
10     scanf("%s",str);
11     int t=0,sum=0,ans=0,n=0;
12     for(int i=0;i<strlen(str);i++)
13     {
14         t=(str[i]-‘0‘)%3;
15         sum+=t;
16         n++;
17         if(t==0||sum%3==0||n==3)
18         {
19             ans++;
20             n=sum=0;
21         }
22     }
23     printf("%d\n",ans);
24     return 0;
25 }

思路二(dp):

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int mod=1e9+7;
16 //const double PI=acos(-1);
17 #define Bug cout<<"---------------------"<<endl
18 const int maxm=1e6+10;
19 const int maxn=1e6+10;
20 using namespace std;
21
22 char str[maxn];
23 LL sum[maxn];//前缀和
24 int dp[maxn];//dp[i]表示前i个数的答案
25
26 int main()
27 {
28     scanf("%s",str+1);
29     for(int i=1;str[i];i++)
30     {
31         if(i==1)
32             sum[i]=str[i]-‘0‘;
33         else
34             sum[i]=sum[i-1]+str[i]-‘0‘;
35     }
36     int num=0;
37     if((str[1]-‘0‘)%3==0)
38         dp[1]=1;
39     for(int i=2;str[i];i++)
40     {
41         if((str[i]-‘0‘)%3==0)
42             dp[i]=dp[i-1]+1;
43         else
44         {
45             for(int j=i-1;j>0;j--)
46             {
47                 if((sum[i]-sum[j-1])%3==0)
48                 {
49                     dp[i]=max(dp[i],dp[j-1]+1);
50                     break;
51                 }
52                 else
53                     dp[i]=dp[i-1];
54             }
55         }
56     }
57     printf("%d\n",dp[strlen(str+1)]);
58     return 0;
59 }

原文地址:https://www.cnblogs.com/jiamian/p/11804471.html

时间: 2025-01-14 00:00:53

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

Codeforces Round #277.5 (Div. 2)——C贪心—— Given Length and Sum of Digits

You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base with

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

题意:给你一个序列a, 问a[i] ^ (a[j] & a[k])的最大值,其中i < j < k. 思路:我们考虑对于每个a[i]求出它的最优解.因为是异或运算,所以我们从高位向低位枚举,如果这一位a[i]是0,我们就在a[i]的右边找两个位置让它们按位与起来这位是1.那么,我们贪心的保留可以通过按位与凑出某个二进制数的最靠右的两个位置.这个可以通过dp的方式预处理出来.之后,我们枚举每一个数a[i],先找出它的哪些位是0,之后从高位到低位枚举,判断这一位是否可以变成1.如果之前已经

Codeforces Round #FF(255) (Div. 2)

A题 /* ID: neverchanje PROG: LANG: C++11 */ #include<vector> #include<iostream> #include<cstring> #include<string> #include<algorithm> #include<cmath> #include<cstdio> #include<set> #include<queue> #inc

Codeforces Round #277.5 (Div. 2) JAVA版题解

Codeforces Round #277.5 (Div. 2) A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output In this problem your goal is to sort an array consisting of n integers in at most n swaps. For t

Codeforces Beta Round #22 (Div. 2 Only)

Codeforces Beta Round #22 (Div. 2 Only) http://codeforces.com/contest/22 A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 500005 7 t

Codeforces Beta Round #35 (Div. 2)

Codeforces Beta Round #35 (Div. 2) http://codeforces.com/contest/35 A 这场的输入输出是到文件中的,不是标准的输入输出...没注意看,RE了好久... 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x

Codeforces Beta Round 84 (Div. 2 Only)

layout: post title: Codeforces Beta Round 84 (Div. 2 Only) author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces 传送门 不得不说这远古场太简单了 A - Nearly Lucky Number (签到) 题意 给出一个数字 求其中7和4的数目是否是7和4组成 #include<bits/stdc++.h> using namespa

Codeforces Round #615(Div.3)解题报告

Codeforces Round #615(Div.3)解题报告 A. Collecting Coins 注意\(n\)可能不够用的情况. #include<bits/stdc++.h> using namespace std; typedef long long ll; int a, b, c, n; void solve() { cin >> a >> b >> c >> n; int mx = max(max(a, b), c); int

Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array

E. Lucky Array Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467are not. Petya has an arra