【Hihocoder1636】Pangu and Stones(区间DP)
题意:N堆石子,每次可以合并连续的长度从L到R的若干堆石子为1堆,费用为选择的石子总个数,求将N堆合并成1堆的最小总花费,无解输出0
思路:dp[i][j][k]表示将i到j这段区间合并为k堆的最小代价
\[ 初始条件 dp[i][j][j-i+1]=0 \]
\[ dp[i][j][k]=min(dp[i][x][y-1]+dp[x+1][j][1]+s[j]-s[i-1] (k=1,i<=x<=j-1,L<=y<=R) \]
\[ dp[i][j][k]=min(dp[i][x][k-1]+dp[x+1][j][1] (k>=2,i<=x<=j-1) \]
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
#define N 150
#define M 6100000
#define eps 1e-8
#define pi acos(-1)
#define oo 1e9 ll dp[N][N][N],a[N],s[N]; int main()
{
//freopen("hihocoder1636.in","r",stdin);
//freopen("hihocoder1636.out","w",stdout);
int n,L,R;
while(scanf("%d%d%d",&n,&L,&R)!=EOF)
{
s[]=;
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
s[i]=s[i-]+a[i];
}
memset(dp,0x3f,sizeof(dp));
for(int i=;i<=n;i++)
for(int j=i;j<=n;j++) dp[i][j][j-i+]=;
for(int len=;len<=n;len++)
for(int i=;i<=n-len+;i++)
{
int j=i+len-;
for(int x=i;x<=j-;x++)
for(int y=L;y<=R;y++)
dp[i][j][]=min(dp[i][j][],dp[i][x][y-]+dp[x+][j][]+s[j]-s[i-]);
for(int k=;k<=len;k++)
for(int x=i;x<=j-;x++)
dp[i][j][k]=min(dp[i][j][k],dp[i][x][k-]+dp[x+][j][]);
}
if(dp[][n][]>oo) printf("0\n");
else printf("%lld\n",dp[][n][]);
}
return ;
}
【Hihocoder1636】Pangu and Stones(区间DP)的更多相关文章
- icpc 2017北京 J题 Pangu and Stones 区间DP
#1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...
- hihocoder 1636 : Pangu and Stones(区间dp)
Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the first livi ...
- 2017北京网络赛 J Pangu and Stones 区间DP(石子归并)
#1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...
- hihocoder1636 Pangu and Stones
思路: 区间dp.dp[l][r][k]表示把区间[l, r]的石子合并成k堆所需要的最小代价. 实现: #include <iostream> #include <cstring& ...
- hihocoder1636 Pangu and Stones(区间DP(石子合并变形))
题目链接:http://hihocoder.com/problemset/problem/1636 题目大意:有n堆石头,每次只能合并l~r堆,每次合并的花费是要合并的石子的重量,问你合并n堆石子的最 ...
- 区间DP小结
也写了好几天的区间DP了,这里稍微总结一下(感觉还是不怎么会啊!). 但是多多少少也有了点感悟: 一.在有了一点思路之后,一定要先确定好dp数组的含义,不要模糊不清地就去写状态转移方程. 二.还么想好 ...
- Pangu and Stones(HihoCoder-1636)(17北京OL)【区间DP】
题意:有n堆石头,盘古每次可以选择连续的x堆合并,所需时间为x堆石头的数量之和,x∈[l,r],现在要求,能否将石头合并成一堆,如果能,最短时间是多少. 思路:(参考了ACM算法日常)DP[i][j] ...
- Pangu and Stones HihoCoder - 1636 区间DP
Pangu and Stones HihoCoder - 1636 题意 给你\(n\)堆石子,每次只能合成\(x\)堆石子\((x\in[L, R])\),问把所有石子合成一堆的最小花费. 思路 和 ...
- [ICPC 北京 2017 J题]HihoCoder 1636 Pangu and Stones
#1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...
- hihoCoder 1636 Pangu and Stones
hihoCoder 1636 Pangu and Stones 思路:区间dp. 状态:dp[i][j][k]表示i到j区间合并成k堆石子所需的最小花费. 初始状态:dp[i][j][j-i+1]=0 ...
随机推荐
- 在无TNS配置时,登录到数据库。
sqlplus user/pw@ip:port/servicename sqlplus user/pwd@tnsname sqlplus user/pwd---aix sqlplus /nolog&g ...
- 【转】如何在VC下检测当前存在的串口及串口热拔插
当我们在用VS进行串口编程时,在打开串口前,经常想知道当前PC上存在多少个串口,哪些串口可用?哪些串口已经打开了,最好是在一个Combo Box中列表系统当前所有可用的串口以供选择,然而如何获取系统当 ...
- python入门:最基本的用户登录
#! usr/bin/env python # -*- coding: utf-8 -*- #最基本的用户登录 import getpass usre = input("username:& ...
- 【Shiro】调用doGetAuthenticationInfo进行认证成功之后,isAuthenticated是false的问题。
使用@Configuration配置shiro无状态登录时出现的问题,在subject.login之后当前线程重新绑定了一个假定subject,isAuthenticated. 这里自定义的访问拦截器 ...
- destoon手机端mobileurl函数增加城市分类参数
mobileurl函数在include/global.func.php 858行 共四个参数,moduleid-模型id,catid-分类id,itemid -文章id,page-页码 functio ...
- apply(), applymap(), map()
Pandas 中map, applymap and apply的区别 https://blog.csdn.net/u010814042/article/details/76401133/ Panda ...
- 剑指Offer(书):不用四则运算做加法
题目:写一个函数,求两个整数之和,不得使用四则运算位运算. package com.gjjun.jzoffer; /** * 写一个函数,求两个整数之和,不得使用四则运算 * * @author gj ...
- JAVA基础篇—Servlet小结
一.get请求和post请求的区别: 1.get请求是通过url传递参数,post请求是通过请求体传递参数的 2.get请求最多允许传递255个字符,对长度有限制,所以数据比较大的时候我们使用post ...
- Persona5
65536K Persona5 is a famous video game. In the game, you are going to build relationship with your ...
- nmap命令扫描存活主机
1.ping扫描:扫描192.168.0.0/24网段上有哪些主机是存活的: [root@laolinux ~]# nmap -sP 192.168.0.0/24 Starting Nmap 4. ...