Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ff first
  5. #define ss second
  6. #define m_p make_pair
  7. #define pb push_back
  8. #define ppb pop_back
  9. #define pf push_front
  10. #define ppf pop_front
  11. #define ll long long
  12. #define l_b lower_bound
  13. #define u_b upper_bound
  14.  
  15. int ar[15];
  16. bool dp[1500];
  17. int used[1500];
  18. int stamp[15][15],len[15];
  19. int main()
  20. {
  21.     int s;
  22.     while(scanf("%d",&s)==1 && s)
  23.     {
  24.         int n;
  25.         scanf("%d",&n);
  26.         for(int i=1;i<=n;i++)
  27.         {
  28.             scanf("%d",&len[i]);
  29.             for(int j=1;j<=len[i];j++) scanf("%d",&stamp[i][j]);//input
  30.         }
  31.  
  32.         int ch=-1,ans=0; // ans=final overall maximum cover , ch= appropriate set's index for maximum coverage
  33.  
  34.         for(int i=1;i<=n;i++) // loop for every set
  35.         {
  36.             memset(dp,0,sizeof dp);//initialization
  37.             for(int ii=0;ii<=1050;ii++) used[ii]=1e9;//initialization
  38.             dp[0]=1;//initialization
  39.             used[0]=0;//initialization
  40.             int mcov=0,mpos;//mcov= maximum cover for this set
  41.                             //mpos= hudai :P
  42.  
  43.             for(int j=0;j<=1050;j++)
  44.             {
  45.                 if(!dp[j])
  46.                 {
  47.                     mcov=j-1; // found our range no more loop is need.
  48.                     break;
  49.                 }
  50.                 for(int k=1;k<=len[i];k++)//picking the coins of this set one by one
  51.                 {
  52.                     for(int l=1;l<=(s-used[j]);l++)
  53.                     {
  54.                         dp[j+stamp[i][k]*l]=1;// covering the next amounts
  55.                         used[j+stamp[i][k]*l]=min(used[j+stamp[i][k]*l],l+used[j]); //updating used
  56.                     }
  57.                 }
  58.             }
  59.  
  60.             if(ch==-1 || mcov>ans) ans=mcov,ch=i;
  61.             else if(mcov==ans) // in case of equality , finding the appropriate set
  62.             {
  63.                 if(len[i]<len[ch]) ch=i;
  64.                 else if(len[i]==len[ch])
  65.                 {
  66.                     int ind=len[i];
  67.                     while(ind>0 && stamp[i][ind]==stamp[ch][ind]) ind--;
  68.                     if(ind && stamp[i][ind]<stamp[ch][ind]) ch=i;
  69.                 }
  70.             }
  71.  
  72.         }
  73.         printf("max coverage =%4d :",ans);//a bit typical output format
  74.         for(int i=1;i<=len[ch];i++)
  75.         {
  76.             printf("%3d",stamp[ch][i]);//a bit typical output format
  77.         }
  78.         puts("");
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement