【P1052 过河】dp

P1052
做法 很容易想到 dp[i] = min(dp[i-j]+num[i],dp[i]), S<=j <= T;
但是 L 有 1e9 dp存不下
但是我们可以发现石头只有100个 意思是说石头之间距离如果很大 那么就会直接传递下去 不可能在中间吃石头
问题是怎么距离如何确定呢?
我们知道 p 和 p - 1 是互质的
那么 gcd(p,p-1) = 1
px + (p-1)y = 1 是有整数解的
p
x + (p-1)y = dis 有整数解
两个值都为正需要的条件是 dis>(p
(p-1))-1
那么只要距离大于等于 (p
(p-1))的我们给他压缩成 10*(10-1) = 90(因为最长一步是10
然后开始跑dp
注意最后要从 p + 9 开始 因为题目说可能跳过去
p+9 代表你去跳过去的地方找
初始化dp[0] = 0 其他都设正无穷

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define lc (rt<<1)
#define rc (rt<<11)
#define mid ((l+r)>>1)

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
const int MAX_N = 20025;
int dp[MAX_N],dis[150],arr[150],num[MAX_N];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    ll L;int S,T,n,ans = 0x3f3f3f3f,p;scanf("%lld%d%d%d",&L,&S,&T,&n);
    for(int i = 1;i<=n;++i) scanf("%d",&arr[i]);
    if(S==T)
    {
        int ans = 0;
        for(int i = 1;i<=n;++i) if(arr[i]%S==0) ans++;
        printf("%d\n",ans);
        return 0;
    }
    sort(arr+1,arr+1+n);
    dis[n+1] = min(L-arr[n],1ll*100);
    p = 0;arr[0] = 0;
    for(int i = 1;i<=n;++i)
    {
        dis[i] = min(90,arr[i]-arr[i-1]);
        p+=dis[i];num[p]++;
    }
    p+=dis[n+1];
    memset(dp,0x3f,sizeof(dp));
    dp[0] = 0;
    for(int i = 1;i<=p+9;++i)
    {
        for(int j = S;j<=T;++j) if(i>=j) dp[i] = min(dp[i],dp[i-j]+num[i]);
    }
    for(int i = p;i<=p+9;i++)
        ans = min(ans,dp[i]);
    printf("%d\n",ans);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/heucodesong/article/details/89329199