题解:P16016 [ICPC 2021 NAC] Contest Construction
longyitongxue · · 题解
我们发现,要选一个题,只需和前面选过的最后两个题比较,考虑 DP,定义
::::success[Code] :::warning[温馨提示]{open} :::
#include<bits/stdc++.h>
#define int int_fast64_t
using namespace std;
int dp[55][55][25],a[55];//dp[i][j][x] -> 选中 x 道题,最后两题下标是 i,j 的合法子集
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
dp[i][j][2]=1;//选 2 道题,所有 i<j 的方案数为 1
}
}
for(int x=2;x<k;x++){
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(dp[i][j][x]){
for(int t=j+1;t<=n;t++){
if(a[t]<=a[i]+a[j]){
dp[j][t][x+1]+=dp[i][j][x];
}
}
}
}
}
}
int ans=0;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
ans+=dp[i][j][k];
}
}
cout<<ans;
return 0;
}
::::