瀹屽叏鑳屽寘杈撳嚭鏂规鏁帮紙dp)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了瀹屽叏鑳屽寘杈撳嚭鏂规鏁帮紙dp)相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/fast' title='fast'>fast   lin   娴嬭瘯鏁版嵁   template   cstring   stream   pac   temp   閲戣瀺   

鍦ㄤ竴涓?0缇庡厓鐨勫晢搴楅噷锛屾墍鏈変笢瑗块兘鍊?0缇庡厓鎴栨洿灏戙€備负浜嗗湪鍑虹撼澶勬洿鏈夋晥鍦颁负瀹㈡埛鏈嶅姟锛岄渶瑕佷互鏈€灏戞暟閲忕殑纭竵杩涜鎵鹃浂銆?br>鍦ㄨ繖涓棶棰樹腑锛屾偍灏嗕娇鐢ㄤ笉鍚岀殑纭竵鎻愪緵缁欏畾鐨勬壘闆跺€笺€傜紪鍐欎竴涓▼搴忔潵璁$畻姣忕纭竵绫诲瀷鎵€闇€鐨勭‖甯佹暟閲忋€?br>杈撳叆鍖呮嫭鍊紇锛岀‖甯佺粍n鐨勫ぇ灏忎互鍙婃瘡涓‖甯佺殑闈㈠€糵1锛宖2锛?..锛宖n銆傝緭鍑烘槸鏁板瓧鍒楄〃锛屽嵆c1锛?..锛宑n锛岃〃绀烘瘡绉嶇‖甯佺被鍨嬫墍闇€鐨勭‖甯佹暟閲忋€傛洿鏀瑰彲鑳芥湁寰堝鏂规硶銆?v鍊兼槸涓€涓弧瓒? <v鈮?000鐨勬暣鏁帮紝琛ㄧず鎵€闇€鐨勬洿鏀?br>鍒嗐€傜‖甯佺殑闈㈠€煎皬浜庢垨绛変簬10000銆傜▼搴忕殑杈撳嚭搴旈噰鐢ㄦ墍闇€纭竵鏁伴噺鏈€灏戠殑缁勫悎銆?br>渚嬪锛岀敱棣欐腐閲戣瀺绠$悊灞€鍙戣鐨勯娓‖甯佸寘鎷?0缇庡垎锛?0缇庡垎锛?0缇庡垎锛?缇庡厓锛?缇庡厓锛?缇庡厓鍜?0缇庡厓锛屽湪杈撳叆涓〃绀轰负n = 7锛宖1 = 10 锛宖2 = 20锛宖3 = 50锛宖4 = 100锛宖5 = 200锛宖6 = 500锛宖7 = 1000銆?br>杈撳叆
娴嬭瘯鏁版嵁鍙兘鍖呭惈璁稿娴嬭瘯鐢ㄤ緥锛岃澶勭悊鍒版枃浠舵湯灏俱€?br>姣忎釜娴嬭瘯鐢ㄤ緥鐨勪竴琛屼腑閮藉寘鍚暣鏁皏锛宯锛宖1锛?..锛宖n銆備繚璇乶鈮?0涓? <f1 <f2 <... <fn銆?br>杈撳嚭
杈撳嚭鏄竴琛屼腑鐨刵涓暟瀛楋紝浠ョ┖鏍煎垎闅斻€傚鏋滄病鏈夊彲鑳界殑鏇存敼锛屽垯鎮ㄧ殑杈撳嚭搴斾负鍗曚釜-1銆傚鏋滃彲鑳界殑瑙e喅鏂规涓嶆涓€绉嶏紝鍒欐偍鐨勭▼搴忓簲杈撳嚭浣跨敤鏇村闈㈠€艰緝浣庣殑纭竵鐨勮В鍐虫柟妗堛€?br>鏍蜂緥杈撳叆
2000 7 10 20 50100200500 1000
250 4 10 20 125150
35 4 10 20 125 150
48 4 1 8 16 20
40 4 1 10 13 37
43 5 1 2 21 40 80
鏍蜂緥杈撳嚭澶嶅埗
0 0 0 0 0 0 2
0 0 2 0
-1
0 1 0 2
3 0 0 1
1 1 0 1 0

 

 

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include<cstring>
#include<cstdio>
#include<iostream>
#include<queue> 
#include<algorithm>
using namespace std;
typedef long long ll;
template <typename Tp>
void read(Tp &x){//read(n);
    x=0;char ch=1;int fh;
    while(ch!=鈥?/span>-鈥?/span>&&(ch>鈥?/span>9鈥?/span>||ch<鈥?/span>0鈥?/span>)){
        ch=getchar();
    }
    if(ch==鈥?/span>-鈥?/span>){
        fh=-1;ch=getchar();
    }else fh=1;
    while(ch>=鈥?/span>0鈥?/span>&&ch<=鈥?/span>9鈥?/span>){
        x=(x<<1)+(x<<3)+ch-鈥?/span>0鈥?/span>;ch=getchar();
    }
    x*=fh;
}
inline char read1()//瀛楃涓茶鍏ユ寕
{
    register char ch=getchar();
    while(ch<鈥?/span>A鈥?/span>||ch>鈥?/span>M鈥?/span>)ch=getchar();
    return ch; 
}
const int maxn=1e6+100;
const int mod=1000000007;
const int INF=0x3f3f3f; 
int a[maxn];
int dp[maxn];
int vis[maxn];
int main(){
    int m,n;
    while(cin>>m>>n){
        memset(dp,0x3f3f,sizeof(dp));
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        dp[0]=0;
        for(int i=1;i<=n;i++){
            for(int j=a[i];j<=m;j++){
                dp[j]=min(dp[j],dp[j-a[i]]+1);
            }
        }
        if(dp[m]==0x3f3f3f3f){
            printf("-1
");
            continue;
        }
        int z=m;
        int p=0;
        for(int i=n;i>=1;i--){
            p=0; 
            while(z-a[i]>=0&&dp[z]==dp[z-a[i]]+1){
                vis[i]++;
                z-=a[i];        
            } 
        }
        for(int i=1;i<=n;i++){
            cout<<vis[i]<<" ";
        }
        cout<<endl;
    }
    
} 

 
















以上是关于瀹屽叏鑳屽寘杈撳嚭鏂规鏁帮紙dp)的主要内容,如果未能解决你的问题,请参考以下文章

涓€瀵逛竴鍏宠仈鏌ヨ鏃朵娇鐢╮elation杩炶疮鎿嶄綔鏌ヨ鍚庯紝璋冪敤getLastSql()鏂规硶杈撳嚭鐨剆ql璇彞

浠€涔堟槸sigmoid婵€娲诲嚱鏁帮紵

[鑳屽寘涔濊1]

01鑳屽寘璺緞鍥炴函

绠楁硶鍔ㄦ€佽鍒掞紙浜旓級-鑳屽寘闂2

鍔ㄦ€佽鍒掕繘闃剁瘒1---鑳屽寘闂