S - Making the Grade

POJ - 3666

这个题目要求把一个给定的序列变成递增或者递减序列的最小代价。

这个是一个dp,对于这个dp的定义我觉得不是很好想,如果第一次碰到的话。

上网看了一下题解,dp[i][j]表示前面 i 位已经是有序的了,第 i+1 位变成第j大的最小代价。

这个转移可以保证有序这个条件。

递增递减d两次。

这个题目要离散化,为什么要离散化呢,推荐博客

我以前就是感觉数据大就要离散化,不然数组存不下,不过看了这篇题解感觉他讲的很对啊。

#include <cstring>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 2e3 + ;
ll dp1[maxn][maxn], dp2[maxn][maxn];
int a[maxn], b[maxn]; int main()
{
int n;
scanf("%d", &n);
for (int i = ; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i];
sort(b + , b + + n);
int len = unique(b + , b + + n) - b - ;
for (int i = ; i <= n; i++) {
for (int j = ; j <= len; j++) {
dp1[i][j] = dp2[i][j] = inf64;
}
}
for (int i = ; i <= len; i++) {
dp1[][i] = abs(b[i] - a[]);
dp2[n][i] = abs(b[i] - a[n]);
}
for(int i=;i<n;i++)
{
ll tmp = inf64;
for(int j=;j<=len;j++)
{
tmp = min(tmp, dp1[i][j]);//这一步我感觉很巧妙,优化了一个for循环,
//这里是再找j前面的最小的那个dp[i][j],因为dp[i][j]=min(dp[i-1][1~j])+abs()
dp1[i + ][j] = min(dp1[i + ][j], tmp + abs(a[i + ] - b[j]));
}
}
ll ans = inf64;
for (int i = ; i <= len; i++) ans = min(ans, dp1[n][i]);
for(int i=n;i>;i--)
{
ll tmp = inf64;
for(int j=;j<=len;j++)
{
tmp = min(tmp, dp2[i][j]);
dp2[i - ][j] = min(dp2[i - ][j], tmp + abs(a[i - ] - b[j]));
}
}
for (int i = ; i <= len; i++) ans = min(ans, dp2[][i]);
printf("%lld\n", ans);
return ;
}

然后还有一个题目和这个很类似,好像比这个要难一点,所以我才先写的这个题目。

C. Sonya and Problem Wihtout a Legend

之前的那篇博客也有推荐

这个题目我居然没有写出来,其实和上面基本上是一样的,只有一点点的区别,上面的是非严格的,这个是严格递增的。

然后我不知道怎么转化,或者说我尝试过了,发现不太对。

然后我就去搜了题解,发现有一种方法可以把严格转化成非严格就是 a[i]-i

这样之后求非严格就可以了,然后要注意因为这个是求差值最小,所以这个不会影响答案,

知道这个就很简单了,这个结论以后不要忘记

#include <cstring>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 3e3 + ;
ll dp[maxn][maxn];
ll a[maxn], b[maxn]; int main()
{
int n;
scanf("%d", &n);
for (int i = ; i <= n; i++) {
scanf("%lld", &a[i]);
a[i] -= i;
b[i] = a[i];
}
sort(b + , b + + n);
int len = unique(b + , b + + n) - b - ;
for (int i = ; i <= n; i++) {
for (int j = ; j <= len; j++) {
dp[i][j] = inf64;
}
}
for (int i = ; i <= len; i++) dp[][i] = abs(a[] - b[i]);
for(int i=;i<n;i++)
{
ll tmp = inf64;
for(int j=;j<=len;j++)
{
tmp = min(dp[i][j], tmp);
dp[i + ][j] = min(dp[i + ][j], tmp + abs(a[i + ] - b[j]));
}
}
ll ans = inf64;
for (int i = ; i <= len; i++) ans = min(ans, dp[n][i]);
printf("%lld\n", ans);
return ;
}

