一百分做法,完全背包的运用
#include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<cctype> #include<iostream> using namespace std; const int maxn=10009; const int maxm=1009; const int inf=0x3f3f3f3f; int n,m,k,X[maxn],Y[maxn],P[maxn],L[maxn],H[maxn],f[maxn][maxm]; bool b[maxn][maxn],g[maxn]; int main() { scanf("%d%d%d",&n,&m,&k); for (int i=0;i<n;i++) scanf("%d%d",&X[i],&Y[i]); for (int i=0;i<=n;i++) L[i]=0,H[i]=m+1; for (int i=1;i<=k;i++) { int x; scanf("%d",&x); scanf("%d%d",&L[x],&H[x]); g[x]=true; } int rt=0; for (int i=1;i<=n;i++) { bool t=false; for (int j=1;j<=m;j++) { f[i][j]=inf; if (j>X[i-1])f[i][j]=min(f[i][j],f[i-1][j-X[i-1]]+1); if (j>X[i-1])f[i][j]=min(f[i][j],f[i][j-X[i-1]]+1); }//1 for (int j=m-X[i-1];j<=m;j++) { f[i][m]=min(f[i][m],f[i-1][j]+1); f[i][m]=min(f[i][m],f[i][j]+1); } for (int j=L[i]+1;j<=H[i]-1;j++) { if (j+Y[i-1]<=m) f[i][j]=min(f[i][j],f[i-1][j+Y[i-1]]); }//2 for (int j=1;j<=L[i];j++) f[i][j]=inf; for (int j=H[i];j<=m;j++) f[i][j]=inf; for (int j=1;j<=m;j++) if (f[i][j]<inf){t=true;break;} if (!t) { printf("0\n%d",rt); return 0; } if (g[i]) rt++; } int ans=inf; for (int i=1;i<=m;i++) ans=min(f[n][i],ans); printf("1\n%d",ans); return 0; }
开始挑战 飞扬的小鸟
本来想做70分数据,结果得了85分,不过debug的时间太长了,好几个(非常多)的细节没有考虑到,还好调试功能好用
</pre><pre name="code" class="cpp">#include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<cctype> #include<iostream> using namespace std; const int maxn=10009; const int maxm=1009; const int inf=0x3f3f3f3f; int n,m,k,X[maxn],Y[maxn],P[maxn],L[maxn],H[maxn],f[maxn][maxm]; bool b[maxn][maxn],g[maxn]; bool check(int i,int j) { if (j<=0||j>m) return false; if (b[i][j]) return false; return true;//当时忘记在最后加 return true了 } int main() { scanf("%d%d%d",&n,&m,&k); for (int i=0;i<n;i++) scanf("%d%d",&X[i],&Y[i]); for (int i=1;i<=k;i++) { scanf("%d%d%d",&P[i],&L[i],&H[i]); for (int j=0;j<=L[i];j++) b[P[i]][j]=true; for (int j=H[i];j<=m;j++) b[P[i]][j]=true; g[P[i]]=true; } int rt=0; for (int i=1;i<=n;i++) { bool t=false; for (int j=1;j<m;j++) if (check(i,j)) { f[i][j]=inf; if (check(i-1,j+Y[i-1]))f[i][j]=f[i-1][j+Y[i-1]]; int l=1; while (j-l*X[i-1]>0) { if (check(i-1,j-l*X[i-1])) f[i][j]=min(f[i][j],f[i-1][j-l*X[i-1]]+l); l++; }//这一块没有加l++,然后没有考虑好细节 if (f[i][j]!=inf) t=true; } f[i][m]=inf; if (check(i,m)) for (int j=m;j>0;j--) if (check(i-1,j)) { int l; if ((m-j)%X[i-1]==0) l=(m-j)/X[i-1];else l=(m-j)/X[i-1]+1; if (j==m) l=1; f[i][m]=min(f[i][m],f[i-1][j]+l); } if (f[i][m]!=inf) t=true;//以上都是没有考虑,高度为m时的情况 if (!t) { printf("0\n%d",rt); return 0; }//不可行的情况输出 if (g[i]) rt++; } int ans=inf; for (int i=1;i<=m;i++) ans=min(f[n][i],ans); printf("1\n%d",ans);//有答案的情况输出 return 0; }