P1594 Guard

Title Description

Convoy to a team in a one-way street in the front row, on the Fore River Bridge is a single line. Because the streets are one-way street, so can not overtake any vehicle. Bridge can withstand a given maximum load. In order to control traffic, the bridge on both sides of a bridge stations commander. Convoy are divided into several groups by the vehicle can simultaneously bridge. When a group of the team reached the other side of the bridge, the commander of the end of the commanders on the other end of the notification by telephone, so that the next group of teams can begin by the bridge. By weight of each vehicle it is known. Any group of team weight can not exceed the maximum weight capacity of the bridge. Each vehicle was in the same group are its fastest speed through the bridge. Bridge group by a team is the team time of the slowest vehicle is represented by the time required for the bridge. The minimum time required to calculate the issue value of all the convoys required by the bridge.

Input Format

The first line of the input file contains three positive integers (separated by spaces), the first integer represents the bridge can withstand the maximum load (represented by t); second integer bridge length (expressed in kilometers ); Total third integer (n <1000) of the vehicle in the convoy. The next few lines, each line contains two positive integers W and S (separated by spaces), W represents the weight of the vehicle (represented by t), S represents a bridge car can reach the fastest speed (with km / h representation). And the weight of the car speed when the car is in the order given waiting.

Output Format

The output file should be a real number, a rounded decimal place, representing the entire convoy (expressed in minutes) by the minimum time required bridge.

Sample input and output

Input # 1
100 5 10
40 25
50 20
50 20
70 10
12 50
9 70
49 30
38 25
27 50
19 70
Output # 1
75.0

This question is the use of dynamic programming ideas. With f [i] represents the minimum time before by the car i; i represents the minimum speed of the vehicle to the second vehicle with Vmin j [i] [j]; i represents the weight of the car with the front sum [i]. Normalizing action following formula:

1. The initial value: f [i] = (double) len / v [i] + f [i-1];

2. If the first vehicle to the i-j vehicle by simultaneously:

f[i]=min(f[i],f[j-1]+(double)len/vmin[j][i]);

Score: 100

Time complexity: O (N ^ 2)

Complexity Space: O (3 * N + N ^ 2)

 

#include <iostream>
#include <cstdio>
using namespace std;
int N, len, mw;
long long W[1002], sum[1002];
double f[1002], lg2[1002]={-1}, S[1002][10];

inline int read(){
    int s=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-'){
            w=-1;
        }
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        s=s*10+ch-'0';
        ch=getchar();
    }
    return s*w;
}


double Query(int l, int r){
    int lg=lg2[r-l+1];
    return max(S[l][lg], S[r-(1<<lg)+1][lg]);
}

int main(){
    mw=read();
    len=read();
    N=read();
    for (int i=1; i<=N; i++) lg2[i]=lg2[i>>1]+1;
    for(int i=1; i<=N; i++){
        W[i]=read();
        S[i][0]=read();
        f[i]=S[i][0]=(double)len/S[i][0];
        sum[i]=W[i]+sum[i-1];
    }
    for (int j=1; j<=lg2[N]; j++)
        for (int i=1; i+(1<<j)-1<=N; i++)
            S[i][j]=max(S[i][j-1], S[i+(1<<(j-1))][j-1]);
    for(int i=1; i<=N; i++){
        f[i]+=f[i-1];
        for(int j=i-1; j>=1; j--){
            if (sum[i]-sum[j-1]<=mw){
                f[i]=min(f[i], f[j-1]+Query(j, i));
            }
            else break;
        }
    }
    printf("%.1lf", f[N]*60); 
    return 0;
}

 



Guess you like

Origin www.cnblogs.com/hrj1/p/11749969.html