題意:對於兩個陣列,定義merge運算:每次把兩個陣列的首部的較小的那個拿出,並放到乙個新的陣列中。現在給定乙個長度為n*2的排列,問你這個排列是否可能由兩個長度為n的陣列merge得到?可能則列印yes,否則no。
思路:因為每次拿頭部最小的放進新陣列,所以一旦我們碰到乙個數值a[i],緊跟a[i]後面的並且小於a[i]的全都應該和a[i]是同一組的,那麼我們對於每次出現的這種,我們記錄它們的長度。如果可能由兩個長度為n的組成,那麼我們可以找到一些長度,他們加起來等於n,也就是01揹包問題了。
**:
#include
#pragma gcc optimize("ofast")
#define endl '\n'
#define null null
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define int long long
#define pii pair
#define pdd pair
#define ull unsigned long long
#define all(x) x.begin(),x.end()
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ct cerr<<"time elapsed:"<<1.0*clock()/clocks_per_sec<<"s.\n";
char
*fs,
*ft,buf[
1<<20]
;#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline
intread()
while
(ch>=
'0'&&ch<=
'9')
return x*f;
}using
namespace std;
const
int n=
4e3+5;
const
int inf=
0x3f3f3f3f
;const
int mod=
1e9+7;
const
double eps=
1e-6
;const
double pi=
acos(-
1);int a[n]
,dp[n]
,d[n]
;signed
main()
int ma=a[1]
,cnt=
1,len=0;
for(
int i=
2;i<=
2*n;i++
)else
cnt++;}
d[++len]
=cnt;
for(
int i=
1;i<=len;i++
)for
(int j=n;j>=d[i]
;j--
) dp[j]
=max
(dp[j]
,dp[j-d[i]
]+d[i]);
if(dp[n]
==n)
cout<<
"yes"
cout<<
"no"
<}}
0 1揹包問題python 0 1揹包問題1
鼓搗好久 終於了然了一些 0 1揹包問題描述 有乙個竊賊在偷竊一家商店時發現有n件物品,第i件物品價值為vi元,重量為wi,假設vi和wi都為整數。他希望帶走的東西越值錢越好,但他的揹包中之多只能裝下w磅的東西,w為一整數。他應該帶走哪幾樣東西?注 0 1揹包問題中 每件物品或被帶走,或被留下,需要...
揹包問題 01揹包問題
n個物品,總體積是v,每個物品的體積的vi,每個物品的最大價值是wi,在不超過v的體積下求最大價值 eg揹包容積為 5 物品數量為 4 物品的體積分別為 物品的價值分別為 思路定義乙個二位陣列int f new int n 1 v 1 f i j 就表示在1 i個物品中選取體積小於v的情況的最大價值...
揹包問題 01揹包
有n件物品和乙個容量為v的揹包。第i件物品的重量是c i 價值是w i 求解將哪些物品裝入揹包可使價值總和最大。01揹包中的 01 就是一種物品只有1件,你可以選擇放進去揹包即1,也可以選擇不放入揹包中即0。include include using namespace std const int ...