POJ 3184 DP+剪枝
思路:
先找到每i头奶牛能在的位置 (一段区间) 记为L[i]和R[i]
f[j]表示在位置j取到的最小值 每回在范围内更新一哈
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,d,L,a[100500],f[100500],l[100500],r[100500];
int main(){
scanf("%d%d",&n,&L);
d=L/(n-1);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
l[i]=max(i*d,L-(n-i-1)*(d+1));
r[i]=min((i+1)*(d+1),L-(n-i-1)*d);
}
memset(f,0x3f,sizeof(f));
f[0]=a[0];
for(int i=1;i<n;i++){
for(int j=r[i];j>=l[i];j--){
int temp=0x3ffffff;
if(j-d>=l[i-1]&&j-d<=r[i-1])temp=f[j-d];
if(j-d-1>=l[i-1]&&j-d-1<=r[i-1])temp=min(temp,f[j-d-1]);
f[j]=temp+abs(j-a[i]);
}
}
printf("%d\n",f[L]);
}
POJ 3184 DP+剪枝的更多相关文章
- DP 剪枝
DP其实也是和搜索一样可以有剪枝的,昨晚看到一个超级好的DP剪枝题:(HDU - 5009) N段东东,要染色,每次给一个区间染色需要的花费为 该区间颜色总数的平方. 每一段只能被染一次色.求 最 ...
- [POJ3612] Telephone Wire(暴力dp+剪枝)
[POJ3612] Telephone Wire(暴力dp+剪枝) 题面 有N根电线杆,初始高度为h[i],要给相邻的两根连线.可以选择拔高其中一部分电线杆,把一根电线杆拔高\(\Delta H\)的 ...
- hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)
题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...
- poj 1080 dp如同LCS问题
题目链接:http://poj.org/problem?id=1080 #include<cstdio> #include<cstring> #include<algor ...
- poj 1609 dp
题目链接:http://poj.org/problem?id=1609 #include <cstdio> #include <cstring> #include <io ...
- cf298F:状压dp+剪枝
div2的F题,只想到了一个复杂度略高的dp,T了几次,后来加了剪枝减掉一些无用的状态终于过了.. 题意: 一个n*m的矩阵 (n<=5,m<=20),对格子进行黑白染色,已经给出了每行每 ...
- POJ 1037 DP
题目链接: http://poj.org/problem?id=1037 分析: 很有分量的一道DP题!!! (参考于:http://blog.csdn.net/sj13051180/article/ ...
- Jury Compromise POJ - 1015 dp (标答有误)背包思想
题意:从 n个人里面找到m个人 每个人有两个值 d p 满足在abs(sum(d)-sum(p)) 最小的前提下sum(d)+sum(p)最大 思路:dp[i][j] i个人中 和 ...
- poj 1088(DP+递归)
这题状态方程很容易得到:DP[i][j] = max(DP[i-1][j],DP[i+1][j],DP[i][j-1],DP[i][j+1]) + 1 难点在于边界条件和剪枝,因为这方程的条件是点在m ...
随机推荐
- 链表(list)--c实现
做c的开发有1年多了,期间写过c++,感觉基础不够好,补上去,不丢人.o(^▽^)o to better myself. #include <stdio.h> #include <s ...
- useradd: cannot open /etc/passwd
[root@ftp ~]# useradd -g ftp -s/sbin/nologin liwmuseradd: cannot open /etc/passwd [root@ftp ~]# user ...
- 紫书 习题8-18 UVa 11536 (扫描法)
这道题貌似可以用滑动窗口或者单调栈做, 但是我都没有用到. 这道题要求连续子序列中和乘上最小值最大, 那么我们就可以求出每一个元素, 以它为最小值的的最大区间的值, 然后取max就ok了.那么怎么求呢 ...
- HTTP——学习笔记(6)https
HTTP+加密+认证+完整性保护=HTTPS HTTP是一种新协议吗?: 不是,HTTPS只是HTTP通信接口部分用SSL和TLS协议代替而已 HTTP中,身处应用层的HTTP直接和TCP通信.而在使 ...
- MySql5.7免安装版配置过程(ubuntu16.04)
MySql5.7免安装版配置过程(ubuntu16.04) 原创 2017年02月07日 16:58:24 标签: 1001 编辑 删除 一.安装环境: 操作系统:ubuntu16.04 数据库:my ...
- RvmTranslator for Linux
RvmTranslator for Linuxeryar@163.com RvmTranslator can translate the RVM file exported by AVEVA Plan ...
- 10.29 工作笔记 ndk编译C++,提示找不到头文件(ndk-build error: string: No such file or directory)
ndk编译C++.提示找不到头文件(ndk-build error: string: No such file or directory) 被这个问题弄得愁眉苦脸啊.心想为啥一个string都找不到呢 ...
- poj--1904--King's Quest(scc建图)
King's Quest Time Limit: 15000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit ...
- Word histogram
Here is a program that reads a file and builds a histogram of the words in the file: process_file lo ...
- BZOJ 3931 Dijkstra+网络流
思路: (我能说按照题意模拟么) 用long long inf 要开大--. //By SiriusRen #include <queue> #include <cstdio> ...