S - Making the Grade POJ - 3666 结论 将严格递减转化成非严格的的更多相关文章

  1. A-Making the Grade(POJ 3666)

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4656   Accepted: 2206 ...

  2. Making the Grade POJ - 3666

    A straight dirt road connects two fields on FJ's farm, but it changes elevation more than FJ would l ...

  3. DP:Making the Grade(POJ 3666)

     聪明的修路方案 题目大意:就是农夫要修一条路,现在要求这条路要么就是上升的,要么就是下降的,总代价为∑|a[i]-b[i]|,求代价最低的修路方案, (0 ≤ β≤ 1,000,000,000) , ...

  4. 「POJ 3666」Making the Grade 题解(两种做法)

    0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...

  5. Poj 3666 Making the Grade (排序+dp)

    题目链接: Poj 3666 Making the Grade 题目描述: 给出一组数,每个数代表当前位置的地面高度,问把路径修成非递增或者非递减,需要花费的最小代价? 解题思路: 对于修好的路径的每 ...

  6. 把一个序列转换成非严格递增序列的最小花费 POJ 3666

    //把一个序列转换成非严格递增序列的最小花费 POJ 3666 //dp[i][j]:把第i个数转成第j小的数,最小花费 #include <iostream> #include < ...

  7. B - Housewife Wind POJ - 2763 树剖+边权转化成点权

    B - Housewife Wind POJ - 2763 因为树剖+线段树只能解决点权问题,所以这种题目给了边权的一般要转化成点权. 知道这个以后这个题目就很简单了. 怎么转化呢,就把这个边权转化为 ...

  8. POJ - 3666 Making the Grade(dp+离散化)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  9. POJ 3666 Making the Grade(二维DP)

    题目链接:http://poj.org/problem?id=3666 题目大意:给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调不降或者单调不增(题目BUG,只能求 ...

随机推荐

  1. git撤销远程commit

    git reset --hard [commit_id] git push origin HEAD --force

  2. JS数据结构与算法 - 剑指offer二叉树算法题汇总

    ❗❗ 必看经验 在博主刷题期间,基本上是碰到一道二叉树就不会碰到一道就不会,有时候一个下午都在搞一道题,看别人解题思路就算能看懂,自己写就呵呵了.一气之下不刷了,改而先去把二叉树的基础算法给搞搞懂,然 ...

  3. sublime text3配置html环境

    1.安装View in Browser 2.配置快捷键 [1]Preferences—Key Bindings—User. [2]插入代码 [ //ie { "keys": [&q ...

  4. Python 编程环境搭建(Windows 系统中)

    由于大家普遍使用 Windows 系统,所以本文只介绍 Windows 系统中 Python 环境的安装. 在 Windows 中安装 Python 与安装普通软件没什么差别,下载所需版本的安装包后, ...

  5. F - Select Half dp

    题目大意:从n个数里边选n/2个数,问和最大是多少. 题解:这是一个比较有意思的DP,定义状态dp[i][1],表示选了第i个数的最优状态,dp[i][0]表示没有选第i个数的最优状态. 状态是如何转 ...

  6. Labyrinth 树的直径加DFS

    The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is d ...

  7. java文件上传、下载、图片预览

    多文件保存到本地: @ResponseBody    @RequestMapping(value = "/uploadApp",produces = { "applica ...

  8. 永恒之蓝MS17010复现

    MS17010复现 靶机win7:192.168.41.150 攻击kali:   192.168.41.147 扫描 通过auxiliary/scanner/smb/smb_ms17_010模块扫描 ...

  9. Centos8安装Docker提示:package docker-ce-3:19.03.8-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but none of the providers can be installed

    Centos8安装Docker提示:package docker-ce-3:19.03.8-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but ...

  10. 引导 ARM Linux

    引导 ARM Linux 本文翻译自:https://www.kernel.org/doc/html/latest/arm/booting.html 引导 ARM Linux 需要一个引导加载程序,它 ...