P1052 river [the DP]

Title Description

There is a single-plank bridge on the river, a frog want to jump from one side of the river along the difficult path. There are some stones on the bridge, frogs hate to step on the stones. The bridge length and a skip distance of frogs are positive integers, we can put in single frog point may arrive as a string of integer points on the number line: 0,1, ..., L0,1, ..., L (wherein L L is the length of the bridge). Coordinates 00 indicates the starting point of the bridge, coordinates L L dot indicates the end of the bridge. Frog from the starting point of the bridge, kept jumping to the finish direction. A jump distance S S to T T any positive integer (including S, T S , T ). When the frog jump or skip coordinates L L at the point, even out of the frog has a difficult path.

Title given in single length L L , frog jump distance S, T S , T , stone bridge position. Your task is to determine the frog in order to cross the river, the minimum required number of stepping stones.

Resolve

Normal, this is a simple enough dp, state definition is to reach the minimum number of stones to step on each point of transfer is the former \ (s \ sim t \) state of the step transfer over.

However, a large range of data that problem, then use path compression techniques.

Clearly, if we take (1 \ sim10 \) \ least common multiple, distance between two points if this is the least common multiple, then no matter \ (s, t \) Why, you can skip this one point from the last point. Thus, we encounter every two points a distance greater than the least common multiple of this distance will be the least common multiple of the modulus.

Reference Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 350000
#define E 1e-12
#define lcm 2520
using namespace std;
int dp[N],a[N],d[N],l,s,t,m;
bool v[N];
int main()
{
    scanf("%d%d%d%d",&l,&s,&t,&m);
    for(int i=1;i<=m;++i) scanf("%d",&a[i]);
    sort(a+1,a+m+1);
    for(int i=1;i<=m;++i) d[i]=(a[i]-a[i-1])%lcm;
    for(int i=1;i<=m;++i){
        a[i]=a[i-1]+d[i];
        v[a[i]]=1;
    }
    l=a[m];
    fill(dp+1,dp+N,m);
    for(int i=0;i<=l+t;++i)
        for(int j=s;j<=t;++j){
            if(i-j>=0) dp[i]=min(dp[i],dp[i-j]);
            dp[i]+=v[i];
        }
    int ans=INF;
    for(int i=l;i<=l+t;++i)
        ans=min(ans,dp[i]);
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/DarkValkyrie/p/11388109.html