Ultraviolet A 473-Raucous Rockers (DP)
There are n songs and m CDs, each of which can be at most t-time songs, and the length of each song is given, you must enter the CD in sequence (you can choose not to record a few songs) and the maximum number of songs you can record.
Use d [I] [j] [0] to represent the first I song and play j songs. The minimum number of CDs is used, use d [I] [j] [1] to represent the first I song and put j songs. If the number of CDs is the least, the maximum remaining capacity of the last one can be reached. Recurrence is completed based on whether or not to play the song I.
In this case, recursion is correct, because when the first song is on j, the best case is to use as few CDs as possible, in addition, the capacity of the last disc should be as large as possible (for example, if the number of Discs used is large and the size of the last disc is larger, this is not as good as the previous one ). The program uses a rolling array to optimize the memory.
State transition equation:
If d [I-1] [j] [1]> = a [I,
If d [I-1] [j] [1]
#include
#include
#include
int a[1530];int d[2][1530][2];char e[100010];int main(void){int i,j,v,n,m,pi,qi,p1,p2,minp1,minp2,sump,top,lo,cur,ans;scanf("%d",&pi);for(qi=1;qi<=pi;qi++){scanf("%d%d%d",&n,&v,&m);while(getchar()==' '){;}gets(e+1);lo=strlen(e+1);top=0;i=1;while(i<=lo){if((e[i]>='0')&&(e[i]<='9')){sump=0;while((e[i]>='0')&&(e[i]<='9')){sump=sump*10+e[i]-'0';i++;}top++;a[top]=sump;}else{i++;}}cur=1;for(i=1;i<=n;i++){cur^=1;if(d[cur^1][i-1][1]>=a[i]){d[cur][i][0]=d[cur^1][i-1][0];d[cur][i][1]=d[cur^1][i-1][1]-a[i];}else{d[cur][i][0]=d[cur^1][i-1][0]+1;d[cur][i][1]=v-a[i];}for(j=1;j
=a[i]){p1=d[cur^1][j-1][0];p2=d[cur^1][j-1][1]-a[i];if((p1
minp2))){minp1=p1;minp2=p2;}}else{p1=d[cur^1][j-1][0]+1;p2=v-a[i];if((p1
minp2))){minp1=p1;minp2=p2;}}d[cur][j][0]=minp1;d[cur][j][1]=minp2;}}ans=0;for(i=n;i>=1;i--){if(d[cur][i][0]<=m){ans=i;break;}}printf("%d\n",ans);if(qi!=pi){printf("\n");}}return 0;}