1387: CD
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
![]() | 3s | 8192K | 1973 | 367 | Standard |
You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How to choose tracks from CD to get most out of tape space and have as short unused space as possible.
Assumptions:
- number of tracks on the CD. does not exceed 100
- no track is longer than N minutes
- length of each track is expressed as an integer number
- N is also integer
Program should find the set of tracks which fills the tape best and print it in the same sequence as the tracks are stored on the CD
Input
Any number of lines. Each one contains value N, (after space) number of tracks and durations of the tracks. For example from first line in sample data: N=5, number of tracks=3, first track lasts for 1 minute, second one 3 minutes, next one 4 minutesOutput
string `` sum:" and sum of duration times.Sample Input
5 3 1 3 4 10 4 9 8 4 2 20 4 10 5 7 4 90 8 10 23 1 2 3 4 5 7 45 8 4 10 44 43 12 9 8 2
Sample Output
sum:5 sum:10 sum:19 sum:55 sum:45
This problem is used for contest: 105
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,a[300],s[300],sum,total;
void dfs(int d=m-1,int p=0)
{
if (sum==n) return;
if (d<0) return;
if (p+s[d]<=sum) return;
// if (p+a[0]>n) return;
int t=p+a[d];
if (t<=n && t>sum) sum=t;
if (t<=n) dfs(d-1,t);
dfs(d-1,p);
}
int main()
{
int i;
while(scanf("%d%d",&n,&m)!=EOF)
{
total=0;
for(i=0;i<m;i++) {scanf("%d",&a[i]);total+=a[i];}
if(total<=n) {printf("sum:%d/n",total);continue;}
sort(a,a+m);
memset(s,0,sizeof(s));
for(i=0;i<m;i++)
{
if(i==0) s[i]=a[i];
else s[i]=s[i-1]+a[i];
}
sum=0;
dfs();
printf("sum:%d/n",sum);
}
return 0;
}
/*
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,a[300],s[300],total;
int maxn;
bool cmp(int x,int y) {return x>y;}
void dfs(int id,int sum)
{
if(maxn==n) return ;
if(id==m) return ;
if(id>=1&&sum+total-s[id-1]<=maxn) return ;
int t=sum+a[id];
if(t<=n&&t>maxn) maxn=t;
if(t<=n) dfs(id+1,t);
dfs(id+1,sum);
}
int main()
{
int i;
while(scanf("%d%d",&n,&m)!=EOF)
{
total=0;
for(i=0;i<m;i++) {scanf("%d",&a[i]);total+=a[i];}
if(total<=n) {printf("sum:%d/n",total);continue;}
sort(a,a+m,cmp);
memset(s,0,sizeof(s));
for(i=0;i<m;i++)
{
if(i==0) s[i]=a[i];
else s[i]=s[i-1]+a[i];
}
maxn=0;
dfs(0,0);
printf("sum:%d/n",maxn);
}
return 0;
}
*/
/*
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,a[300],s[300],total;
int maxn;
bool cmp(int x,int y) {return x>y;}
void dfs(int id,int sum)
{
if(maxn==n) return ;
if(sum>n) return ;
if(id>=1&&sum+total-s[id-1]<=maxn) return ;
if(id==m)
{
if(sum>maxn) maxn=sum;
return ;
}
dfs(id+1,sum+a[id]);
dfs(id+1,sum);
}
int main()
{
int i;
while(scanf("%d%d",&n,&m)!=EOF)
{
total=0;
for(i=0;i<m;i++) {scanf("%d",&a[i]);total+=a[i];}
if(total<=n) {printf("sum:%d/n",total);continue;}
sort(a,a+m,cmp);
memset(s,0,sizeof(s));
for(i=0;i<m;i++)
{
if(i==0) s[i]=a[i];
else s[i]=s[i-1]+a[i];
}
maxn=0;
dfs(0,0);
printf("sum:%d/n",maxn);
}
return 0;
}
*/