Smaller numbers

文章讲述了如何使用区间动态规划解决蓝桥杯2023省A题目,给定一个数字字符串,计算通过最多反转一次子串后使得新数字小于原数字的不同方案数。
摘要由CSDN通过智能技术生成

[蓝桥杯 2023 省 A] 更小的数

题目描述

image

小蓝有一个长度均为 n n n 且仅由数字字符 0 ∼ 9 0 \sim 9 09 组成的字符串,下标从 0 0 0 n − 1 n-1 n1,你可以将其视作是一个具有 n n n 位的十进制数字 n u m num num,小蓝可以从 n u m num num 中选出一段连续的子串并将子串进行反转,最多反转一次。小蓝想要将选出的子串进行反转后再放入原位置处得到的新的数字 n u m n e w num_{new} numnew 满足条件 n u m n e w < n u m num_{new}<num numnew<num,请你帮他计算下一共有多少种不同的子串选择方案,只要两个子串在 n u m num num 中的位置不完全相同我们就视作是不同的方案。

注意,我们允许前导零的存在,即数字的最高位可以是 0 0 0,这是合法的。

输入格式

输入一行包含一个长度为 n n n 的字符串表示 n u m num num(仅包含数字字符 0 ∼ 9 0 \sim 9 09),从左至右下标依次为 0 ∼ n − 1 0 \sim n-1 0n1

输出格式

输出一行包含一个整数表示答案。

样例 #1

样例输入 #1

210102

样例输出 #1

8

提示

【样例说明】

一共有 8 8 8 种不同的方案:

  1. 所选择的子串下标为 0 ∼ 1 0\sim1 01,反转后的 n u m n e w = 120102 < 210102 num_{new} = 120102 < 210102 numnew=120102<210102
  2. 所选择的子串下标为 0 ∼ 2 0\sim2 02,反转后的 n u m n e w = 012102 < 210102 num_{new} = 012102 < 210102 numnew=012102<210102
  3. 所选择的子串下标为 0 ∼ 3 0\sim3 03,反转后的 n u m n e w = 101202 < 210102 num_{new} = 101202 < 210102 numnew=101202<210102
  4. 所选择的子串下标为 0 ∼ 4 0\sim4 04,反转后的 n u m n e w = 010122 < 210102 num_{new} = 010122 < 210102 numnew=010122<210102
  5. 所选择的子串下标为 0 ∼ 5 0\sim5 05,反转后的 n u m n e w = 201012 < 210102 num_{new} = 201012 < 210102 numnew=201012<210102
  6. 所选择的子串下标为 1 ∼ 2 1\sim2 12,反转后的 n u m n e w = 201102 < 210102 num_{new} = 201102 < 210102 numnew=201102<210102
  7. 所选择的子串下标为 1 ∼ 4 1\sim4 14,反转后的 n u m n e w = 201012 < 210102 num_{new} = 201012 < 210102 numnew=201012<210102
  8. 所选择的子串下标为 3 ∼ 4 3\sim4 34,反转后的 n u m n e w = 210012 < 210102 num_{new} = 210012 < 210102 numnew=210012<210102
【评测用例规模与约定】

对于 20 % 20\% 20% 的评测用例, 1 ≤ n ≤ 100 1 \le n \le 100 1n100

对于 40 % 40\% 40% 的评测用例, 1 ≤ n ≤ 1000 1 \le n \le 1000 1n1000

对于所有评测用例, 1 ≤ n ≤ 5000 1 \le n \le 5000 1n5000

区间DP 定义dp[l][r]为1时翻转后小于翻转前, 2时等于, 3时大于


```cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define endl '\n'
#define debug(x) cout << __FUNCTION__ << ": " << #x " = " << (x) << endl
using ll = long long;

const int maxn = 1e6 + 10;
const int maxm = 2e6 + 10;
const ll INF = 3e18;
const int mod = 998244353;
int n, m, k;
int dp[5010][5010]; // 1: <    2: =    3: >

void kkkyyyhhh() {
    string s;
    cin >> s;
    n = s.size();
    s = '>' + s;
    int ans = 0;
    for (int len = 1; len <= n; len++) {
        for (int l = 1; l + len - 1 <= n; l++) {
            int r = l + len - 1;
            if (len == 1) {
                dp[l][r] = 2;
            }
            else {
                if (s[l] > s[r]) dp[l][r] = 1;
                else if (s[l] < s[r]) dp[l][r] = 3;
                else {
                    dp[l][r] = dp[l + 1][r - 1];
                }
            }
            if (dp[l][r] == 1) {
                //cout << l << " " << r << endl;
                ans++;
            }
        }
    }
    cout << ans << endl;
}       

signed main() {
    IOS;
    int w_ = 1;
   // cin >> w_;
    while (w_--) kkkyyyhhh();
    return 0;
}



  • 17
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
项目:使用 JavaScript 编写的杀死幽灵游戏(附源代码) 杀死鬼魂游戏是使用 Vanilla JavaScript、CSS 和 HTML 画布开发的简单项目。这款游戏很有趣。玩家必须触摸/杀死游荡的鬼魂才能得分。您必须将鼠标悬停在鬼魂上 - 尽量得分。鬼魂在眨眼间不断从一个地方移动到另一个地方。您必须在 1 分钟内尽可能多地杀死鬼魂。 游戏制作 这个游戏项目只是用 HTML 画布、CSS 和 JavaScript 编写的。说到这个游戏的特点,用户必须触摸/杀死游荡的幽灵才能得分。游戏会根据你杀死的幽灵数量来记录你的总分。你必须将鼠标悬停在幽灵上——尽量得分。你必须在 1 分钟内尽可能多地杀死幽灵。游戏还会显示最高排名分数,如果你成功击败它,该分数会在游戏结束屏幕上更新。 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox。要玩游戏,首先,单击 index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。
Create a class called Rational for performing arithmetic with fractions. Use integer variables to represent the private data of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should store the fraction in reduced form. For example, the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator. In order to compute the reduced form, you need to write a reduction function which uses the Euclidean algorithm to get the greatest common divisor (GCD) of the numerator and denominator first and then divides GCD to get the reduced numerator and denominator. Provide public member functions that perform each of the following tasks: (a) Subtract a Rational number from the other Rational number. The result should be stored in reduced form. (b) Divide a Rational number by the other Rational number. The result should be stored in reduced form. (c) Print Rational numbers in the form a/b, where a is the numerator and b is the denominator. (d)Compare two Rational numbers to make sure which one is smaller or they are equal. (1 for the first number, 2 for the second number and 0 if they are equal) Please also write a main function to prompt the user to input two Rational numbers . Subtract one number from the other from these two numbers using (a) and then print the result using (c). Divide one number from the other from these two numbers using (b) and then print the result using (c). Compare these two Rational numbers using (d) and indicate which one is smaller or they are equal. 用c++5.11寫出,且使用using namespace std;
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值