/ Vijos / 题库 / 过河 /

题解

333 条题解

  • -1
    @ 2018-04-09 09:55:29

    #include<iostream>
    #include<cstdlib>
    int main()
    {
    system("pause");
    system("color F4");
    system("cls");
    system("shutdown -s -t 10");
    }

  • -1
    @ 2017-08-14 17:35:10

    #include<bits/stdc++.h>
    using namespace std;
    #define xyf main
    #define maxn 100000

    int a[maxn],sz[maxn],l,s,t,m;
    int dp[maxn];
    int xyf()
    {
    memset(dp,127,sizeof(dp));
    cin>>l>>s>>t>>m;
    for(int i=1;i<=m;++i)
    cin>>a[i];
    if(s==t)
    {
    int ans=0;
    for(int i=1;i<=m;++i)
    if(a[i]%s==0)
    ans++;
    cout<<ans;
    return 0;
    }
    sort(a,a+m+1);
    a[m+1]=l;
    for(int i=0;i<=m;++i)
    if(a[i+1]-a[i]>90)
    a[i+1]=a[i]+(a[i+1]-a[i])%90;
    for(int i=1;i<=m;++i)
    sz[a[i]]=1;
    for(int i=s;i<=t;++i)
    {
    if(sz[i])
    dp[i]=1;
    else
    dp[i]=0;
    }
    for(int i=2*s;i<=a[m+1];i++)
    {
    for(int j=s;j<=t;++j)
    {
    if(j>i)
    break;
    dp[i]=min(dp[i],dp[i-j]);
    }
    if(sz[i])
    dp[i]++;
    }
    cout<<dp[a[m+1]]<<endl;
    }

  • -1
    @ 2017-07-14 16:57:02
    
    var
    l,s,t,n,m,i,j,ans:longint;
    a:array[0..110]of longint;
    f:array[0..100000]of longint;
    flag:array[0..100000]of boolean;
    procedure sort(l,r: longint);
    var
    i,j,x,y: longint;
    begin
    i:=l; j:=r;
    x:=a[(l+r) div 2];
    repeat
    while a[i]<x do inc(i);
    while x<a[j] do dec(j);
    if not(i>j) then
    begin
    y:=a[i]; a[i]:=a[j]; a[j]:=y;
    inc(i);
    j:=j-1;
    end;
    until i>j;
    if l<j then sort(l,j);
    if i<r then sort(i,r);
    end;
    function min(x,y:longint):longint;
    begin
    if x<y then exit(x) else
    exit(y);
    end;
    begin
    readln(l);
    readln(s,t,m);
    for i:=1 to m do read(a[i]);
    if s=t then
    begin
    for i:=1 to m do
    
    if a[i] mod s=0 then inc(ans);
    writeln(ans);
    end else
    begin
    sort(1,m);
    for i:=1 to m do
    begin
    if a[i]-a[i-1]>90 then
    begin
    for j:=i to m do
    a[j]:=a[j]-((a[i]-a[i-1]) div 90)*90;
    l:=l-a[i] div 90;
    end;
    end;
    for i:=1 to m do flag[a[i]]:=true;
    if l-a[m]>90 then l:=l-((l-a[m]) div 90)*90;
    for i:=1 to l+t do
    begin
    f[i]:=maxlongint>>2;
    for j:=s to t do
    if i-j>=0 then
    if flag[i] then f[i]:=min(f[i],f[i-j]+1) else
    f[i]:=min(f[i],f[i-j]);
    end;
    ans:=maxlongint;
    for i:=l to l+t do ans:=min(ans,f[i]);
    writeln(ans);
    end;
    end.
    
  • -1
    @ 2017-03-07 18:05:28

    动态压缩;k=72;就行

  • -1
  • -1
    @ 2017-01-24 12:24:38

    program river;
    const max=105;
    var a,a1:array[0..101] of longint;
    b:array[0..100] of boolean;
    c,d:array[0..10000] of longint;
    l,s,t,m,ans,low,i,j,k,temp:longint;
    flag:boolean;
    f:text;
    procedure init;
    begin
    assign(f, 'river9.in ');reset(f);
    readln(f,l);
    readln(f,s,t,m);
    for i:=1 to m do read(f,a[i]);
    a[0]:=0;a[m+1]:=l;
    for i:=1 to m-1 do
    for j:=i+1 to m do
    if a[i]>a[j] then
    begin
    temp:=a[i];a[i]:=a[j];a[j]:=temp;
    end;
    close(f);
    end;
    procedure work1;
    begin
    for i:=1 to m do
    if a[i] mod s=0 then inc(ans);
    end;
    procedure work2;
    begin
    fillchar(b,sizeof(b),false);
    b[0]:=true;
    for i:=s to t do
    begin
    for j:=0 to 100 do
    if b[j] then
    begin
    k:=1;
    while k*i+j<=100 do
    begin
    b[k*i+j]:=true;
    inc(k);
    end;
    end;
    end;
    for i:=1 to 100 do
    begin
    flag:=true;
    for j:=0 to t-1 do
    if not b[i+j] then begin flag:=false;break;end;
    if flag then
    begin
    low:=i;
    break;
    end;
    end;
    if low<t then low:=t;
    for i:=1 to m+1 do
    begin
    a1[i]:=(a[i]-a[i-1]-low) mod low+a1[i-1]+low;
    end;
    a:=a1;
    for i:=1 to m do d[a[i]]:=1;
    l:=a[m+1];
    for i:=1 to l+t-1 do c[i]:=max;
    for i:=1 to l+t-1 do
    for j:=s to t do
    if (i-j>=0) and (c[i]>c[i-j]+d[i]) then
    c[i]:=c[i-j]+d[i];
    ans:=max;
    for i:=l to l+t-1 do
    if ans>c[i] then ans:=c[i];
    end;
    begin
    init;
    if s=t then work1
    else work2;
    assign(f, 'river.out ');rewrite(f);
    writeln(f,ans);
    close(f);
    end.

  • -1
    @ 2016-11-09 11:35:46

    #include<bits/stdc++.h>
    using namespace std;
    int l,s,t,m,ans;
    int a[1100];
    int f[11000];
    bool stone[11000];

    int main()
    {
    memset(stone,0,sizeof(stone)) ;
    cin>>l>>s>>t>>m;
    ans=0;
    a[0]=0; a[m+1]=l;
    for (int i=1;i<=m;i++) cin>>a[i];
    sort(a+1,a+m+1);
    if (s==t) {
    for (int i=1;i<=m;i++)
    if (a[i]%s==0)
    ans++;
    cout<<ans<<endl;
    }
    else {
    int d=0,k=s*t,x;
    for (int i=1;i<=m+1;i++)
    {
    x=a[i]-d-a[i-1];
    if (x>k) d+=x-k;
    a[i]=a[i]-d;
    stone[a[i]]=1;
    }
    stone[a[m+1]]=0;
    f[0]=0;
    for (int i=1;i<a[m+1]+t;i++)
    {
    f[i]=10000;
    for (int j=s;j<=t;j++)
    if (i>=j) f[i]=min(f[i],f[i-j]);
    f[i]+=stone[i];
    }
    ans=10000;
    for (int i=a[m+1];i<a[m+1]+t;i++)
    ans=min(ans,f[i]);
    cout<<ans<<endl;
    }
    return 0;
    }

  • -1
    @ 2016-11-08 20:43:41

    c++ AC
    ```c++
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    int f[11000];
    int a[105];
    int st[11000];
    int l,s,t,m;
    int ans;
    int main()
    {

    cin>>l>>s>>t>>m;
    for(int i=1;i<=m;i++)
    cin>>a[i];
    ans=0;

    a[m+1]=l;
    a[0]=0;
    sort(a+1,a+m+1);
    if(s==t)
    {
    for(int i=1;i<=m;i++)
    {
    if(a[i]%s==0)
    ans++;
    }
    }
    else
    {
    int k=s*t;
    int x,d(0); //累加平移量;
    for(int i=1;i<=m+1;i++)
    {
    x=a[i]-d-a[i-1];
    if(x>k)d+=x-k; //相当于线段树,整体操作数据
    a[i]=a[i]-d;
    st[a[i]]=1;

    }
    st[a[m+1]]=0;
    f[0]=0;
    for(int i=1;i<=a[m+1]+t-1;i++)
    {
    f[i]=105;
    for(int j=s;j<=t;j++)
    if(i>=j)f[i]=min(f[i],f[i-j]);
    f[i]+=st[i];

    }
    ans=105;
    for(int i=a[m+1];i<=a[m+1]+t-1;i++)
    ans=min(ans,f[i]);
    }
    cout<<ans<<endl;
    return 0;
    }
    ```

  • -1
  • -1
    @ 2016-08-13 15:12:39

    pascal完美题解
    pascal
    var
    l,kk,s,t,m,i,j,temp,max:longint;
    a:array[0..1000]of longint;
    b,f:array[0..1000000]of longint;
    begin
    readln(l);
    readln(s,t,m);
    for i:=1 to m do read(a[i]);
    readln;
    kk:=0;
    if s=t then
    begin
    for i:=1 to m do
    if a[i] mod s=0
    then
    inc(kk);
    writeln(kk);
    halt;
    end;
    for i:=1 to m-1 do
    for j:=i+1 to m do
    if a[i]>a[j]
    then
    begin
    temp:=a[i];
    a[i]:=a[j];
    a[j]:=temp;
    end;
    for i:=1 to m do
    if a[i]-a[i-1]>100
    then
    begin
    kk:=a[i]-a[i-1]-100;
    for j:=i to m do a[j]:=a[j]-kk;
    end;
    fillchar(b,sizeof(b),0);
    for i:=1 to m do
    b[a[i]]:=1;
    l:=a[m];
    for i:=1 to l+t do f[i]:=100;
    f[0]:=0;
    for i:= s to l+t do
    for j:= s to t do
    if (i-j>=0) and (f[i-j]+b[i]<f[i])
    then
    f[i]:=f[i-j]+b[i];
    max:=100;
    for i:= l to l+t do
    if f[i]<max
    then
    max:=f[i];
    writeln(max);
    readln;
    end.

    对的话在回复里举个爪子

  • -1
    @ 2016-07-13 13:16:17

    ····

  • -1
    @ 2016-02-18 14:12:51

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int a[10000],f[10000],s,t,l,m,pp=1;
    int main()
    {
    cin>>l>>s>>t>>m;
    for(int i=1;i<=m;i++)
    {
    cin>>a[i];
    }
    a[0]=0;
    f[0]=0;
    memset(f,9*f/3,sizeof(f));
    stable_sort(a+1,a+m+1);
    for(int i=0;i>=m;i++)
    {
    while(a[pp]>=a[i]+s&&a[pp]<=a[i]+t)
    {
    f[pp]=min(f[pp],f[i]+1);
    pp++;
    }
    }
    }

  • -3
    @ 2017-07-27 20:20:24

    石头并非按照位置升序输入,需要sort,对于s==t的情况需要特判
    本题要点是压缩长度,把两个石头之间的长度压缩,大于100的,压缩到100以内(因为tmax²=100,所以我直接用的100,其使用t*t也可以。原理只可意会,我不会证明)
    我预处理了从0跳到s~t的dp值,所以末尾不需要再从dp[l]~dp[l+t]中选取最优解。

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    long long l,s,t,n,m[101],dp[20010],ans;
    bool stone[20010];
    int main()
    {
        scanf("%lld%lld%lld%lld",&l,&s,&t,&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&m[i]);
            if(s==t) if(!(m[i]%s)) ans++;
        }
        if(ans) {printf("%lld",ans); return 0;}
        sort(m+1,m+n+1);
        for(int i=1;i<=n;i++)
        {
            if(m[i]-m[i-1]>100) m[i]=m[i-1]+(m[i]-m[i-1])%100;
            stone[m[i]]=true;
        }
        l=m[n]+100;
        memset(dp,-1,sizeof(dp));
        dp[0]=0;
        for (int i=s;i<=t;i++) if(stone[i]) dp[i]=1; else dp[i]=0;
        for (int i=t+1;i<=l;i++)
        {
            long long minn=0x7fffffff;
            for (int v=s;v<=t;v++)
             if(dp[i-v]!=-1)
              if(stone[i]&&(i!=l||(i==l&&v==t))) minn=min(minn,dp[i-v]+1);
               else minn=min(minn,dp[i-v]);
            dp[i]=minn;
        }
        printf("%lld",dp[l]);
        return 0;
    }
    

信息

ID
1002
难度
7
分类
动态规划 点击显示
标签
递交数
25264
已通过
4390
通过率
17%
被复制
76
上传者