hdu 4412 Sky Soldiers(区间DP)
Sky Soldiers
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 559 Accepted Submission(s): 181
of the army can analysis the probability of landing in a certain place through landing history records. To make it simple, the statistician suggests that these sky soldiers will land on finite discrete points of a straight line.
This mission plans to place m provisions for the soldiers on the line for landing. These soldiers will be informed the direction of the nearest provision point by a special device after landing, and then walk to the point. The manager of this mission is asking
you for help: to determine m points for provisions so that the expected sum of walking distance should be minimized. You can put provisions on any point of the landing line.
The following k lines contain descriptions of landing parameters for the soldiers numbered from 1 to k. Each description consists of an integer L followed by L pairs of (x, p), which indicates that the probability of the soldier's landing on integer coordination
x is p. It is guaranteed that all the p values are positive real numbers, and the sum of p in a single line is exactly 1. The same x may appear more than once on the same line which you should simply add up all the probability p of the pairs with equal x.
The number of places on which all the soldiers could land is no more than 1000 and it can not be less than m.
The input ends with k=m=0.
2 1
2 0 0.5 1 0.5
2 1 0.1 3 0.9
0 0
2.30
题意:
n个伞兵。落地后。每一个伞兵可能会落在若干个点上点都在x轴上。落在每一个点都有一个概率。如今在x轴上建立m个基地,每一个伞兵走到近期的基地。确定基地建立的地点使得全部伞兵所走的路程总和的期望最小。
思路:
乍一看像期望dp。细致思考后能够发现这是一个区间DP。如果一个伞兵落在x点。那么他走的路程的期望为p1*|x1-x|+p2*|x2-x|....*pm*|xm-x|。所以我们能够把n个伞兵等价成一个伞兵。
然后它到一个点的概率为全部伞兵到那点的概率总和。那如今就能够写出状态了。dp[i][j]表示在前i个位置建j个基地。
该等效伞兵走的路程的最小期望。那么这题就类似poj 1160
Post Office那题了。转移方程为dp[i][j]=dp[k][j-1]+cost[k+1][i]。k<i。
cost[i][j]表示在i,j之间建一个基地且该基地负责集合[i,j]上的伞兵。
所走距离的期望。如今重点怎么高速算cost[i][j]了。考虑我们在算cost[j][i]的时候。随着j的减小基地的最优位置cur要么前移要么不变。所以我们就能够在O(n^2)的时间复杂度下算出了。
具体见代码:
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1010;
typedef long long ll;
struct node
{
int x;
double p;
} po[maxn];
map<int,double> mp;
map<int,double>::iterator it;
int n,m;
double dp[maxn][55],cost[maxn][maxn];
int main()
{
int k,i,j,l,x,cur,dis;
double p,lp,rp,cl,cr,tp; while(scanf("%d%d",&k,&m),k||m)
{
mp.clear();
for(i=0;i<k;i++)
{
scanf("%d",&l);
while(l--)
{
scanf("%d%lf",&x,&p);
mp[x]+=p;
}
}
n=0;
for(it=mp.begin();it!=mp.end();it++)
{
po[++n].x=it->first;
po[n].p=it->second;
}
for(i=n;i>=1;i--)
{
cost[i][i]=0;
cur=i;
rp=po[i].p;
lp=0;
cl=cr=0;
for(j=i-1;j>=1;j--)
{
dis=po[cur].x-po[j].x;
cl+=dis*po[j].p;//重心位置左边的期望和
lp+=po[j].p;//重心位置左边的概率和cr,rp为重心位置右边相应值
tp=cl+cr;//总期望
while(cur>1&&rp-lp<0)
{
dis=po[cur].x-po[cur-1].x;
cr+=dis*rp;
cl-=dis*lp;
cur--;
rp+=po[cur].p;
lp-=po[cur].p;
tp=cl+cr;
}
cost[j][i]=tp;
//printf("%d->%d tp %lf\n",j,i,tp);
}
}
for(i=0;i<=m;i++)
dp[i][i]=0;
for(i=1;i<=n;i++)
dp[i][0]=1e15;
for(j=1;j<=m;j++)
{
for(i=j;i<=n;i++)
{
tp=1e15;
for(k=j-1;k<i;k++)
tp=min(tp,dp[k][j-1]+cost[k+1][i]);
dp[i][j]=tp;
}
}
printf("%.2lf\n",dp[n][m]);
}
return 0;
}
hdu 4412 Sky Soldiers(区间DP)的更多相关文章
- hdu 4412 Sky Soldiers DP
动态规划,主要是用单调性求区间的最小期望. 代码如下: #include<iostream> #include<stdio.h> #include<algorithm&g ...
- HDU 5115 Dire Wolf 区间dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5115 Dire Wolf Time Limit: 5000/5000 MS (Java/Others ...
- HDU 5693 D Game 区间dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5693 题解: 一种朴实的想法是枚举选择可以删除的两个或三个数(其他的大于三的数都能凑成2和3的和), ...
- hdu 4597 Play Game 区间dp
Play Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=459 ...
- 【HDOJ】4412 Sky Soldiers
1. 题目描述有$k$个伞兵跳伞,有$m$个汇点.当伞兵着陆后,需要走向离他最近的汇点.如何选择这$m$个结点,可以使得士兵最终行走的距离的期望最小.求这个最小的期望. 2. 基本思路假设已经选好了这 ...
- hdu 6049---Sdjpx Is Happy(区间DP+枚举)
题目链接 Problem Description Sdjpx is a powful man,he controls a big country.There are n soldiers number ...
- hdu 5693 && LightOj 1422 区间DP
hdu 5693 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5693 等差数列当划分细了后只用比较2个或者3个数就可以了,因为大于3的数都可以由2和3 ...
- hdu 4745 Two Rabbits 区间DP
http://acm.hdu.edu.cn/showproblem.php?pid=4745 题意: 有两只兔子Tom Jerry, 他们在一个用石头围城的环形的路上跳, Tom只能顺时针跳,Jerr ...
- hdu 5181 numbers——思路+区间DP
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5181 题解:https://www.cnblogs.com/Miracevin/p/10960717.ht ...
随机推荐
- 阿里巴巴分布式服务框架 Dubbo
1.Dubbo是阿里巴巴内部的SOA服务化治理方案的核心框架,每天为2000+ 个服务提供3,000,000,000+ 次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点.Dubbo自2011年开源 ...
- AES算法工具类
什么是对称加密算法? AES已经变成目前对称加密中最流行算法之一:AES可以使用128.192.和256位密钥,并且用128位分组加密和解密数据. 对称加密算法安全吗? 看过间谍局的知友们一定知道电台 ...
- CenoOS下如何对mysql编码进行配置
1 修改/etc/mysql/my.cnf配置文件 增加default-character-set=utf8 配置文件如下 [client] port = 3306 socket = /var/run ...
- 混沌数学之Chua's circuit(蔡氏电路)
蔡氏电路(英语:Chua's circuit),一种简单的非线性电子电路设计,它可以表现出标准的混沌理论行为.在1983年,由蔡少棠教授发表,当时他正在日本早稻田大学担任访问学者[1].这个电路的制作 ...
- C++类模板的声明和定义为什么要放在同一个文件
不是只能放在.h里面,但是推荐放在.h里面.STL模板实现全部是放在.h里面的.------------------编译能通过.1)参与编译的只是.cpp文件,不会报错的原因,是因为它能在.h里面找到 ...
- Horspool 字符串匹配算法
Horspool 字符串匹配算法对Boyer-Moore算法的简化算法. Horspool 算法是一种基于后缀匹配的方法,是一种“跳跃式”匹配算法,具有sub-linear亚线性时间复杂度. Hors ...
- oralce sql 分页
create table student ( sid varchar2(10), --学号 sname varchar2(10), --姓名 classid varchar2(10), --班级号 s ...
- Android中Intent的显示和隐式使用
Android应用程序中组件之间的通信都少不了Intent的使用,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件, ...
- Restful安全认证及权限的解决方案
一.Restful安全认证常用方式 1.Session+Cookie 传统的Web认证方式.需要解决会话共享及跨域请求的问题. 2.JWT JSON Web Token. 3.OAuth 支持两方和三 ...
- 数据库操作语句类型(DQL、DML、DDL、DCL)简介
SQL语言共分为四大类:数据查询语言DQL,数据操纵语言DML,数据定义语言DDL,数据控制语言DCL. 1. 数据查询语言DQL数据查询语言DQL基本结构是由SELECT子句,FROM子句,WHER ...