清北暑假模拟day1 生活

时间:2021-11-22 17:17:25

清北暑假模拟day1 生活

清北暑假模拟day1 生活

/*
数字三角形,要求第K大的值,可以推知,如果得知k的范围,那么一定是在上一行可转移状态的对应范围内(反证法可以证明),这个在背包九讲里也有提及
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm> using namespace std; int n,k,f[][][],z[][],y[]; int main()
{
freopen("live.in","r",stdin);
freopen("live.out","w",stdout); scanf("%d%d",&n,&k);
for (int a=;a<=n;a++)
for (int b=;b<=a;b++)
scanf("%d",&z[a][b]);
for (int a=;a<=n;a++)
for (int b=;b<=a;b++)
for (int c=;c<=k;c++)
f[a][b][c]=-;
f[][][]=z[][];
for (int a=;a<n;a++)
for (int b=;b<=a;b++)
for (int c=;c<=k;c++)
if (f[a][b][c]!=-)
{
int v=f[a][b][c]+z[a+][b];
for (int d=;d<=k;d++)
if (v>=f[a+][b][d])
{
for (int e=k;e>d;e--)
f[a+][b][e]=f[a+][b][e-];
f[a+][b][d]=v;
break;
}
v=f[a][b][c]+z[a+][b+];
for (int d=;d<=k;d++)
if (v>=f[a+][b+][d])
{
for (int e=k;e>d;e--)
f[a+][b+][e]=f[a+][b+][e-];
f[a+][b+][d]=v;
break;
}
}
int cnt=;
for (int a=;a<=n;a++)
for (int b=;b<=k;b++)
y[++cnt]=f[n][a][b];
sort(y+,y+cnt+);
printf("%d\n",y[cnt-k+]); return ;
}