Educational Codeforces Round 18 C. Divide by Three DP

本文介绍了一种使用动态规划方法来解决一个特定问题的算法:即给定一个正整数,通过删除最少数量的数字使其变为3的倍数,并确保结果没有前导零。文章详细解释了状态定义、转移方程以及实现细节。
C. Divide by Three
 

A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.

The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.

Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n.

If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.

Input

The first line of input contains n — a positive integer number without leading zeroes (1 ≤ n < 10100000).

Output

Print one number — any beautiful number obtained by erasing as few as possible digits. If there is no answer, print  - 1.

Examples
input
1033
output
33
Note

In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two.

 

 题意:

  给你一个01串,问你最少删除多少个字符,使得余下的串10进制下%3=0,不得有前导0

题解:

  设定dp[i][j][0/1/2]表示前i个字符中,组成%3=j的串需要的最少删除次数;

  同时0表示还未填数,

     1表示有一个前导0,

   2表示开头填了一个非0数

  需要记录路径pre

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 1e5+10, M = 1e3+20, mod = 1e9+7, inf = 2e9;


int dp[N][5][3],pre[N][5][3];//前i个数mod3 = j最少需要删除的字母个数 是否有前导0
char s[N];
int n,a[N],ans[N];
int main() {
    scanf("%s",s+1);
    int n = strlen(s+1);
    for(int i = 1; i <= n; ++i) a[i] = s[i] - '0';
    for(int i = 0; i <= n; ++i) {
        for(int j = 0; j < 3; ++j) dp[i][j][0] = inf,dp[i][j][1] = inf, dp[i][j][2] = inf;
    }
    dp[0][0][0] = 0;
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < 3; ++j) {
            if(dp[i][j][0] < dp[i+1][(j+a[i+1])%3][(a[i+1])==0?1:2]) {
                dp[i+1][(j+a[i+1])%3][(a[i+1])==0?1:2] = dp[i][j][0];
                pre[i+1][(j+a[i+1])%3][(a[i+1])==0?1:2] = 1;
            }
            if(dp[i][j][1]+1 < dp[i+1][(j+a[i+1])%3][(a[i+1])==0?1:2]) {
                dp[i+1][(j+a[i+1])%3][(a[i+1])==0?1:2] = dp[i][j][1]+1;
                pre[i+1][(j+a[i+1])%3][(a[i+1])==0?1:2] = 1;
            }
            if(dp[i][j][2] < dp[i+1][(j+a[i+1])%3][2]) {
                dp[i+1][(j+a[i+1])%3][2] = dp[i][j][2];
                pre[i+1][(j+a[i+1])%3][2] = 1;
            }

            if(dp[i][j][1]+1 < dp[i+1][j][1]) {
               dp[i+1][j][1] = dp[i][j][1]+1;
               pre[i+1][j][1] = -1;
            }
            if(dp[i][j][0]+1 < dp[i+1][j][0]) {
               dp[i+1][j][0] = dp[i][j][0]+1;
               pre[i+1][j][0] = -1;
            }
            if(dp[i][j][2]+1 < dp[i+1][j][2]) {
               dp[i+1][j][2] = dp[i][j][2]+1;
               pre[i+1][j][2] = -1;
            }
        }
    }
    if(dp[n][0][2] >= inf && dp[n][0][1] >= inf) {
        puts("-1");
        return 0;
    }
    if(dp[n][0][1] < dp[n][0][2]) {
        puts("0");
        return 0;
    }
    int j = 0,num = n - dp[n][0][2];
    for(int i = n; i >= 1; --i) {
        if(pre[i][j][2] == 1) {
            ans[num--] = a[i];
            j = ((j - a[i])%3 + 3) % 3;
        }
        if(num == 0) break;
    }
    for(int i = 1; i <= n - dp[n][0][2]; ++i) cout<<ans[i];
    return 0;
}

 

  

转载于:https://www.cnblogs.com/zxhl/p/6638762.html

### Codeforces Round 927 Div. 3 比赛详情 Codeforces是一个面向全球程序员的比赛平台,定期举办不同级别的编程竞赛。Div. 3系列比赛专为评级较低的选手设计,旨在提供更简单的问题让新手能够参并提升技能[^1]。 #### 参赛规则概述 这类赛事通常允许单人参加,在规定时间内解决尽可能多的问题来获得分数。评分机制基于解决问题的速度以及提交答案的成功率。比赛中可能会有预测试案例用于即时反馈,而最终得分取决于系统测试的结果。此外,还存在反作弊措施以确保公平竞争环境。 ### 题目解析:Moving Platforms (G) 在这道题中,给定一系列移动平台的位置和速度向量,询问某时刻这些平台是否会形成一条连续路径使得可以从最左端到达最右端。此问题涉及到几何学中的线段交集判断和平面直角坐标系内的相对运动分析。 为了处理这个问题,可以采用如下方法: - **输入数据结构化**:读取所有平台的数据,并将其存储在一个合适的数据结构里以便后续操作。 - **时间轴离散化**:考虑到浮点数精度误差可能导致计算错误,应该把整个过程划分成若干个小的时间间隔来进行模拟仿真。 - **碰撞检测算法实现**:编写函数用来判定任意两个矩形之间是否存在重叠区域;当发现新的连接关系时更新可达性矩阵。 - **连通分量查找技术应用**:利用图论知识快速求解当前状态下哪些节点属于同一个集合内——即能否通过其他成员间接相连。 最后输出结果前记得考虑边界条件! ```cpp // 假设已经定义好了必要的类和辅助功能... bool canReachEnd(vector<Platform>& platforms, double endTime){ // 初始化工作... for(double currentTime = startTime; currentTime <= endTime ;currentTime += deltaT){ updatePositions(platforms, currentTime); buildAdjacencyMatrix(platforms); if(isConnected(startNode,endNode)){ return true; } } return false; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值