銆愬姩鎬佽鍒掋€戣儗鍖呬節璁插強鐩稿簲涔犻
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆愬姩鎬佽鍒掋€戣儗鍖呬節璁插強鐩稿簲涔犻相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/max' title='max'>max
csrf his while video closed follow銆愬弬鑰冨崥瀹㈠強瑙嗛銆?/h2>
1銆?a title="澶ч洩鑿? href="https://www.bilibili.com/video/av33930433?from=search&seid=1735310761952665245" target="_blank">澶ч洩鑿?/a>
2銆?a href="https://blog.csdn.net/yandaoqiusheng/article/details/84782655" target="_blank">鑳屽寘涔濊——鍏ㄧ瘒璇︾粏鐞嗚В涓庝唬鐮佸疄鐜?/a>
3銆?a id="cb_post_title_url" class="postTitle2" href="https://www.cnblogs.com/jbelial/articles/2116074.html">dd澶х墰鐨勩€婅儗鍖呬節璁层€?/a>
4銆?a href="https://www.luogu.org/blog/RPdreamer/bei-bao-wen-ti" target="_blank">鑳屽寘闂 (闄勫崟璋冮槦鍒椾紭鍖栧閲嶈儗鍖?/a>
銆愰鐩€?/h2>
1銆丄cwing
2銆?1鑳屽寘闂
Luogu 2925 骞茶崏鍑哄敭
Luogu 1616 鐤媯鐨勯噰鑽?/a>
HDU 3466 Proud Merchants
3銆佸畬鍏ㄨ儗鍖呴棶棰?/h3>
HDU 1114 Piggy-Bank
Luogu 1853 鎶曡祫鐨勬渶澶ф晥鐩?/a>
4銆佸閲嶈儗鍖呴棶棰?/h3>
HDU 1059 Dividing
Luogu P1776 瀹濈墿绛涢€?/a>
5銆佹贩鍚堣儗鍖呴棶棰?/h3>
Luogu P1833 妯辫姳
HDU 3535 AreYouBusy
6銆佷簩缁磋垂鐢ㄨ儗鍖呴棶棰?/h3>
Luogu 1507 NASA鐨勯鐗╄鍒?/a>
HDU 2159 FATE
7銆佸垎缁勮儗鍖呴棶棰?/h3>
Luogu 1757 閫氬ぉ涔嬪垎缁勮儗鍖?/a>
HDU 1712 ACboy needs your help
8銆佹湁渚濊禆鑳屽寘闂
HDU 3449 Consumer
Luogu 1064 閲戞槑鐨勯绠楁柟妗?/a>
9銆佹硾鍖栫墿鍝?/h3>
HDU 2639 Bone Collector II
HDU 3810 Magina
銆愬皬缁撱€?/p>
銆€銆€鑳屽寘铏借鏈変節璁诧紝鍏跺疄鏍稿績鍏跺疄鍙湁01鑳屽寘鍜屽畬鍏ㄨ儗鍖呬袱涓渶瑕佸ぇ瀹舵繁鍒荤悊瑙e悗锛屽叾浣欑殑鑳屽寘闂鍙槸浠庝笉鍚岀殑瑙掑害鏉ユ彁鍑洪棶棰橈紝瀹炶川涓婅繕鏄拰鏈€鍩烘湰鐨勮儗鍖呴棶棰樹竴鏍枫€傝櫧鐒跺ぇ瀹惰寰椾節璁插緢涔卞緢楹荤儲銆備竴寮€濮嬫垜涔熸槸杩欐牱瑙夊緱鐨勶紝鍚庢潵鐪嬩簡Acwing涓婇洩鑿滅殑璁茶В锛岀涓€鎰熻鏄細涓嶄粎闀垮緱甯呰繕鏁插緱涓€鎵嬪ソ浠g爜锛岀湡鐨勪笉寰椾笉浣╂湇浜哄鏉ヨ嚜鍖楀ぇ銆備箣鍚庡彲鑳介殢鐫€绠楁硶鍩虹璇剧殑寮€灞曪紝鎴戣繕瑕佹洿鏂颁竴娉㈠崥瀹€傚簾璇濅笉澶氳銆?/p>
銆?1鑳屽寘銆?/h3>
鏈?N 浠剁墿鍝佸拰涓€涓閲忔槸 V 鐨勮儗鍖呫€傛瘡浠剁墿鍝佸彧鑳戒娇鐢ㄤ竴娆°€?
绗?i 浠剁墿鍝佺殑浣撶Н鏄?vi锛屼环鍊兼槸 wi銆?
姹傝В灏嗗摢浜涚墿鍝佽鍏ヨ儗鍖咃紝鍙娇杩欎簺鐗╁搧鐨勬€讳綋绉笉瓒呰繃鑳屽寘瀹归噺锛屼笖鎬讳环鍊兼渶澶с€?
杈撳嚭鏈€澶т环鍊笺€?/span>
鏈?N 浠剁墿鍝佸拰涓€涓閲忔槸 V 鐨勮儗鍖呫€傛瘡浠剁墿鍝佸彧鑳戒娇鐢ㄤ竴娆°€?
绗?i 浠剁墿鍝佺殑浣撶Н鏄?vi锛屼环鍊兼槸 wi銆?
姹傝В灏嗗摢浜涚墿鍝佽鍏ヨ儗鍖咃紝鍙娇杩欎簺鐗╁搧鐨勬€讳綋绉笉瓒呰繃鑳屽寘瀹归噺锛屼笖鎬讳环鍊兼渶澶с€?
杈撳嚭鏈€澶т环鍊笺€?/span>
01鑳屽寘锛歠[i][j] 锛氬墠i涓墿鍝侊紝鍏跺閲忎负j鏃剁殑鏈€澶т环鍊?nbsp;
for ( 1~n ):銆€銆€銆€銆€渚濇鏋氫妇鎵€鏈夌墿鍝? for ( 0 ~ m ):銆€銆€銆€銆€鏋氫妇鑳屽寘鐨勫閲? if ( j >= v[i] ):銆€銆€銆€銆€濡傛灉褰撳墠鐨勭墿鍝佽兘鏀捐繘鑳屽寘 f[i][j] = max( f[i-1][j] , f[i-1][j-v[i]]+w[i]) 涓嶉€? 閫?/span>
銆€銆€
銆€銆€
鍚屾椂杩欓噷鏈変竴涓┖闂翠笂鐨勪紭鍖栵紝涓昏鏄熀浜庢瘡娆$殑鐗╁搧鏀惧畬鍚庯紝鍙湁瀵瑰簲鐨勫唴瀹硅繘琛屾洿鏂帮紝濡傛灉鏀句笅涓€涓墿鍝佺殑鏃跺€欙紝鍏跺疄鍙渶瑕佹洿鏂颁笂涓€涓姸鎬佸嵆鍙€?/p>
01鑳屽寘锛屽彧鍏佽鏀句竴娆★紝鎵€浠ユ洿鏂扮殑鏃跺€欒繕闇€瑕?nbsp; for寰幆鏋氫妇鐨勯『搴忚繕闇€瑕佹敞鎰忥細
for( int j = V ; j >= v[i] ; j-- ) 銆€銆€f[j] = max( f[j] , f[j-v[i]] + w[i] ) 銆€銆€銆€銆€銆€銆€銆€銆€璇ョ姸鎬?绗琲涓墿鍝佽繕娌℃湁琚鐩栵紝 銆€銆€銆€銆€銆€∴ f[j-v[i]]瀹為檯灏辨槸鎸囩i-1涓墿鍝佹椂鐨勭姸鎬? 銆€銆€銆€銆€銆€∴ 杩欓噷鍏跺疄 浠庡悗寰€鍓嶅巻閬嶏紝姣忔鍙闂竴鍥烇紝鑰屼笖鏄熀浜庡墠i-1涓墿鍝佹洿鏂板悗鐨勫€硷紝鎵€浠ョ瓑浠蜂簬鍙槸鏀句簡涓€閬?/pre>
01鑳屽寘妯℃澘锛?/h2>
1 #include<iostream>
2 using namespace std;
3
4 const int N = 1e3+5;
5
6 int f[N],v[N],w[N];
7 int n,m;
8
9 int main()
10 {
11 cin >> n >> m ;
12 for(int i=0;i<n;i++){
13 cin >> v[i] >> w [i];
14 }
15 for(int i=0;i<n;i++){
16 for(int j=m;j>=v[i];j--){
17 f[j] = max(f[j],f[j-v[i]]+w[i]);
18 }
19 }
20 cout << f[m] << endl;
21 }
1 #include<iostream> 2 using namespace std; 3 4 const int N = 1e3+5; 5 6 int f[N],v[N],w[N]; 7 int n,m; 8 9 int main() 10 { 11 cin >> n >> m ; 12 for(int i=0;i<n;i++){ 13 cin >> v[i] >> w [i]; 14 } 15 for(int i=0;i<n;i++){ 16 for(int j=m;j>=v[i];j--){ 17 f[j] = max(f[j],f[j-v[i]]+w[i]); 18 } 19 } 20 cout << f[m] << endl; 21 }
瑙傚療涓婇潰鐨勪唬鐮侊細
1銆佽闂负浠€涔?f[m]灏辨槸鏈€缁堢瓟妗堬紵锛堝彲鑳芥斁缃殑鐗╁搧瀹归噺杩樻病鏈夊埌杈緈锛?/h3>
鍘熷洜鏄細涓庡垵濮嬪寲鏈夊叧锛屽洜涓哄垵濮嬪寲涓?锛屼緥濡?nbsp; 鍦?瀹归噺涓簁鐨勬椂鍊欏氨宸茬粡鎶婅儗鍖呯殑浠峰€艰揪鍒版渶澶т簡锛?f[j] = max( f[j] , f[j-v[i]]+w[i] )
璇ヨ繃绋?k+1 浠?寮€濮嬫斁鐗╁搧锛屼絾鍏跺疄鏈€鍚庢斁鐨勭墿鍝佺殑绉嶇被涓巉[k]绛変环鐨勩€?/p>
銆€銆€k , k+1 , …… m
∴f[m]姝ゆ椂涓虹瓟妗堛€?/p>
2銆佽嫢鎯虫妸瀹归噺鎭板ソ涓簃鐨勬渶澶т环鍊硷紵
銆€銆€闇€瑕佸鍒濆鍖栧姩鎵嬭剼锛屽垵濮嬪寲f[0]=0,f[鍏朵粬] = -inf
瀹屽叏鑳屽寘
鏈?N 绉嶇墿鍝佸拰涓€涓閲忔槸 V 鐨勮儗鍖咃紝姣忕鐗╁搧閮芥湁鏃犻檺浠跺彲鐢ㄣ€?
绗?i 绉嶇墿鍝佺殑浣撶Н鏄?vi锛屼环鍊兼槸 wi銆?
姹傝В灏嗗摢浜涚墿鍝佽鍏ヨ儗鍖咃紝鍙娇杩欎簺鐗╁搧鐨勬€讳綋绉笉瓒呰繃鑳屽寘瀹归噺锛屼笖鎬讳环鍊兼渶澶с€?
杈撳嚭鏈€澶т环鍊笺€?/span>
f[i]琛ㄧず鎬讳綋绉负i 鏃剁殑鏈€澶т环鍊?/p>
鏈€鏈寸礌鐨勫仛娉曪細鍩轰簬01鑳屽寘鍋氭硶
for( i = 1 ; i <= n ; i++ )銆€銆€渚濇鏋氫妇鎵€鏈夌墿鍝? 銆€銆€for( j = V ; j >= v[i] ; j-- )銆€銆€鏋氫妇褰撳墠瀹归噺 銆€銆€銆€銆€for( k = 0 ; k*v[i] <= j ; k++ )銆€銆€鏋氫妇瀹归噺涓嬪彲浠ユ斁澶氬皯涓紝鑻ヨ兘鏀句笅鐨勫墠鎻愪笅鏇存柊 銆€銆€銆€銆€銆€銆€f[j] = max( f[j] , f[j-k*v[i]]+k*w[i] )銆€銆€
銆€銆€
浼樺寲鍚庯細
for( i = 1 ; i <= n ; i++ ) 銆€銆€for( j = v[i] ; j <= V ; j++ )銆€銆€//娉ㄦ剰鏋氫妇鐨勯『搴?/span> 銆€銆€銆€銆€f[j] = max( f[j] , f[j-v[i]]+w[i]) 銆€ 鍏跺疄鍦ㄥ綋鍓嶄綅缃殑j鐨勬椂鍊欙紝瀹归噺涓猴細j-v[i]锛屽彲鑳藉凡缁忓埄鐢ㄨ鐗╁搧杩涜鏇存柊浜嗐€? 銆€鏁堟灉涓猴細澶氭閫夋嫨璇ョ墿鍝併€?/span>
銆€銆€
璇佹槑锛?/p>
鏁板褰掔撼娉曪細 銆€銆€1銆佸亣璁捐€冭檻i-1涓墿鍝佸悗锛屾墍鏈夌殑f[j]閮芥槸姝g‘鐨? 銆€銆€2銆佹潵璇佹槑锛岃€冭檻绗琲涓墿鍝佸悗锛屾墍鏈夌殑f[j]涔熼兘鏄纭殑 銆€銆€瀵逛簬鏌愪釜j鑰岃█锛屾渶浼樿В鍖呭惈k涓獀[i] 銆€銆€f[j-k*v[i]] 銆€銆€f[j-(k-1)*v[i]]+w[i] 銆€銆€…………………… 銆€銆€f[j-v[i]] + w[i]
瀹屽叏鑳屽寘妯℃澘
1 #include<iostream> 2 using namespace std; 3 const int N = 1e3+10; 4 5 int f[N],v[N],w[N]; 6 7 int n,m; 8 9 int main() 10 { 11 cin >> n >> m ; 12 for(int i=0;i<n;i++){ 13 cin >> v[i] >> w[i] ; 14 } 15 for(int i=0;i<n;i++){ 16 for(int j=v[i];j<=m;j++){ 17 f[j] = max( f[j] , f[j-v[i]]+w[i]); 18 } 19 } 20 cout << f[m] << endl; 21 return 0; 22 }
澶氶噸鑳屽寘
鏈?N 绉嶇墿鍝佸拰涓€涓閲忔槸 V 鐨勮儗鍖呫€?
绗?i 绉嶇墿鍝佹渶澶氭湁 si 浠讹紝姣忎欢浣撶Н鏄?vi锛屼环鍊兼槸 wi銆?
姹傝В灏嗗摢浜涚墿鍝佽鍏ヨ儗鍖咃紝鍙娇鐗╁搧浣撶Н鎬诲拰涓嶈秴杩囪儗鍖呭閲忥紝涓斾环鍊兼€诲拰鏈€澶с€?
杈撳嚭鏈€澶т环鍊笺€?/span>
鏈寸礌鍋氭硶锛?/p>
for(int i=1;i<=n;i++)//鏋氫妇鐗╁搧 for(int j=V;j>=0;j--)//鏋氫妇浣撶Н for(int k=1;k<=num[i],k++) //杩欎釜鏋氫妇鍒皀um[i] if(j-k*c[i]>=0)//鍒ゆ柇鑳藉惁瑁呬笅. f[j]=max(f[j],f[j-k*c[i]]+k*w[i]);
銆€銆€
浜岃繘鍒朵紭鍖?/h3>
浜岃繘鍒舵媶鍒嗙殑鍘熺悊
鎴戜滑鍙互鐢?1,2,4,8...2^n 琛ㄧず鍑?1 鍒?2^{n+1}-1鐨勬墍鏈夋暟.
鑰冭檻鎴戜滑鐨勪簩杩涘埗琛ㄧず涓€涓暟銆?
鏍规嵁绛夋瘮鏁板垪姹傚拰,鎴戜滑寰堝鏄撶煡閬撴垜浠緱鍒扮殑鏁版渶澶у氨鏄?2^{n+1}-1
鑰屾垜浠煇涓€涓暟鐢ㄤ簩杩涘埗鏉ヨ〃绀虹殑璇?姣忎竴浣嶄笂浠h〃鐨勬暟閮芥槸 2 鐨勬骞?
灏辫繛濂囨暟涔熷彲浠?渚嬪-> 19 鍙互琛ㄧず涓?10011 (2)
?
浜岃繘鍒舵媶鍒嗙殑鍋氭硶
鍥犱负鎴戜滑鐨勪簩杩涘埗琛ㄧず娉曞彲浠ヨ〃绀轰粠 1 鍒?num[i] 鐨勬墍鏈夋暟,鎴戜滑瀵瑰叾杩涜鎷嗗垎,灏卞緱鍒板ソ澶氫釜澶х墿鍝?杩欓噷鐨勫ぇ鐗╁搧浠h〃澶氫釜杩欐牱鐨勭墿鍝佹墦鍖呭緱鍒扮殑涓€涓ぇ鐗╁搧).
(绠€鍗曟潵璁?鎴戜滑鍙互鐢ㄤ竴涓ぇ鐗╁搧浠h〃 1,2,4,8.. 浠剁墿鍝佺殑鍜屻€?
鑰岃繖浜涘ぇ鐗╁搧鍙堝彲浠ユ牴鎹笂闈㈢殑鍘熺悊琛ㄧず鍑哄叾浠栦笉鏄?鐨勬骞傜殑鐗╁搧鐨勫拰.
鍥犳杩欐牱鐨勫仛娉曟槸鍙鐨?
鎴戜滑鍙堝緱鍒颁簡澶氫釜澶х墿鍝?鎵€浠ュ啀鍘昏窇01鑳屽寘鍗冲彲.
浜岃繘鍒舵媶鍒嗙殑鍘熺悊 鎴戜滑鍙互鐢?1,2,4,8...2^n 琛ㄧず鍑?1 鍒?2^{n+1}-1鐨勬墍鏈夋暟. 鑰冭檻鎴戜滑鐨勪簩杩涘埗琛ㄧず涓€涓暟銆? 鏍规嵁绛夋瘮鏁板垪姹傚拰,鎴戜滑寰堝鏄撶煡閬撴垜浠緱鍒扮殑鏁版渶澶у氨鏄?2^{n+1}-1 鑰屾垜浠煇涓€涓暟鐢ㄤ簩杩涘埗鏉ヨ〃绀虹殑璇?姣忎竴浣嶄笂浠h〃鐨勬暟閮芥槸 2 鐨勬骞? 灏辫繛濂囨暟涔熷彲浠?渚嬪-> 19 鍙互琛ㄧず涓?10011 (2) ? 浜岃繘鍒舵媶鍒嗙殑鍋氭硶 鍥犱负鎴戜滑鐨勪簩杩涘埗琛ㄧず娉曞彲浠ヨ〃绀轰粠 1 鍒?num[i] 鐨勬墍鏈夋暟,鎴戜滑瀵瑰叾杩涜鎷嗗垎,灏卞緱鍒板ソ澶氫釜澶х墿鍝?杩欓噷鐨勫ぇ鐗╁搧浠h〃澶氫釜杩欐牱鐨勭墿鍝佹墦鍖呭緱鍒扮殑涓€涓ぇ鐗╁搧). (绠€鍗曟潵璁?鎴戜滑鍙互鐢ㄤ竴涓ぇ鐗╁搧浠h〃 1,2,4,8.. 浠剁墿鍝佺殑鍜屻€? 鑰岃繖浜涘ぇ鐗╁搧鍙堝彲浠ユ牴鎹笂闈㈢殑鍘熺悊琛ㄧず鍑哄叾浠栦笉鏄?鐨勬骞傜殑鐗╁搧鐨勫拰. 鍥犳杩欐牱鐨勫仛娉曟槸鍙鐨? 鎴戜滑鍙堝緱鍒颁簡澶氫釜澶х墿鍝?鎵€浠ュ啀鍘昏窇01鑳屽寘鍗冲彲.
銆€銆€
浜岃繘鍒朵紭鍖栫殑妯℃澘锛?/h3>
1 #include<iostream>
2 using namespace std;
3
4 const int N = 2e3+10;
5
6 int f[N],v[N],w[N],s[N];
7
8 int n,m;
9
10 int main()
11 {
12 cin >> n >> m;
13 for(int i=0;i<n;i++){
14 cin >> v[i] >> w[i] >> s[i] ;
15 }
16 for(int i=0;i<n;i++){
17
18 for(int k=0 ; (1<<k) <= s[i] ; k++ ){
19 int K = (1<<k);
20 for(int j=m ; j>=K*v[i] ; j-- ){
21 f[j] = max( f[j] , f[j-K*v[i]] + K*w[i] );
22 }
23 s[i] = s[i] - (1<<k);
24 }
25 for(int j=m ; j >= s[i]*v[i] ; j-- ){
26 f[j] = max( f[j] , f[j-s[i]*v[i]] + s[i]*w[i] );
27 }
28 }
29 cout << f[m] << endl ;
30 return 0 ;
31 }
1 #include<iostream> 2 using namespace std; 3 4 const int N = 2e3+10; 5 6 int f[N],v[N],w[N],s[N]; 7 8 int n,m; 9 10 int main() 11 { 12 cin >> n >> m; 13 for(int i=0;i<n;i++){ 14 cin >> v[i] >> w[i] >> s[i] ; 15 } 16 for(int i=0;i<n;i++){ 17 18 for(int k=0 ; (1<<k) <= s[i] ; k++ ){ 19 int K = (1<<k); 20 for(int j=m ; j>=K*v[i] ; j-- ){ 21 f[j] = max( f[j] , f[j-K*v[i]] + K*w[i] ); 22 } 23 s[i] = s[i] - (1<<k); 24 } 25 for(int j=m ; j >= s[i]*v[i] ; j-- ){ 26 f[j] = max( f[j] , f[j-s[i]*v[i]] + s[i]*w[i] ); 27 } 28 } 29 cout << f[m] << endl ; 30 return 0 ; 31 }
鍗曡皟闃熷垪浼樺寲锛?nbsp;
渚嬪锛氱墿鍝佺殑瀹归噺涓?: 瀹规槗鍙戠幇鐨勬槸,鍚屼竴棰滆壊鐨勬牸瀛?瀵?c[i] 鍙栨ā寰楀埌鐨勪綑鏁扮浉鍚? 涓?瀹冧滑鐨勫樊婊¤冻绛夊樊鏁板垪! (鍏樊涓?c[i] . 閫氶」鍏紡涓?j=k∗c[i]+ 鍙栨ā寰楀埌鐨勪綑鏁? 鎵€浠ユ垜浠彲浠ユ牴鎹 c[i] 鍙栨ā寰楀埌鐨勪綑鏁拌繘琛屽垎缁? 鍗冲彲鍒嗕负 0,1,2,3…c[i]−1 鍏?c[i] 缁? 涓旀瘡缁勪箣闂寸殑鐘舵€佽浆绉讳笉浜掔浉褰卞搷.(娉ㄦ剰杩欓噷鏄粍.鐩稿悓棰滆壊涓轰竴缁? 鐩稿悓棰滆壊鐨勬牸瀛?浣嶇疆闈犲悗鐨勬牸瀛?灏嗗彈鍒颁綅缃潬鍓嶆牸瀛愮殑褰卞搷. 鍗?f[9] 鍙兘鍙楀埌 f[5] 鐨勫奖鍝?涔熷彲鑳藉彈鍒?f[1] 鐨勫奖鍝? 鑰?f[5] 涔熷彲鑳藉彈鍒?f[1] 鐨勫奖鍝? 鎵€浠ユ垜浠€冭檻灏嗗師濮嬬姸鎬佽浆绉绘柟绋嬪彉褰?
璁撅細d = v[i] 锛? 銆€銆€a = j / d 锛? 銆€銆€b = j % d 锛? ∴銆€j = a * d + b 銆€銆€銆€銆€鍟? 闄ゆ暟 + 浣欐暟 鍘熷鐨勫紡瀛愶細 銆€銆€f[i][j]=max(f[i-1][j],f[j-k*v[i]]+k*w[i]); 杞寲闇€瑕佺敤鐨勫紡瀛愭湁锛? 1銆乯 - k * d = ( a - k ) * d + b 2銆乤 - k鈥?/span> = k 鎺ㄥ杩囩▼涓猴細 瀵?j - k * d 杩涜澶勭悊锛? 銆€ j 鍒╃敤 鍋忕Щ閲忔潵琛ㄧず锛? 銆€ j - k * d = a * d + b - k * d = ( a - k ) * d + b 鍏朵腑浠鈥?/span> = a - k a - ( a - k ) = k a - k鈥?/span> = k 杞寲杩囩▼濡備笅锛? f[i][j] = max( f[i-1][j-k*d]+k*w[i] ) 銆€銆€銆€銆€ = max( f[i-1][k`× d + b] + (a - k`) × w[i] ) 銆€銆€銆€銆€ = max( f[i-1][k`× d + b] - k` × w[i] ) + a × w[i] (甯告暟鍙互鎻愬埌澶栭潰)
鍗曡皟闃熷垪妯℃澘
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 const int N = 2e4+10; 6 typedef struct Node { 7 int id , val ; 8 }node ; 9 10 Node Q[N]; 11 int n , m , v , w , s ; 12 int f[N]; 13 14 int main() 15 { 16 cin >> n >> m ; 17 for( int i = 0 ; i < n ; i++ ){ 18 cin >> v >> w >> s ; 19 for( int j = 0 ; j < v ; j++ ){銆€銆€//鏋氫妇浣欐暟 20 int head = 1 , tail = 0 ; 21 for( int k = j , d = 0 ; k <= m ; k += v , d++ ){銆€銆€ 22 int r = f[k] - d * w ; 23 while ( head <= tail && r >= Q[tail].val ) tail -- ; 24 Q[++tail] = node{ d , r }; 25 while ( Q[head].id < d - s ) head ++ ; 26 f[k] = Q[head].val + d * w ; 27 } 28 } 29 } 30 cout << f[m] << endl; 31 return 0; 32 }
娣峰悎鑳屽寘
鏈?N 绉嶇墿鍝佸拰涓€涓閲忔槸 V 鐨勮儗鍖呫€?
鐗╁搧涓€鍏辨湁涓夌被锛?
绗竴绫荤墿鍝佸彧鑳界敤1娆★紙01鑳屽寘锛夛紱
绗簩绫荤墿鍝佸彲浠ョ敤鏃犻檺娆★紙瀹屽叏鑳屽寘锛夛紱
绗笁绫荤墿鍝佹渶澶氬彧鑳界敤 si 娆★紙澶氶噸鑳屽寘锛夛紱
姣忕浣撶Н鏄?vi锛屼环鍊兼槸 wi銆?
姹傝В灏嗗摢浜涚墿鍝佽鍏ヨ儗鍖咃紝鍙娇鐗╁搧浣撶Н鎬诲拰涓嶈秴杩囪儗鍖呭閲忥紝涓斾环鍊兼€诲拰鏈€澶с€?
杈撳嚭鏈€澶т环鍊笺€?/span>
杈撳叆鏍煎紡 銆€銆€绗竴琛屼袱涓暣鏁帮紝N锛孷锛岀敤绌烘牸闅斿紑锛屽垎鍒〃绀虹墿鍝佺鏁板拰鑳屽寘瀹圭Н銆? 銆€銆€鎺ヤ笅鏉ユ湁 N 琛岋紝姣忚涓変釜鏁存暟 vi,wi,si锛岀敤绌烘牸闅斿紑锛屽垎鍒〃绀虹 i 绉嶇墿鍝佺殑浣撶Н銆佷环鍊煎拰鏁伴噺銆? 銆€銆€si=−1 琛ㄧず绗?i 绉嶇墿鍝佸彧鑳界敤1娆★紱 銆€銆€si=0 琛ㄧず绗?i 绉嶇墿鍝佸彲浠ョ敤鏃犻檺娆★紱 銆€銆€si>0 琛ㄧず绗?i 绉嶇墿鍝佸彲浠ヤ娇鐢?si 娆★紱 杈撳嚭鏍煎紡 銆€銆€杈撳嚭涓€涓暣鏁帮紝琛ㄧず鏈€澶т环鍊笺€? 鏁版嵁鑼冨洿 銆€銆€0<N,V≤1000 銆€銆€0<vi,wi≤1000 銆€銆€−1≤si≤1000
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 const int N = 1e3+5; 6 7 int f[N],n,m; 8 typedef struct Good{ 9 int s,v,w; 10 }Node; 11 12 Node a[N]; 13 int main(){ 14 scanf("%d%d",&n,&m); 15 16 for(int i=0;i<n;i++) scanf("%d%d%d",&a[i].v,&a[i].w,&a[i].s); 17 for(int i=0;i<n;i++){ 18 if( a[i].s == -1 ){ 19 for(int j=m;j>=a[i].v;j--) 20 f[j] = max( f[j] , f[j-a[i].v] +a[i].w ); 21 }else if( a[i].s == 0 ){ 22 for(int j=a[i].v;j<=m;j++) 23 f[j] = max( f[j] , f[j-a[i].v] +a[i].w ); 24 }else{ 25 for(int k=1 ; k <= a[i].s ; k<<=1 ){ 26 a[i].s -= k ; 27 for(int j=m ; j>=k*a[i].v ; j--) 28 f[j] = max( f[j] , f[j-k*a[i].v] + k*a[i].w ); 29 } 30 if( a[i].s ){ 31 for(int j=m ; j>=a[i].s*a[i].v ; j-- ){ 32 f[j] = max( f[j] , f[j-a[i].s*a[i].v] + a[i].s*a[i].w ); 33 } 34 } 35 } 36 } 37 printf("%d ",f[m]); 38 return 0 ; 39 }
浜岀淮璐圭敤鐨勮儗鍖呴棶棰?/h2>
鏈?N 浠剁墿鍝佸拰涓€涓閲忔槸 V 鐨勮儗鍖咃紝鑳屽寘鑳芥壙鍙楃殑鏈€澶ч噸閲忔槸 M銆?
姣忎欢鐗╁搧鍙兘鐢ㄤ竴娆°€備綋绉槸 vi锛岄噸閲忔槸 mi锛屼环鍊兼槸 wi銆?
姹傝В灏嗗摢浜涚墿鍝佽鍏ヨ儗鍖咃紝鍙娇鐗╁搧鎬讳綋绉笉瓒呰繃鑳屽寘瀹归噺锛屾€婚噸閲忎笉瓒呰繃鑳屽寘鍙壙鍙楃殑鏈€澶ч噸閲忥紝涓斾环鍊兼€诲拰鏈€澶с€?
杈撳嚭鏈€澶т环鍊笺€?/span>
鏈?N 浠剁墿鍝佸拰涓€涓閲忔槸 V 鐨勮儗鍖咃紝鑳屽寘鑳芥壙鍙楃殑鏈€澶ч噸閲忔槸 M銆?
姣忎欢鐗╁搧鍙兘鐢ㄤ竴娆°€備綋绉槸 vi锛岄噸閲忔槸 mi锛屼环鍊兼槸 wi銆?
姹傝В灏嗗摢浜涚墿鍝佽鍏ヨ儗鍖咃紝鍙娇鐗╁搧鎬讳綋绉笉瓒呰繃鑳屽寘瀹归噺锛屾€婚噸閲忎笉瓒呰繃鑳屽寘鍙壙鍙楃殑鏈€澶ч噸閲忥紝涓斾环鍊兼€诲拰鏈€澶с€?
杈撳嚭鏈€澶т环鍊笺€?/span>
銆€銆€
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 const int N = 1e3+5; 6 const int M_ = 1e2+5; 7 8 int f[M_][M_]; 9 10 int n,V,M; 11 int v,m,w; 12 int main() 13 { 14 cin >> n >> V >> M ; 15 16 for(int i=0;i<n;i++){ 17 18 cin >> v >> m >> w ; 19 20 for(int j=V ; j>=v ; j-- ){ //浣撶Н 21 for(int k=M ; k>=m ; k-- ){ //璐ㄩ噺 22 f[j][k] = max( f[j][k] , f[j-v][k-m] + w ) ; 23 } 24 } 25 } 26 cout << f[V][M] << endl ; 27 return 0 ; 28 }
鍒嗙粍鑳屽寘闂
鏈?N 缁勭墿鍝佸拰涓€涓閲忔槸 V 鐨勮儗鍖呫€?
姣忕粍鐗╁搧鏈夎嫢骞蹭釜锛屽悓涓€缁勫唴鐨勭墿鍝佹渶澶氬彧鑳介€変竴涓€?
姣忎欢鐗╁搧鐨勪綋绉槸 vij锛屼环鍊兼槸 wij锛屽叾涓?i 鏄粍鍙凤紝j 鏄粍鍐呯紪鍙枫€?
姹傝В灏嗗摢浜涚墿鍝佽鍏ヨ儗鍖咃紝鍙娇鐗╁搧鎬讳綋绉笉瓒呰繃鑳屽寘瀹归噺锛屼笖鎬讳环鍊兼渶澶с€?
杈撳嚭鏈€澶т环鍊笺€?/span>
1 #include<iostream> 2 #include<algorithm> 3 4 using namespace std; 5 const int N = 1e2 + 5 ; 6 7 int n,m,s; 8 int v[N],w[N],f[N]; 9 10 int main(){ 11 cin >> n >> m ; 12 for(int i=0;i<n;i++){ 13 14 cin >> s ; 15 for(int j = 0 ; j < s ; j++ ) cin >> v[j] >> w[j] ; 16 for(int j = m ; j >= 0; j-- ){ 17 for(int k = 0 ; k < s ; k++ ){ 18 if( j >= v[k] ){ 19 f[j] = max( f[j] , f[j-v[k]]+w[k] ); 20 } 21 } 22 } 23 } 24 cout << f[m] << endl; 25 return 0; 26 }
鏈変緷璧栫殑鑳屽寘闂
鏈?N 涓墿鍝佸拰涓€涓閲忔槸 V 鐨勮儗鍖呫€? 鐗╁搧涔嬮棿鍏锋湁渚濊禆鍏崇郴锛屼笖渚濊禆鍏崇郴缁勬垚涓€妫垫爲鐨勫舰鐘躲€傚鏋滈€夋嫨涓€涓墿鍝侊紝鍒欏繀椤婚€夋嫨瀹冪殑鐖惰妭鐐广€? 濡備笅鍥炬墍绀猴細
濡傛灉閫夋嫨鐗╁搧5锛屽垯蹇呴』閫夋嫨鐗╁搧1鍜?銆傝繖鏄洜涓?鏄?鐨勭埗鑺傜偣锛?鏄?鐨勭埗鑺傜偣銆? 姣忎欢鐗╁搧鐨勭紪鍙锋槸 i锛屼綋绉槸 vi锛屼环鍊兼槸 wi锛屼緷璧栫殑鐖惰妭鐐圭紪鍙锋槸 pi銆傜墿鍝佺殑涓嬫爣鑼冨洿鏄?1…N銆? 姹傝В灏嗗摢浜涚墿鍝佽鍏ヨ儗鍖咃紝鍙娇鐗╁搧鎬讳綋绉笉瓒呰繃鑳屽寘瀹归噺锛屼笖鎬讳环鍊兼渶澶с€? 杈撳嚭鏈€澶т环鍊笺€?/span>
1 #include<cstring> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 6 const int N = 105; 7 8 int head[N] , To[N] , nxt[N] , idx ; 9 int f[N][N] , v[N] , w[N] ; 10 int n , m , p , root ; 11 12 void Add_edge( int u , int v ){ 13 nxt[++idx] = head[u] ; 14 head[u] = idx; 15 To[idx] = v ; 16 } 17 18 void dfs( int u ){ 19 20 for( int i = head[u] ; ~i ; i = nxt[i] ){ 21 int Son = To[i] ; 22 dfs(Son) ; 23 for( int j = m - v[u] ; j >= 0 ; j-- ){ 24 for( int k = 0 ; k <= j ; k++ ){ 25 f[u][j] = max( f[u][j] , f[u][j-k] + f[Son][k] ); 26 } 27 } 28 } 29 for( int i = m ; i >= v[u] ; i-- ) f[u][i] = f[u][i-v[u]] + w[u] ; 30 for( int i = 0 ; i < v[u] ; i++ ) f[u][i] = 0; 31 } 32 int main() 33 { 34 memset( head , -1 , sizeof head ); 35 36 cin >> n >> m ; 37 for( int i = 1 ; i <= n ; i++ ){ 38 cin >> v[i] >> w[i] >> p ; 39 if( p == -1 ) root = i ; 40 else{ 41 Add_edge( p , i ); 42 } 43 } 44 dfs(root); 45 printf("%d ",f[root][m]); 46 return 0 ; 47 }
鑳屽寘闂姹傛柟妗堟暟
鏈?N 浠剁墿鍝佸拰涓€涓閲忔槸 V 鐨勮儗鍖呫€傛瘡浠剁墿鍝佸彧鑳戒娇鐢ㄤ竴娆°€? 绗?i 浠剁墿鍝佺殑浣撶Н鏄?vi锛屼环鍊兼槸 wi銆? 姹傝В灏嗗摢浜涚墿鍝佽鍏ヨ儗鍖咃紝鍙娇杩欎簺鐗╁搧鐨勬€讳綋绉笉瓒呰繃鑳屽寘瀹归噺锛屼笖鎬讳环鍊兼渶澶с€? 杈撳嚭 鏈€浼橀€夋硶鐨勬柟妗堟暟銆傛敞鎰忕瓟妗堝彲鑳藉緢澶э紝璇疯緭鍑虹瓟妗堟ā 1e9+7 鐨勭粨鏋溿€?/pre>
銆€銆€
1 #include<cstring> 2 #include<iostream> 3 #include<algorithm> 4 5 using namespace std; 6 const int N = 1e3+5 ; 7 const int Inf = 2e6 ; 8 const int Mod = 1e9+7 ; 9 10 int n,m,w,v; 11 12 int f[N] ; 13 int Way[N] ; 14 15 int main() 16 { 17 cin >> n >> m ; 18 19 //Initization 20 Way[0] = 1 ; f[0] = 0 ; 21 for(int i = 1 ; i <= m ; i++ ){ 22 f[i] = -Inf ; 23 } 24 25 for( int i = 0 ; i<n ; i++ ){ 26 cin >> v >> w ; 27 for( int j = m ; j >= v ; j-- ){ 28 int tmp = f[j] , Path = 0 ; 29 f[j] = max( f[j] , f[j-v] + w ); 30 if( f[j] == tmp ) Path += Way[j] ; 31 if( f[j] == f[j-v] + w ) Path += Way[j-v]; 32 if( Path >= Mod ) Path -= Mod ; 33 Way[j] = Path ; 34 } 35 } 36 37 int Maxz = -1 , ans = 0 ; 38 for( int i = 1 ; i <= m ; i++ ) 39 Maxz = max( Maxz , f[i] ); 40 for( int i = 0 ; i <= m ;i++ ){ 41 if( Maxz == f[i] ){ 42 ans = ans + Way[i] ; 43 if( ans >= Mod ) 44 ans -= Mod ; 45 } 46 } 47 48 cout << ans << endl ; 49 return 0 ; 50 }
鑳屽寘闂姹傚叿浣撴柟妗?/h2>
鏈?N 浠剁墿鍝佸拰涓€涓閲忔槸 V 鐨勮儗鍖呫€傛瘡浠剁墿鍝佸彧鑳戒娇鐢ㄤ竴娆°€?
绗?i 浠剁墿鍝佺殑浣撶Н鏄?vi锛屼环鍊兼槸 wi銆?
姹傝В灏嗗摢浜涚墿鍝佽鍏ヨ儗鍖咃紝鍙娇杩欎簺鐗╁搧鐨勬€讳綋绉笉瓒呰繃鑳屽寘瀹归噺锛屼笖鎬讳环鍊兼渶澶с€?
杈撳嚭 瀛楀吀搴忔渶灏忕殑鏂规銆傝繖閲岀殑瀛楀吀搴忔槸鎸囷細鎵€閫夌墿鍝佺殑缂栧彿鎵€鏋勬垚鐨勫簭鍒椼€傜墿鍝佺殑缂栧彿鑼冨洿鏄?1…N銆?/pre>
鏈?N 浠剁墿鍝佸拰涓€涓閲忔槸 V 鐨勮儗鍖呫€傛瘡浠剁墿鍝佸彧鑳戒娇鐢ㄤ竴娆°€? 绗?i 浠剁墿鍝佺殑浣撶Н鏄?vi锛屼环鍊兼槸 wi銆? 姹傝В灏嗗摢浜涚墿鍝佽鍏ヨ儗鍖咃紝鍙娇杩欎簺鐗╁搧鐨勬€讳綋绉笉瓒呰繃鑳屽寘瀹归噺锛屼笖鎬讳环鍊兼渶澶с€? 杈撳嚭 瀛楀吀搴忔渶灏忕殑鏂规銆傝繖閲岀殑瀛楀吀搴忔槸鎸囷細鎵€閫夌墿鍝佺殑缂栧彿鎵€鏋勬垚鐨勫簭鍒椼€傜墿鍝佺殑缂栧彿鑼冨洿鏄?1…N銆?/pre>
1 #include<iostream> 2 #include<algorithm> 3 4 using namespace std; 5 const int N = 1010; 6 7 int v[N],w[N]; 8 int f[N][N]; 9 int n,m; 10 11 int main() 12 { 13 cin >> n >> m ; 14 15 for( int i = 1 ; i<=n ; i++ ) 16 cin >> v[i] >> w[i] ; 17 18 for( int i = n ; i>=1 ; i-- ){ 19 for( int j = 0 ; j <= m ;j++ ){ 20 f[i][j] = f[ i+1 ][j] ; 21 if( j >= v[i] ) f[i][j] = max( f[i][j] , f[i+1][j-v[i]] + w[i] ); 22 } 23 } 24 25 int vol = m ; 26 for(int i=1;i<=n && vol > 0 ;i++){ 27 if( vol >= v[i] && f[i][vol] == f[ i+1 ][ vol-v[i] ] + w[i] ){ 28 cout << i << " " ; 29 vol -= v[i] ; 30 } 31 } 32 return 0 ; 33 }
2銆?1鑳屽寘闂
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 const int N = 5e4+10; 6 int f[N]; 7 int main() 8 { 9 int V,n; 10 scanf("%d%d",&V,&n); 11 for(int i=1,v;i<=n;i++){ 12 scanf("%d",&v); 13 for(int j=V;j>=v;j--){ 14 f[j] = max( f[j-v]+v , f[j] ); 15 } 16 } 17 printf("%d ",f[V]); 18 return 0; 19 }
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 1e5+10; 4 int f[N]; 5 int n,v,w,V; 6 int main() 7 { 8 ios_base :: sync_with_stdio(false); 9 cin.tie(NULL) , cout.tie(NULL) ; 10 cin >> V >> n ; 11 for( int i = 1 ; i <= n ; i++ ){ 12 cin >> v >> w ; 13 for( int j = v ; j <= V ; j ++ ){ 14 f[j] = max( f[j-v]+w , f[j] ); 15 } 16 } 17 cout << f[V] << endl; 18 return 0; 19 }
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 5e3+10; 4 typedef struct Node{ 5 int v , p , val ; 6 }Node ; 7 Node a[N]; 8 9 bool cmp ( Node u , Node v ){ 10 return u.p - u.v < v.p - v.v ; 11 } 12 13 int f[N]; 14 int n , V ; 15 int main(){ 16 17 while( scanf("%d%d",&n,&V)!=EOF ){ 18 for( int i = 0 ; i <= V ; i++ ) f[i] = 0 ; 19 for( int i = 0 ; i < n ; i++ ){ 20 scanf("%d%d%d",&a[i].v , & a[i].p , & a[i].val ); 21 } 22 sort( a , a + n , cmp ); 23 for( int i = 0 ; i < n ; i++ ){ 24 for( int j = V ; j >= a[i].p && j >= a[i].v ; j-- ){ 25 f[j] = max( f[j] , f[j-a[i].v] + a[i].val ); 26 } 27 } 28 printf("%d ",f[V]); 29 } 30 return 0 ; 31 }
3銆佸畬鍏ㄨ儗鍖呴棶棰?/h3>
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int inf = 0x3f3f3f3f; 4 const int N = 1e4+10; 5 int f[N],n; 6 7 int main() 8 { 9 int T,E,F,V; 10 scanf("%d",&T); 11 while(T--){ 12 scanf("%d%d",&E,&F); 13 V = F - E ; 14 15 for( int i = 1 ; i <= V ; i++ ) f[i] = inf ; 16 17 scanf("%d",&n); 18 for( int i=0,v,w;i<n;i++){ 19 scanf("%d%d",&w,&v); 20 for(int j = v ; j <= V; j++ ){ 21 f[j] = min( f[j], f[j-v]+w ); 22 } 23 } 24 25 if( f[V] == inf ){ 26 printf("This is impossible. "); 27 }else{ 28 printf("The minimum amount of money in the piggy-bank is %d. ",f[V]); 29 } 30 31 } 32 return 0; 33 }
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 1e7+10 ; 4 int f[N],w[N],v[N]; 5 int S , n , D , V; 6 int main() 7 { 8 9 cin >> V >> n >> D ; 10 for( int i = 0 ; i < D ; i++ ){ 11 cin >> v[i] >> w[i] ; 12 } 13 //cout << "###" << endl; 14 for( int i = 0 ; i < n ; i++ ){ 15 for( int d = 0 ; d < D ; d++ ){ 16 for( int j = v[d] ; j <= V ; j++ ){ 17 f[j] = max( f[j] , f[j-v[d]] + w[d] ); 18 //cout << f[j] << endl; 19 } 20 } 21 V = V + f[V]; 22 } 23 cout << V + f[V]<< endl; 24 return 0; 25 }
4銆佸閲嶈儗鍖呴棶棰?/h3>
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 120050 ; 4 int f[N]; 5 int a[7]; 6 int main() 7 { 8 int kase = 0 ; 9 while( 1 ){ 10 11 int sum = 0 ; 12 for( int i = 1 ; i <= 6 ; i++ ){ 13 scanf("%d",&a[i]); 14 //a[i] *= i ; 15 sum += i*a[i] ; 16 } 17 18 if( sum == 0 ) break ; 19 //if( kase ) puts(""); 20 if( sum & 1 ){ 21 printf("Collection #%d: Can鈥榯 be divided. ",++kase); 22 continue; 23 } 24 25 for( int i = 1 ; i <= sum/2 ; i++ ) f[i] = 0 ; 26 27 for( int i = 1 ; i <= 6 ; i++ ){ 28 for( int k = 0 ; 1 << k <= a[i] ; k++ ){ 29 int K = 1 << k ; 30 a[i] -= K ; 31 for( int j = sum/2 ; j >= K * i ; j -- ){ 32 f[j] = max( f[j] , f[j-K*i] + K*i ); 33 } 34 } 35 for( int j = sum/2 ; j >= a[i]*i ; j -- ){ 36 f[j] = max( f[j] , f[j-a[i]*i] + a[i]*i ); 37 } 38 } 39 /* 40 puts(" ##### "); 41 for( int i = 0 ; i <= sum/2 ;i++ ){ 42 printf("%d%c",f[i],i==sum/2?鈥榎n鈥?鈥?鈥?; 43 } 44 */ 45 if( f[sum/2] == sum/2) { 46 printf("Collection #%d: Can be divided. ",++kase); 47 }else{ 48 printf("Collection #%d: Can鈥榯 be divided. ",++kase); 49 } 50 } 51 return 0 ; 52 }
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 const int N = 2e5+10; 6 typedef struct Node { 7 int id , val ; 8 }node ; 9 10 Node Q[N]; 11 int n , m , v , w , s ; 12 int f[N]; 13 14 int main() 15 { 16 cin >> n >> m ; 17 for( int i = 0 ; i < n ; i++ ){ 18 cin >> w >> v >> s ; 19 for( int j = 0 ; j < v ; j++ ){ 20 int head = 1 , tail = 0 ; 21 for( int k = j , d = 0 ; k <= m ; k += v , d++ ){ 22 int r = f[k] - d * w ; 23 while ( head <= tail && r >= Q[tail].val ) tail -- ; 24 Q[++tail] = node{ d , r }; 25 while ( Q[head].id < d - s ) head ++ ; 26 f[k] = Q[head].val + d * w ; 27 } 28 } 29 } 30 cout << f[m] << endl; 31 return 0; 32 }
5銆佹贩鍚堣儗鍖呴棶棰?/h3>
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 6 const int N = 1e5+5; 7 8 int f[N],n,m; 9 typedef struct Good{ 10 int s,v,w; 11 }Node; 12 13 Node a[N]; 14 int main(){ 15 int T1,T2,M1,M2; 16 scanf("%d:%d %d:%d %d",&T1,&M1,&T2,&M2,&n); 17 18 m = T2 * 60 + M2 - ( T1 * 60 + M1 ); 19 20 for(int i=0;i<n;i++) scanf("%d%d%d",&a[i].v,&a[i].w,&a[i].s); 21 for(int i=0;i<n;i++){ 22 if( a[i].s == 1 ){ 23 for(int j=m;j>=a[i].v;j--) 24 f[j] = max( f[j] , f[j-a[i].v] +a[i].w ); 25 }else if( a[i].s == 0 ){ 26 for(int j=a[i].v;j<=m;j++) 27 f[j] = max( f[j] , f[j-a[i].v] +a[i].w ); 28 }else{ 29 for(int k=1 ; k <= a[i].s ; k<<=1 ){ 30 a[i].s -= k ; 31 for(int j=m ; j>=k*a[i].v ; j--) 32 f[j] = max( f[j] , f[j-k*a[i].v] + k*a[i].w ); 33 } 34 if( a[i].s ){ 35 for(int j=m ; j>=a[i].s*a[i].v ; j-- ){ 36 f[j] = max( f[j] , f[j-a[i].s*a[i].v] + a[i].s*a[i].w ); 37 } 38 } 39 } 40 } 41 printf("%d ",f[m]); 42 return 0 ; 43 }
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 120; 4 int f[N][N]; 5 int n,m,V,s; 6 int main() 7 { 8 while( ~scanf("%d%d",&n,&V) ){ 9 memset( f , 0 , sizeof(f) ); 10 for( int i = 1 ,v , w ; i <= n ; i++ ){ 11 scanf("%d%d",&m,&s); 12 13 if( s == 0 ){ 14 for( int p = 0 ; p <= V ; p++ ) 15 f[i][p] = -1 ; 16 }else{ 17 for( int p = 0 ; p <= V ; p++ ) 18 f[i][p] = f[i-1][p] ; 19 } 20 21 while(m--){ 22 scanf("%d%d",&v,&w); 23 for( int j = V ; j >= v ; j-- ){ 24 if( s == 0 ){ //鑷冲皯閫変竴涓?/span> 25 if( f[i][j-v] != -1 ) 26 f[i][j] = max( f[i][j] , f[i][j-v] + w ); 27 if( f[i-1][j-v] != -1 ) 28 f[i][j] = max( f[i][j] , f[i-1][j-v] + w ); 29 }else if( s == 1 ){ //鏈€澶氶€変竴涓?/span> 30 if( f[i-1][j] != -1 ) 31 f[i][j] = max( f[i][j] , f[i-1][j] ); 32 if( f[i-1][j-v] != -1 ) 33 f[i][j] = max( f[i][j] , f[i-1][j-v] + w ); 34 }else{ //闅忔満 35 36 if( f[i][j-v] != -1 ) 37 f[i][j] = max( f[i][j] , f[i][j-v] + w ) ; 38 if( f[i-1][j-v] != -1 ) 39 f[i][j] = max( f[i][j] , f[i-1][j-v] + w ); 40 41 //f[i][j] = max( f[i][j] , f[i-1][j-v] + w ); 42 } 43 } 44 } 45 } 46 printf("%d ",f[n][V]); 47 } 48 return 0 ; 49 }
6銆佷簩缁磋垂鐢ㄨ儗鍖呴棶棰?/h3>
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 450; 4 int f[N][N]; 5 int n,M,V; // Number , Weight , Volume 6 int v,m,c; // volume , weight , Calorie 7 8 int main() 9 { 10 cin >> V >> M ; 11 cin >> n ; 12 for( int i = 1 ; i <= n ; i++ ){ 13 cin >> v >> m >> c ; 14 for( int j = V ; j >=v ; j-- ){ 15 for ( int k = M ; k >= m ; k-- ){ 16 f[j][k] = max( f[j][k] , f[j-v][k-m] + c ); 17 } 18 } 19 } 20 cout << f[V][M] << endl; 21 return 0; 22 } 23 24 25 /* 26 320 350 27 4 28 160 40 120 29 80 110 240 30 220 70 310 31 40 400 220 32 */
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 1e2+10 ; 4 int target , V , n , M ; 5 int f[N] ; 6 int g[N] ; 7 int main() 8 { 9 while( ~scanf("%d%d%d%d",&target , &V , &n, &M ) ){ 10 memset( f , 128 , sizeof f ); 11 memset( g , 0 , sizeof g ); 12 f[0] = 0 ; 13 int v , w ; 14 while(n--){ 15 scanf("%d%d",&w,&v); 16 for(int j = v ; j <= V && g[j-v] + 1 <= M ; j++ ){ 17 if( f[j] < f[j-v] + w ){ 18 f[j] = f[j-v] + w ; 19 g[j] = g[j-v] + 1 ; 20 } 21 } 22 } 23 int ans = -1 ; 24 for( int i = 1 ; i <= V ; i++ ){ 25 if( f[i] >= target ){ 26 ans = V - i ; 27 break; 28 } 29 } 30 printf("%d ",ans); 31 } 32 return 0; 33 }
7銆佸垎缁勮儗鍖呴棶棰?/h3>
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 1e3 + 5 ; 4 5 int n,V ; 6 int f[N]; 7 8 typedef struct Node{ 9 int v , w ; 10 Node ( int V = 0 , int W = 0 ):v(V),w(W){} 11 }Good; 12 13 14 vector <Node> Kind[N]; 15 16 int main(){ 17 18 ios_base :: sync_with_stdio(false); 19 cin.tie(NULL) , cout.tie(NULL) ; 20 21 cin >> V >> n ; 22 for( int i = 1 , v , w , No ; i <= n ; i++ ){ 23 cin >> v >> w >> No ; 24 Kind[No].push_back( Node(v,w) ); 25 } 26 27 for(int i=0;i<N;i++){ 28 if( Kind[i].empty() ) continue ; 29 for( int j = V ; j >= 0 ; j -- ){ 30 for( auto k : Kind[i] ){ 31 if( j >= k.v ){ 32 f[j] = max( f[j] , f[ j - k.v ] + k.w ); 33 } 34 } 35 } 36 } 37 38 cout << f[V] << endl; 39 40 return 0; 41 } 42 43 /* 44 cin >> n >> m ; 45 for(int i=0;i<n;i++){ 46 47 cin >> s ; 48 for(int j = 0 ; j < s ; j++ ) cin >> v[j] >> w[j] ; 49 for(int j = m ; j >= 0; j-- ){ 50 for(int k = 0 ; k < s ; k++ ){ 51 if( j >= v[k] ){ 52 f[j] = max( f[j] , f[j-v[k]]+w[k] ); 53 } 54 } 55 } 56 } 57 cout << f[m] << endl; 58 */
HDU 1712 ACboy needs your help
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 1e2 + 20 ; 4 int G[N][N] ; 5 int f[N]; 6 int n,m; 7 int main() 8 { 9 while( scanf("%d%d",&n,&m) ,(n+m) ){ 10 11 memset( f , 0 , sizeof f ); 12 13 for( int i = 1 ; i <= n ; i++ ){ 14 for( int j = 1 ; j <= m ;j++ ){ 15 scanf("%d",&G[i][j]); 16 } 17 } 18 19 for( int i = 1 ; i <= n ; i++ ){ 20 for( int j = m ; j >= 1 ; j-- ){ 21 for( int k = 1 ; k <= m ; k++ ){ 22 if( j >= k ){ 23 f[j] = max( f[j] , f[j-k] + G[i][k] ); 24 } 25 } 26 } 27 } 28 29 printf("%d ",f[m]); 30 } 31 return 0 ; 32 }
8銆佹湁渚濊禆鑳屽寘闂
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N = 1e5 + 10 ; 5 6 typedef struct Node{ 7 int v , w , Fa , Kind ; 8 }Node ; 9 Node a[N]; 10 11 ll f[N],g[N]; 12 int n ,m, V , idx = 0 ; 13 14 int main() 15 { 16 17 while( scanf("%d%d",&n,&V) != EOF){ 18 19 for( int i = 0 ; i <= V ; i++ ) f[i] = 0 ; 20 idx = 0 ; 21 22 for( int i = 1 ; i <= n ; i++ ){ 23 scanf("%d%d",&a[idx].v , &m ); 24 a[idx].w = a[idx].Fa = 0 ; 25 a[idx].Kind = i ; 26 idx ++ ; 27 for( int j = 0 ; j < m ; j++ ){ 28 scanf("%d%d",&a[idx].v , &a[idx].w); 29 a[idx].Kind = a[idx].Fa = i ; 30 idx ++ ; 31 } 32 } 33 34 for( int i = 0 ; i < idx ;i++){ 35 if( !a[i].Fa ){ 36 for( int j = 0 ; j < a[i].v ; j++ ) 37 g[j] = 0 ; 38 39 for( int j = a[i].v ; j <= V ; j++ ) 40 g[j] = f[j-a[i].v]+a[i].w; 41 42 for( int k = 0 ; k < idx ; k++ ){ 43 if( a[k].Fa == a[i].Kind ){ 44 for( int j = V ; j >= a[k].v + a[i].v ; j -- ){ 45 g[j] = max( g[j] , g[j-a[k].v] + a[k].w ); 46 } 47 } 48 } 49 for( int j = a[i].v ; j <= V ; j++ ) 50 f[j] = max( f[j] , g[j] ); 51 52 } 53 } 54 printf("%lld ",f[V]); 55 } 56 return 0; 57 }
1 #pragma GCC optimize(2) 2 #include<bits/stdc++.h> 3 using namespace std; 4 const int M = 4e4+10; 5 const int N = 65 ; 6 7 /* Add_edge */ 8 typedef struct Node{ 9 int to , next ; 10 }Edge ; 11 12 Edge e[N<<1]; 13 int head[N] , idx ; 14 void Add_edge(int u,int v){ 15 e[idx] = Edge{ v , head[u] } ; 16 head[u] = idx++ ; 17 } 18 void Init(){ 19 idx = 0; 20 memset( head , -1 , sizeof head ); 21 } 22 23 /* ___If you ask me how much i love you ______*/ 24 /* The moonlight stands for my heart */ 25 26 27 /* Package */ 28 29 int n , V ; 30 int v[N] , w[N] ; 31 int f[N][M]; 32 void dfs( int u ){ 33 for( int i = head[u] ; ~i ; i = e[i].next ){ 34 int Son = e[i].to ; 35 dfs( Son ) ; 36 for( int j = V - v[u] ; j >= 0 ; j -- ){ 37 for( int k = 0 ; k <= j ; k ++ ){ 38 f[u][j] = max( f[u][j] , f[u][j-k] + f[Son][k] ); 39 } 40 } 41 } 42 for( int i = V ; i >= v[u] ; i -- ) f[u][i] = f[u][i-v[u]] + v[u] * w[u] ; 43 for( int i = 0 ; i < v[u] ; i++ ) f[u][i] = 0 ; 44 } 45 46 /* */ 47 48 int main() 49 { 50 Init() ; 51 ios_base :: sync_with_stdio(false); 52 cin.tie(NULL) , cout.tie(NULL); 53 cin >> V >> n ; 54 for( int i = 1 , Fa ; i <= n ; i++ ){ 55 cin >> v[i] >> w[i] >> Fa ; 56 Add_edge( Fa , i ); 57 } 58 dfs( 0 ) ; 59 cout << f[0][V] << endl; 60 return 0 ; 61 }
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 5e6+5; 4 const int M = 1e3+5; 5 int h[N]; 6 int f[N]; 7 typedef struct Node{ 8 int v , w , Fa; 9 }Node ; 10 Node a[M]; 11 12 int V , n ; 13 int main(){ 14 ios_base :: sync_with_stdio(false); 15 cin.tie(NULL) , cout.tie(NULL) ; 16 cin >> V >> n ; 17 18 for( int i = 1 ; i <= n ; i++ ){ 19 cin >> a[i].v >> a[i].w >> a[i].Fa ; 20 a[i].w *= a[i].v ; 21 } 22 for( int i = 1 ; i <= n ; i++ ){ 23 if( !a[i].Fa ){ 24 for( int j = 0 ; j <= a[i].v ; j++ ) h[i] = 0 ; 25 for( int j = a[i].v ; j <= V ; j++ ) 26 h[j] = f[j-a[i].v]+a[i].w ; 27 28 for( int j = 1 ; j <= n ; j ++ ){ 29 if( a[j].Fa == i ){ 30 for( int k = V ; k >= a[i].v + a[j].v ; k-- ){ 31 h[k] = max( h[k] , h[k-a[j].v] + a[j].w ); 32 } 33 } 34 } 35 for( int j = a[i].v ; j <= V ; j++ ) 36 f[j] = max( f[j] , h[j] ); 37 38 } 39 } 40 cout << f[V] << endl ; 41 return 0 ; 42 }
9銆佹硾鍖栫墿鍝?/h3>
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 1e3+10; 4 const int M = 65; 5 int f[N][M]; 6 int a[N],b[N]; 7 int v[N],w[N]; 8 int n,V,k; 9 int main(){ 10 int T ; 11 scanf("%d",&T); 12 while(T--){ 13 memset(f,0,sizeof f); 14 scanf("%d%d%d",&n,&V,&k); 15 for( int i = 0 ; i < n ; i++ ) 16 scanf("%d",&w[i]); 17 for( int i = 0 ; i < n ; i++ ) 18 scanf("%d",&v[i]); 19 20 for( int i = 0 ; i < n ; i++ ){ 21 for( int j = V ; j >= v[i] ; j-- ){ 22 for( int t = 1 ; t <= k ; t ++ ){ 23 a[t] = f[j][t] ; 24 b[t] = f[j-v[i]][t] + w[i] ; 25 } 26 27 int m , x , y ; 28 m = x = y = 1 ; 29 a[k+1] = b[k+1] = -1 ; 30 while( m <= k && (a[x] != -1 || b[y] != -1 ) ){ 31 if( a[x] > b[y] ){ 32 f[j][m] = a[x++] ; 33 }else{ 34 f[j][m] = b[y++] ; 35 } 36 if( f[j][m] != f[j][m-1] ) 37 m++ ; 38 } 39 } 40 } 41 printf("%d ",f[V][k]); 42 } 43 return 0 ; 44 }