Codeforces Round #277.5 (Div. 2)E题

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
题目大意:一个人要沿着一条河旅行,他每次走的距离都有疲劳值(太近不开心,太远不开心)。有几个休息的地方坐标为xi(起点是0)和一个风景值bi,每次的疲劳值是上次的休息点xi和下一次到的休息点xj和一个他满意的距离L,疲劳值会累加,旅行完的疲劳值为到每个休息过的点
∑sqrt(abs(xj-xi-L))/∑bi,求使旅行完的总疲劳值最小的休息点路径。
题目分析:乍一看以为是DP,然后傻乎乎的去写。。。然后才发现直接以上一地点到下一地点为状态会有后效性。。。。姿势:要求最后答案最小化,用二分答案, 当二分找到最小的ans使ans<∑sqrt(abs(xi-xj-L))/∑bi,即∑sqrt(abs(xi-xj-L))-ans*∑bi>0成立时最大的ans。这样我们就可以设f[i]表示到达第i个点时的∑sqrt(abs(xi-xj-L))-ans*∑bi最小值(显然ans是从最小的序列得出来的所以对最小值一定成立,如果不懂这里的话可以自己列一下这里的递推式,你会发现这里很奇妙的消除了原来的后效性O(n*n))。然后记录下递推的编号就可以了。再逆推回去输出答案。

#include
#include
#include
using namespace std;
const int INF = (1<<31)-10;
int n,ll,b[1010],x[1010],pre[1010],out[1010];
double f[1010];
bool check(double ans)
{
    int i,j,k;
    bool result=0;
    f[0]=0.0;
    for (i=1;i<=n;i++) { ///DP计算ans是否成立。
        f[i]=INF;
        for (j=i-1;j>=0;j--) {
            if (f[j]+sqrt(abs(x[i]-x[j]-ll))-b[i]*ans0.000000000001) {  ///二分答案,注意精度问题!
        if (check(ans)) {
            r=ans;ans=(l+r)/2.0;
        } else {
            l=ans;ans=(l+r)/2.0;
        }
    }
    k=0;w=n;///最后那次一定是成立的。
    while (w>0) {
        k++;out[k]=w;
        w=pre[w];
    }
    for (i=k;i>1;i--) printf("%d ", out[i]);
    printf("%d\n", out[1]);
    return 0;
}

你可能感兴趣的:(其他,二分)