Codeforces 489E Hiking

E. Hiking
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

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.

Sample test(s)
input
5 9
10 10
20 10
30 1
31 5
40 10
output
1 2 4 5 
Note

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值