Codeforces 489E Hiking
来源:互联网 发布:java serverlet 范例 编辑:程序博客网 时间:2024/05/27 19:26
A traveler is planning a water hike along the river. He noted the suitable rest points for the night and wrote out their distances from the starting point. Each of these locations is further characterized by its picturesqueness, so for the i-th rest point the distance from the start equals xi, and its picturesqueness equals bi. The traveler will move down the river in one direction, we can assume that he will start from point 0 on the coordinate axis and rest points are points with coordinates xi.
Every day the traveler wants to cover the distance l. In practice, it turns out that this is not always possible, because he needs to end each day at one of the resting points. In addition, the traveler is choosing between two desires: cover distance l every day and visit the most picturesque places.
Let's assume that if the traveler covers distance rj in a day, then he feels frustration , and his total frustration over the hike is calculated as the total frustration on all days.
Help him plan the route so as to minimize the relative total frustration: the total frustration divided by the total picturesqueness of all the rest points he used.
The traveler's path must end in the farthest rest point.
The first line of the input contains integers n, l (1 ≤ n ≤ 1000, 1 ≤ l ≤ 105) — the number of rest points and the optimal length of one day path.
Then n lines follow, each line describes one rest point as a pair of integers xi, bi (1 ≤ xi, bi ≤ 106). No two rest points have the same xi, the lines are given in the order of strictly increasing xi.
Print the traveler's path as a sequence of the numbers of the resting points he used in the order he used them. Number the points from 1 to n in the order of increasing xi. The last printed number must be equal to n.
5 910 1020 1030 131 540 10
1 2 4 5
In the sample test the minimum value of relative total frustration approximately equals 0.097549. This value can be calculated as .
就是选择一个子序列,使得存在最优比率。这种题目是在cf第一次遇上,还是很有意思的。
方法:二分套dp。如果我们二分出最优比率,那么跑一个dp:
if(dp[j]+dis[j][i]-m*b[i]<dp[i])dp[i]=dp[j]+dis[j][i]-m*b[i];即可。
#include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<vector>using namespace std;const int maxn=1111;double dis[maxn][maxn];double dp[maxn];const double inf=1e9;const double eps=1e-9;int pre[maxn];double b[maxn];double a[maxn];int n;double l;vector<int >p;bool check(double m){ for(int i=1;i<=n;i++) { dp[i]=inf; for(int j=0;j<i;j++) { if(dp[j]+dis[j][i]-m*b[i]<dp[i])dp[i]=dp[j]+dis[j][i]-m*b[i],pre[i]=j; } // printf("%d %lf\n",i,dp[i]); } return dp[n]<=0;}int main(){ cin>>n>>l; for(int i=1;i<=n;i++) { scanf("%lf%lf",&a[i],&b[i]); for(int j=0;j<i;j++) dis[j][i]=sqrt(fabs(a[i]-a[j]-l)); } double l=0,r=inf; while(l+eps<=r) { double m=(l+r)/2; if(check(m))r=m; else l=m; }// printf("%lf\n",l); // for(int i=0;i<=n;i++)printf("%d\n",pre[i]); int now=n; while(now>0) { p.push_back(now); now=pre[now]; } for(int i=p.size()-1;i>=0;i--)printf("%d ",p[i]); return 0;}
- 【Codeforces】 489E Hiking
- CodeForces 489E Hiking
- Codeforces 489E Hiking
- CodeForces 489E Hiking
- codeforces 489e Hiking dp+01分数规划+二分
- codeforces 277.5 E.Hiking 01分数规划
- Codeforces 489E Hiking(普通01分数规划加DP)
- Codeforces Round #277.5 (Div. 2) E. Hiking(二分 DP)
- Codeforces Round #277.5(Div. 2)E. Hiking【Dp+01分数规划】
- Hiking
- codeforces 163E e-Government
- 【Codeforces 163E】E-Government
- Codeforces 78E Evacuation
- 【dp】codeforces 83E
- Codeforces 124 E
- Codeforces 231E
- Codeforces 231E - Cactus
- Codeforces #163 Div2 E
- MeasureSpec&OpenGL&SQLite——tips
- response乱码的问题及setCharacterEncoding()与setContentType()区别
- Java 垃圾回收机制整理
- Play框架搭建
- MooseFS学习笔记
- Codeforces 489E Hiking
- java中的lock和synchronized区别
- iOS多线程之三 GCD
- iOS开发学习零碎笔记
- (四)Spark源码理解之BlockManager---part3
- HttpClient学习笔记(1)
- a20隐藏底部按钮及隐藏状态栏和虚拟按键栏
- Oracle 时间差计算
- UVa 10815 Andy's First Dictionary