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. ffmpeg 交叉编译 版本4.0.4

    touch run.sh chmod 755 run.sh ./run.sh run.sh #!/bin/sh ./configure \ --arch=aarch64 \ --cpu=armv8-a ...

  2. 自己总结 :并发队列ConcurrentLinkedQueue、阻塞队列AraayBlockingQueue、阻塞队列LinkedBlockingQueue 区别 和 使用场景总结

    并发队列ConcurrentLinkedQueue.阻塞队列AraayBlockingQueue.阻塞队列LinkedBlockingQueue 区别 和  使用场景总结 分类: Java2013-0 ...

  3. 2019-07-31【机器学习】无监督学习之降维NMF算法 (人脸特征提取)

    代码 from numpy.random import RandomState #加载RandomState用于创建随机种子 import matplotlib.pyplot as plt from ...

  4. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(四)之Operators

    At the lowest level, data in Java is manipulated using operators Using Java Operators An operator ta ...

  5. python的多线程、多进程、协程用代码详解

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:刘早起早起 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...

  6. HPU第一次团队赛

    D. Tom的战力问题 Tom被斯派克揍了TAT.Tom下定决心要战胜斯派克.但是在战胜最强的斯派克之前,Tom要先打败其他的狗.为此,他打算先收集一下信息.现在Tom在了得到了一些关于战斗力的小道消 ...

  7. 12. 前后端联调 + ( proxy代理 ) + ( axios拦截器 ) + ( css Modules模块化方案 ) + ( css-loader ) + ( 非路由组件如何使用history ) + ( bodyParser,cookieParser中间件 ) + ( utility MD5加密库 ) + ( nodemon自动重启node ) + +

    (1) proxy 前端的端口在:localhost:3000后端的端口在:localhost:1234所以要在webpack中配置proxy选项 (proxy是代理的意思) 在package.jso ...

  8. position的用法(top, bottom, left, right 四个定位属性配合进行使用)

    一般情况下 页面元素的定位方式是根据文档流也就是说默认的从上到下,从左到右的方式进行排列的,而将元素从文档流脱离出来显示的方式有两种,一种是 position 定位另一种是float 浮动,这里我们详 ...

  9. C# WCF之用接口创建服务契约、部署及客户端连接

    服务契约描述了暴露给外部的类型(接口或类).服务所支持的操作.使用的消息交换模式和消息的格式.每个WCF服务必须实现至少一个服务契约.使用服务契约必须要引用命名空间System.ServiceMode ...

  10. Java 中正则表达式使用

    正则表达式基本用法: 测试代码: @Test public void test01() { String str = "adsfd##4324"; // 创建正则表达式对象 Pat ...