Poj Making the Grade【dp】
2016-04-01 11:53
363 查看
Making the Grade
Description
A straight dirt road connects two fields on FJ's farm, but it changes elevation more than FJ would like. His cows do not mind climbing up or down a single slope, but they are not fond of an alternating succession of hills and valleys. FJ would like to add
and remove dirt from the road so that it becomes one monotonic slope (either sloping up or down).
You are given N integers A1, ... , AN (1 ≤ N ≤ 2,000) describing the elevation (0 ≤ Ai ≤ 1,000,000,000) at each of N equally-spaced positions along the road, starting
at the first field and ending at the other. FJ would like to adjust these elevations to a new sequence B1, . ... , BN that is either nonincreasing or nondecreasing. Since it costs the same amount of money to add or remove
dirt at any position along the road, the total cost of modifying the road is
|A1 - B1| + |A2 - B2| + ... + |AN - BN |
Please compute the minimum cost of grading his road so it becomes a continuous slope. FJ happily informs you that signed 32-bit integers can certainly be used to compute the answer.
Input
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains a single integer elevation: Ai
Output
* Line 1: A single integer that is the minimum cost for FJ to grade his dirt road so it becomes nonincreasing or nondecreasing in elevation.
Sample Input
Sample Output
Source
USACO 2008 February Gold
题意:
给出一个数字串,让你修改某些位置的值,使其变成单调函数(不要求严格单调),且规定修改的消耗是 修改前的值减去修改后的值的绝对值,问最小最小消耗是多少。
题解:
dp 问题确实不会做,参考的大神的代码
膜拜大神
动态规划,最重要的是找到动态转移方程,也就是如何决策才能一步步的实现全局最优解
感觉自己理解的还太少,智商被碾压了....
本题的做法大概是
时间和空间都是 n*n 复杂度的算法:
dp[i][j] 表示 把第 i 个元素修改成第 j 小的元素的最小消耗
需要一个数组进行离散化,实现元素和数值的对应,排序之后,第y[j] 就是第 j 小的数
/* http://blog.csdn.net/liuke19950717 */
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll ABS(ll x)
{
return x>0?x:-x;
}
ll x[2005],y[2005],dp[2005][2005];
ll slove(ll n)
{
memset(dp,0,sizeof(dp));
sort(y+1,y+n+1);
for(ll i=1;i<=n;++i)
{
ll tp=dp[i-1][1];
for(ll j=1;j<=n;++j)
{
tp=min(tp,dp[i-1][j]);
//ll m=abs((ll)x[i]-y[j]);
ll m=ABS(x[i]-y[j]);
dp[i][j]=tp+m;
}
}
ll ans=dp
[1];
for(ll i=1;i<=n;++i)
{
ans=min(ans,dp
[i]);
}
return ans;
}
int main()
{
ll n;
//freopen("shuju.txt","r",stdin);
while(~scanf("%I64d",&n))
{
for(ll i=1;i<=n;++i)
{
scanf("%I64d",&x[i]);
y[i]=x[i];
}
printf("%I64d\n",slove(n));
}
return 0;
}
空间优化到 n 的算法:
dp[i] 表示把整个序列修改成有序的且最后一个值是x[i] 需要的最少花费(也许理解有误,求指导)
/* http://blog.csdn.net/liuke19950717 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
using namespace std;
int x[2005],y[2005],dp[2005];
int slove(int n)
{
sort(y,y+n);
memset(dp,0,sizeof(dp));
for(int i=0;i<n;++i)
{
int tp=dp[0];
for(int j=0;j<n;++j)
{
tp=min(tp,dp[j]);
dp[j]=tp+abs(x[i]-y[j]);
}
}
int ans=dp[1];
for(int i=2;i<n;++i)
{
ans=min(ans,dp[i]);
}
return ans;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;++i)
{
scanf("%d",&x[i]);
y[i]=x[i];
}
printf("%d\n",slove(n));
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5656 | Accepted: 2642 |
A straight dirt road connects two fields on FJ's farm, but it changes elevation more than FJ would like. His cows do not mind climbing up or down a single slope, but they are not fond of an alternating succession of hills and valleys. FJ would like to add
and remove dirt from the road so that it becomes one monotonic slope (either sloping up or down).
You are given N integers A1, ... , AN (1 ≤ N ≤ 2,000) describing the elevation (0 ≤ Ai ≤ 1,000,000,000) at each of N equally-spaced positions along the road, starting
at the first field and ending at the other. FJ would like to adjust these elevations to a new sequence B1, . ... , BN that is either nonincreasing or nondecreasing. Since it costs the same amount of money to add or remove
dirt at any position along the road, the total cost of modifying the road is
|A1 - B1| + |A2 - B2| + ... + |AN - BN |
Please compute the minimum cost of grading his road so it becomes a continuous slope. FJ happily informs you that signed 32-bit integers can certainly be used to compute the answer.
Input
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains a single integer elevation: Ai
Output
* Line 1: A single integer that is the minimum cost for FJ to grade his dirt road so it becomes nonincreasing or nondecreasing in elevation.
Sample Input
7 1 3 2 4 5 3 9
Sample Output
3
Source
USACO 2008 February Gold
题意:
给出一个数字串,让你修改某些位置的值,使其变成单调函数(不要求严格单调),且规定修改的消耗是 修改前的值减去修改后的值的绝对值,问最小最小消耗是多少。
题解:
dp 问题确实不会做,参考的大神的代码
膜拜大神
动态规划,最重要的是找到动态转移方程,也就是如何决策才能一步步的实现全局最优解
感觉自己理解的还太少,智商被碾压了....
本题的做法大概是
时间和空间都是 n*n 复杂度的算法:
dp[i][j] 表示 把第 i 个元素修改成第 j 小的元素的最小消耗
需要一个数组进行离散化,实现元素和数值的对应,排序之后,第y[j] 就是第 j 小的数
/* http://blog.csdn.net/liuke19950717 */
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll ABS(ll x)
{
return x>0?x:-x;
}
ll x[2005],y[2005],dp[2005][2005];
ll slove(ll n)
{
memset(dp,0,sizeof(dp));
sort(y+1,y+n+1);
for(ll i=1;i<=n;++i)
{
ll tp=dp[i-1][1];
for(ll j=1;j<=n;++j)
{
tp=min(tp,dp[i-1][j]);
//ll m=abs((ll)x[i]-y[j]);
ll m=ABS(x[i]-y[j]);
dp[i][j]=tp+m;
}
}
ll ans=dp
[1];
for(ll i=1;i<=n;++i)
{
ans=min(ans,dp
[i]);
}
return ans;
}
int main()
{
ll n;
//freopen("shuju.txt","r",stdin);
while(~scanf("%I64d",&n))
{
for(ll i=1;i<=n;++i)
{
scanf("%I64d",&x[i]);
y[i]=x[i];
}
printf("%I64d\n",slove(n));
}
return 0;
}
空间优化到 n 的算法:
dp[i] 表示把整个序列修改成有序的且最后一个值是x[i] 需要的最少花费(也许理解有误,求指导)
/* http://blog.csdn.net/liuke19950717 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
using namespace std;
int x[2005],y[2005],dp[2005];
int slove(int n)
{
sort(y,y+n);
memset(dp,0,sizeof(dp));
for(int i=0;i<n;++i)
{
int tp=dp[0];
for(int j=0;j<n;++j)
{
tp=min(tp,dp[j]);
dp[j]=tp+abs(x[i]-y[j]);
}
}
int ans=dp[1];
for(int i=2;i<n;++i)
{
ans=min(ans,dp[i]);
}
return ans;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;++i)
{
scanf("%d",&x[i]);
y[i]=x[i];
}
printf("%d\n",slove(n));
}
return 0;
}
相关文章推荐
- C/C++中的移位运算
- leetcode——160——Intersection of Two Linked Lists
- 简单斐波那契数列
- java 数据类型
- 集合类(Collection) List/Set/Map... 的区别和联系
- Win10系统开机蓝屏且进不了安全模式的解决办法
- Ubuntu下安装arm-linux-gnueabi-xxx编译器
- 总结的一些面试问题-java
- Spark面对OOM问题的解决方法及优化总结
- 数据库范式
- iwpriv工具通过ioctl动态获取相应无线网卡驱动的private_args所有扩展参数
- c语言字符数组与字符串的使用详解
- Eclipse快捷键
- Android develop tricks——整理自国外的一些Blog
- nyoj540(结构体排序)
- 解决为应用程序池 提供服务的进程关闭时间超过了限制
- SQL Server 2008 R2 开启数据库远程连接
- Git学习(二)---- 创建版本库、添加文件
- iOS学习笔记之UITableView之右侧索引
- web sql database 的例子