代码:
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<cmath>
using namespace std;
const int M=105,N=555075;
int s,t,i,j,k,n,m,w[M],ans,f[N],l,a[N];
bool ff[N],qw[N];
void st(int l,int r)
{
int i=l,j=r,x=w[(i+j)/2];
while (i<=j)
{
while (w[i]<x) i++;
while (w[j]>x) j--;
if (i<=j)
{
k=w[i];w[i]=w[j];w[j]=k;
i++;j--;
}
}
if (i<r) st(i,r);
if (l<j) st(l,j);
}
void cw()
{
for (i=1;i<=m;i++)
if ((w[i]>=s)&&(w[i]%s==0)) ans++;
cout<<ans<<endl;
}
void sw()
{
st(1,m);k=0;
for (i=s;i<=t;i++) k+=i;
w[0]=0;
for (i=m;i>=1;i--) w[i]-=w[i-1];
for (i=1;i<=m;i++)
{
if (w[i]>k) w[i]=w[i-1]+k;
else w[i]+=w[i-1];
qw[w[i]]=1;
}
n=w[m]+k;
for (i=1;i<=n;i++) f[i]=111;
ff[0]=1;f[0]=0;
for (i=0;i<=w[m];i++)
if (ff[i])
{
for (j=s;j<=t;j++)
if (f[i+j]>f[i]+qw[i+j])
{
if (!ff[i+j]) ff[i+j]=1;
f[i+j]=f[i]+qw[i+j];
}
}
ans=111;
for (i=w[m];i<=n;i++)
if (f[i]<ans) ans=f[i];
cout<<ans<<endl;
}
int main()
{
freopen("river.in","r",stdin);
freopen("river.out","w",stdout);
cin>>n>>s>>t>>m;
for (i=1;i<=m;i++) scanf("%D",&w[i]);
if (s==t) cw();
else sw();
fclose(stdin);fclose(stdout);
return 0;
}
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<cmath>
using namespace std;
const int M=105,N=555075;
int s,t,i,j,k,n,m,w[M],ans,f[N],l,a[N];
bool ff[N],qw[N];
void st(int l,int r)
{
int i=l,j=r,x=w[(i+j)/2];
while (i<=j)
{
while (w[i]<x) i++;
while (w[j]>x) j--;
if (i<=j)
{
k=w[i];w[i]=w[j];w[j]=k;
i++;j--;
}
}
if (i<r) st(i,r);
if (l<j) st(l,j);
}
void cw()
{
for (i=1;i<=m;i++)
if ((w[i]>=s)&&(w[i]%s==0)) ans++;
cout<<ans<<endl;
}
void sw()
{
st(1,m);k=0;
for (i=s;i<=t;i++) k+=i;
w[0]=0;
for (i=m;i>=1;i--) w[i]-=w[i-1];
for (i=1;i<=m;i++)
{
if (w[i]>k) w[i]=w[i-1]+k;
else w[i]+=w[i-1];
qw[w[i]]=1;
}
n=w[m]+k;
for (i=1;i<=n;i++) f[i]=111;
ff[0]=1;f[0]=0;
for (i=0;i<=w[m];i++)
if (ff[i])
{
for (j=s;j<=t;j++)
if (f[i+j]>f[i]+qw[i+j])
{
if (!ff[i+j]) ff[i+j]=1;
f[i+j]=f[i]+qw[i+j];
}
}
ans=111;
for (i=w[m];i<=n;i++)
if (f[i]<ans) ans=f[i];
cout<<ans<<endl;
}
int main()
{
freopen("river.in","r",stdin);
freopen("river.out","w",stdout);
cin>>n>>s>>t>>m;
for (i=1;i<=m;i++) scanf("%D",&w[i]);
if (s==t) cw();
else sw();
fclose(stdin);fclose(stdout);
return 0;
}