이진 분할

7326 단어
#include 
#include 
#include 
#define N 10005
#define M 100100
int dp[3][M],a[N];
using namespace std;
int max (int a,int b)
{
    return a>b?a:b;
}
int main()
{
    int m,n,i,k,k1,j,L=0,nk,dk;
    while (cin>>m>>n)
    {
        memset (dp,0,sizeof(dp));
        memset (a,0,sizeof(a));
        k=0;
        while (n--)
        {
            cin>>nk>>dk;
            k1=1;
            while (nk-k1>=0)
               {
                   a[k++]=dk*k1;
                   nk-=k1;
                   k1*=2;
               }
               if (nk)  a[k++]=dk*nk;
        } //     
        for (i=0;i<k;i++)
            for (j=0;j<=m;j++)
                if (i>0)
                {
                    L=i&1;   //        
                    dp[L][j]=dp[L^1][j];
                        if (j>=a[i])
                        dp[L][j]=max(dp[L^1][j-a[i]]+a[i],dp[L][j]);
                }
                else
                {
                    if (j>=a[0])
                    dp[0][j]=a[0];
                }
    cout<<dp[L][m]<<endl;
    }
    return 0;
}

전재 대상:https://www.cnblogs.com/You-Change/p/3426082.html

좋은 웹페이지 즐겨찾기