## 473 - Raucous Rockers

Moderator: Board moderators

### 473 - Raucous Rockers

Hello,

I tried to solve problem 473 by a greedy algorithm:

1. S := sequence of song lengths (as given in input)
2. determine number of disks needed for the songs in S
3. if disks <= m return |S|
4. otherwise remove longest song from S and go to 2.

I assumed that the song order given in the input must be maintained over all disks, so I put songs on disk 1 until it's full, then on disk 2 until it's full, and so on.

I didn't find any case where the result differs from my (too slow) backtracking solution. Obviously, there must be such cases, because I received WA.

Thanks,
Christian

Christian Schuster
Learning poster

Posts: 63
Joined: Thu Apr 04, 2002 2:00 am

Your greedy is correct if you apply it to one disk only. So to get a correct overall algorithm, use dynamic programming to partition the n songs into m intervals of songs which will be assigned to disks. Within one disk, apply your greedy algorithm until the songs fit on that disk. The dynamic programming part is similar to problems 709 and 10239.
Guru

Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

I also just found a counterexample for your current greedy algorithm:
4 11 2
9, 10, 2, 11

optimal solution is:
9 2
11
9
2

unfortunately, the O(n^2 * m) dynamic programming algorithm I have is too slow. And the pruning I inserted seems to be wrong.
Guru

Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Thanks, I just got AC with an O(n*m*t) DP algorithm.

Christian Schuster
Learning poster

Posts: 63
Joined: Thu Apr 04, 2002 2:00 am

Code: Select all
` The songs will be recorded on the set of disks in the order of the dates they were written.`

Does this mean that we will record disk 1 first, then disk 2, then 3, etc.? Or can we record song 1 onto disk 1, then song 2 onto disk 2 and then song 3 onto disk 1 again?
Let's hope Yury doesn't notice that I'm solving problems again.

Abednego
A great helper

Posts: 281
Joined: Tue Sep 10, 2002 5:14 am
Location: Mountain View, CA, USA

It's not allowed to put songs 1 and 3 on disk 1 and song 2 on disk 2. More formally, for every possible pair (a,b) of chosen song numbers having a>b, the relation disk(a)>=disk(b) must hold.

Christian Schuster
Learning poster

Posts: 63
Joined: Thu Apr 04, 2002 2:00 am

Ok. I got it accepted in 2 seconds with a O(n*m*t) DP using O(m*t) memory. n is at most 10240, t is at most 128 and m is at most 128. They are probably smaller than this, but they are certainly not larger (checked with assert()).
Let's hope Yury doesn't notice that I'm solving problems again.

Abednego
A great helper

Posts: 281
Joined: Tue Sep 10, 2002 5:14 am
Location: Mountain View, CA, USA

### Re: 473 - Raucous Rockers

Hello,
Does this problem is a binary knapsack problem with some constraint? The recursion formula:

Code: Select all
`M(i, j) = M(i-1, j) if j-t(i) and j belongs to diffrent discsM(i, j) = max{M(i-1, j), M(i-1, j-t(i)) + 1} otherwise1 <= i <= n1<= j <= m*tM(i, j) - maximum number of songs from range 1..i that could be placed on discs with whole size of j `

The second line is standard 1/0 knapsack formula. The first line gives constraint that single track cannot begin in first CD and ens at second?
Does my reasoning is correct?
rnd
New poster

Posts: 1
Joined: Mon Feb 08, 2010 11:02 pm

### Re: 473 - Raucous Rockers

its easy to solve in N^2*m..but i can't get he m*n*t solution
Could anyone Give some hints ....?

>>>>>>>>> A2
Beliefs are not facts, believe what you need to believe;)
Angeh
Experienced poster

Posts: 108
Joined: Sat Aug 08, 2009 2:53 pm

### 473 - Raucous Rockers - RTE

This code works on my box, but keeps giving me RTEs.
Any ideas?
Code: Select all
`#include <iostream>#define MAX 2000000using namespace std;struct Disk {  int totalTime;  int capacity;};class DiskCollection {   private:       Disk currentDisk;       int totalTracks;       int diskToWrite;       int qtdDisks;  public:  DiskCollection(int qtdDisks, int capacity) {      this->currentDisk.totalTime = 0;      this->currentDisk.capacity = capacity;      this->qtdDisks = qtdDisks;      this->totalTracks = 0;      this->diskToWrite = 0;  }  int getTotalTracks() {      return this->totalTracks;  }  bool isBetterThan(DiskCollection * diskCollection) {      if (this->totalTracks > diskCollection->totalTracks) {          return true;      }      if (this->totalTracks == diskCollection->totalTracks) {          if (this->diskToWrite < diskCollection->diskToWrite) {              return true;          }          if (this->diskToWrite == diskCollection->diskToWrite) {              if (this->currentDisk.totalTime <diskCollection->currentDisk.totalTime) {                  return true;              }              return false;          }          return false;      }      return false;  }  DiskCollection * insertTrack(int trackTime) {      DiskCollection * result = NULL;      if (this->currentDisk.capacity - this->currentDisk.totalTime >=trackTime) {          result = new DiskCollection(this->qtdDisks,                  this->currentDisk.capacity);          result->totalTracks = this->totalTracks + 1;          result->diskToWrite = this->diskToWrite;          result->currentDisk.capacity = this->currentDisk.capacity;          result->currentDisk.totalTime = this->currentDisk.totalTime                  + trackTime;      } else if (this->diskToWrite != this->qtdDisks - 1) {          result = new DiskCollection(this->qtdDisks,                  this->currentDisk.capacity);          result->totalTracks = this->totalTracks + 1;          result->diskToWrite = this->diskToWrite + 1;          result->currentDisk.capacity = this->currentDisk.capacity;          result->currentDisk.totalTime = trackTime;      }      return result;  }};DiskCollection * emptyCollection(int qtdDisks, int capacity) { DiskCollection * empty = new DiskCollection(qtdDisks, capacity); return empty;}int main() {   char line[MAX];   cin.getline(line, MAX);   int qtdTc = atoi(line);   for (int tc = 1; tc <= qtdTc; tc++) {       // Ignora linha em branco       cin.getline(line, MAX);       // Leitura dos parâmetros       cin.getline(line, MAX);       int n = atoi(strtok(line, " "));       int t = atoi(strtok(NULL, " "));       int m = atoi(strtok(NULL, " "));       // Ignora o resto da linha       cin.getline(line, MAX);       char * tok = strtok(line, " ,");       int trackTimes[n + 1];       int i = 1;       while (tok) {           trackTimes[i++] = atoi(tok);           tok = strtok(NULL, " ,");       }       DiskCollection * dc[n + 1][t * m + 1];       for (i = 0; i <= n; i++) {           for (int j = 0; j <= t * m; j++) {               if (i == 0) {                   dc[i][j] = emptyCollection(m, t);               } else if (j - trackTimes[i] < 0) {                   dc[i][j] = dc[i - 1][j];               } else {                   DiskCollection * newCollection = dc[i - 1][j                              - trackTimes[i]]->insertTrack(trackTimes[i]);                   if (newCollection                             && newCollection->isBetterThan(dc[i - 1][j])) {                       dc[i][j] = newCollection;                   } else {                       dc[i][j] = dc[i - 1][j];                   }               }           }       }       cout << dc[n][t * m]->getTotalTracks() << endl << endl;   }}`
fernandohbc
New poster

Posts: 5
Joined: Sat Aug 14, 2010 10:31 pm

### Re: 473 - Raucous Rockers

n<=800 , m<=100 and t<=100.
I got AC with this limits.
shibly
New poster

Posts: 5
Joined: Wed Sep 22, 2010 7:32 am

### Re: 473 - Raucous Rockers

Got WA . Can anoyone help??
Code: Select all
`#include<cstdio>#include<iostream>#include<vector>#include<utility>#include<algorithm>using namespace std;int main(){    int c,i,j,k,ans,n,t,m,temp,tem2,tk,x;    vector<int> songs;    vector<vector<int> > dp;    scanf("%d",&c);    for(x=1;x<=c;x++)    {        songs.clear();        dp.clear();        scanf("%d %d %d",&n,&t,&m);        for(i=0;i<n-1;i++)        {            scanf("%d, ",&temp);            songs.push_back(temp);        }        scanf("%d",&temp);        songs.push_back(temp);        //for(i=0;i<n;i++) cout<<songs[i]<<' ';        dp.resize(n+1);        for(i=0;i<dp.size();i++) dp[i].resize(m+1);        for(i=0;i<=m;i++) dp[n][i]=0;        for(i=0;i<=n;i++) dp[i][0]=0;        if(songs[n-1]<=t) for(i=1;i<=m;i++) dp[n-1][i]=1;        else for(i=1;i<=m;i++) dp[n-1][i]=0;        for(i=n-2;i>=0;i--)        {            for(j=1;j<=m;j++)            {                dp[i][j]=dp[i+1][j];                if(songs[i]<=t)                {                    temp=0;                    tk=0;                    for(k=i;k<n;k++)                    {                        if(temp+songs[k]<=t)                        {                            tk++;                            temp+=songs[k];                            tem2=dp[k+1][j-1];                            if(tem2+tk>dp[i][j]) dp[i][j]=tem2+tk;                        }                    }                }            }        }        if(x!=1) printf("\n%d\n",dp[0][m]);        else printf("%d\n",dp[0][m]);    }    return 0;}`
shopnobaj_raju
New poster

Posts: 7
Joined: Wed Oct 19, 2011 5:07 pm