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

Re:不需要费力气想,用背包背出来

Posted by zhangcy269 at 2023-01-10 17:29:41 on Problem 1032 and last updated at 2023-01-10 17:31:50
In Reply To:Re:不需要费力气想,用背包背出来 Posted by:zoujing1978 at 2018-12-17 11:20:55
> 用背包做的,能贴个代码出来么?我学习一下,谢谢!
#include <iostream>
#include <algorithm>
int main() {
    double x[1001];int fa[1001],r[1001],cntr=0,N;
    scanf("%d",&N);
    memset(x,0,sizeof(x));x[0]=1;
    for(int i=1;i<=N;i++)
        for(int j=N;j>=0;j--)//01背包,逆序遍历
            if(j>=i&&x[j-i])
                if(x[j-i]*i>x[j]){
                    x[j]=x[j-i]*i;
                    fa[j]=j-i;
                }
    int tmp=N;
    while(tmp){
        r[cntr++]=tmp-fa[tmp];
        tmp=fa[tmp];
    }
    std::sort(r,r+cntr);
    for(int i=0;i<cntr;i++) printf("%d ",r[i]);
    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