博客
关于我
Making the Grade [POJ3666] [DP]
阅读量:800 次
发布时间:2023-02-06

本文共 1428 字,大约阅读时间需要 4 分钟。

为了解决这个问题,我们需要将给定的序列转换为单调不增或单调不减序列,并以最小代价完成转换。代价定义为所有元素调整后的值与原值的差的绝对值之和。我们可以使用动态规划来解决这个问题。

方法思路

  • 问题分析:我们需要将序列转换为单调序列,选择递增或递减中的代价更小的方式。动态规划是一个有效的方法来解决这种问题。
  • 离散化处理:由于序列中的数字可能很大,我们对序列进行排序并离散化处理,以减少计算复杂度。
  • 状态定义:定义 dp[i][j] 为处理到第 i 个元素,当前元素为 j(离散化后的值)时的最小代价。
  • 状态转移:对于每个元素 i,我们遍历所有可能的值 j,并记录前一步的最小代价 mn,然后更新当前状态的代价。
  • 优化处理:在每一步中,记录最小值 mn 以避免重复计算,从而优化时间复杂度。
  • 解决代码

    #include 
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #define RG register ll#define rep(i, a, b) for (RG i = a; i <= b; ++i)#define per(i, a, b) for (RG i = a; i >= b; --i)#define ll long long#define inf (1 << 30)#define maxn 2000using namespace std;ll n;ll h[maxn], b[maxn];ll dp[maxn][maxn];inline ll read() { ll x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + (c - '0'); c = getchar(); } return x * f;}void work() { rep(i, 1, n) { ll mn = inf; rep(j, 1, n) { mn = min(mn, dp[i-1][j]); ll cost = (h[i] >= b[j]) ? h[i] - b[j] : b[j] - h[i]; dp[i][j] = mn + cost; } }}ll ans = inf;rep(i, 1, n) { ans = min(ans, dp[n][i]);}cout << ans << endl;

    代码解释

  • 读取输入:使用 read 函数读取输入数据,处理序列长度和序列元素。
  • 排序离散化:将序列 h 排序并存储在数组 b 中。
  • 动态规划初始化:初始化 dp 数组,dp[0][j] 设为 0。
  • 动态规划处理:对于每个元素 i,遍历所有可能的值 j,记录前一步的最小值 mn,并更新当前状态的代价。
  • 计算最小代价:遍历所有可能的值 j,找到最小的代价 ans 并输出。
  • 这种方法通过离散化和动态规划优化,确保了在合理的时间复杂度内解决问题,适用于较大的输入规模。

    转载地址:http://ieufk.baihongyu.com/

    你可能感兴趣的文章
    NN&DL4.7 Parameters vs Hyperparameters
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named 'pandads'
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>