Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

bool --DP 32MS!

Posted by lz1 at 2010-09-19 20:39:22 on Problem 1276
#include <stdio.h>
#include <string.h>
const int L=100001,M=11;
int cash,n,v[M],c[M],ans;
bool f[L];

int main(void){
    freopen ("1276.in","r",stdin);
    freopen ("1276.out","w",stdout);
    f[0]=true;
    while (scanf ("%d%d",&cash,&n)!=EOF){
          for (int i=1;i<=n;i++)scanf ("%d%d",v+i,c+i);
          if (!n||!cash){printf ("0\n");continue;}
          memset (f+1,false,sizeof (bool)*cash);
          for (int i=1;i<=n;i++){
              int m=v[i];
              for (int j=1;j<=m;m-=j,j<<=1)
                  for (int k=cash;k>=j*c[i];k--)
                      if (f[k-c[i]*j])f[k]=true;
              for (int k=cash;k>=m*c[i];k--)
                      if (f[k-c[i]*m])f[k]=true;
              }
          ans=cash;
          while (!f[ans])ans--;
          printf ("%d\n",ans);
          }
    return 0;
    }

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator