Codeforces Round #658 (Div. 2) D. Unmerge(dp,01背包)
程序员文章站
2022-03-11 15:02:56
题目链接思路:将每个数和他之后的比他小的数都分为一组,对分好的组进行01背包,看能否将组的总和变为n。代码:#includeusing namespace std;#define int long long#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);const int N=1e5+7;const double eps=1e-8;const int mod=1e9+7...
题目链接
思路:
将每个数和他之后的比他小的数都分为一组,对分好的组进行01背包,看能否将组的总和变为n。
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=1e5+7;
const double eps=1e-8;
const int mod=1e9+7;
const int inf=0x7fffffff;
const double pi=3.1415926535;
signed main()
{
IOS;
int t;
cin>>t;
while(t--)
{
int ans[N],arr[N],dp[N]={0},n,m=0;
cin>>n;
for(int i=1;i<=2*n;i++)
{
cin>>arr[i];
}
int Max=arr[1],num = 1;
for(int i=2;i<=2*n;i++)
{
if(arr[i]>Max)
{
Max=arr[i];
ans[m++]=num;
num = 0;
}
num++;
}
for(int i=0;i<m;i++)
{
for(int j = n;j >= ans[i];j--)
{
dp[j] = max(dp[j],dp[j-ans[i]]+ans[i]);
}
}
cout<<(dp[n]==n?"YES":"NO")<<endl;
}
return 0;
}
本文地址:https://blog.csdn.net/ACkingdom/article/details/107525024
推荐阅读
-
Codeforces Round #658 (Div. 2) D. Unmerge(dp,01背包)
-
Codeforces Round #608 (Div. 2) D. Portals(DP)
-
Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) D. Recovering BST(区间DP)
-
Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable - dp分组背包
-
Codeforces Round #235 (Div. 2) D. Roman and Numbers(状压dp)_html/css_WEB-ITnose
-
Codeforces Round #235 (Div. 2) D. Roman and Numbers(状压dp)_html/css_WEB-ITnose
-
Codeforces Round #505 (rated, Div. 1 + Div. 2) D. Recovering BST(区间dp/bitset优化)
-
Codeforces Round #658 (Div. 2) D. Unmerge(dp,01背包)