[Luogu1052] river [Dynamic Programming]

P1052 river

The content on ppt onto to QAQ

DP readily occur following method
  • F [x] represents the number of stones on the position coordinate x jump requires a minimum of stepped
  • f[x] = min( f[x-j] + stone[x] ), s <= j <= t
  • However, x ^ 9 up to 10, is difficult to calculate
  • But it is easy to find, stones up to 100, often two stones so far apart from, several consecutive points in front of a stone time coordinates are reachable.
A little to prove it:
When s! = T, the presence of p such that s <= p <p + 1 <= t apparently gcd (p, p + 1) = 1
When Q> = p * (p-1) when, p * x + (p + 1) * y = Q must be a non-negative integer solution i.e., when Q is large enough only by walking several p steps and step p + 1, so the length of the walking distance exactly Q
Proof: When Q = p (p-1), so x = p-1, y = 0; Q 1 increases, the x -, y ++; reduced after x 0, y = p-1, at this time Q = p ^ 2-1, then Q continues to increase then let x = p, y = 0; and so on.
 
Then it is not clear that the number of discrete addition of why so many QAQ
#include<bits/stdc++.h>
using namespace std;
#define Max(x,y) (x)>(y)?(x):(y)
#define Min(x,y) (x)>(y)?(y):(x)
#define ll long long
#define rg register
const int N=200+5,M=1e7+5,inf=0x3f3f3f3f,P=99999997;
int l,s,t,n,ans=inf,a[N],d[N],sto[M],f[M];
template <class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}

int main(){
//    freopen("in.txt","r",stdin);
    rd(l),rd(s),rd(t),rd(n);
    for(int i=1;i<=n;++i) rd(a[i]);
    sort(a+1,a+n+1);a[0]=0,a[n+1]=l;
    for(int i=1;i<=n;++i) d[i]=(a[i]-a[i-1])%3000;
    for(int i=1;i<=n;++i) a[i]=a[i-1]+d[i],sto[a[i]]=1;
    memset(f,inf,sizeof(f));
    f[0]=0,l=a[n];
    for(int i=1;i<=l+t;++i){
        for(int j=s;j<=t;++j)
            if(i-j>=0) f[i]=Min(f[i],f[i-j]);
        f[i]+=sto[i];
    }
//    for(int i=l;i<=l+t;++i) printf("%d ",f[i]);
    for(int i=l;i<l+t;++i) ans=Min(ans,f[i]);
    printf("%d",ans);
    return 0;
}
 

 

Guess you like

Origin www.cnblogs.com/lxyyyy/p/11234389.html