zoukankan      html  css  js  c++  java
  • 清北暑假模拟day1 生活

    /*
    数字三角形,要求第K大的值,可以推知,如果得知k的范围,那么一定是在上一行可转移状态的对应范围内(反证法可以证明),这个在背包九讲里也有提及
    */
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    int n,k,f[110][110][15],z[110][110],y[1010];
    
    int main()
    {
        freopen("live.in","r",stdin);
        freopen("live.out","w",stdout);
    
        scanf("%d%d",&n,&k);
        for (int a=1;a<=n;a++)
            for (int b=1;b<=a;b++)
                scanf("%d",&z[a][b]);
        for (int a=1;a<=n;a++)
            for (int b=1;b<=a;b++)
                for (int c=1;c<=k;c++)
                    f[a][b][c]=-12345;
        f[1][1][1]=z[1][1];
        for (int a=1;a<n;a++)
            for (int b=1;b<=a;b++)
                for (int c=1;c<=k;c++)
                    if (f[a][b][c]!=-12345)
                    {
                        int v=f[a][b][c]+z[a+1][b];
                        for (int d=1;d<=k;d++)
                            if (v>=f[a+1][b][d])
                            {
                                for (int e=k;e>d;e--)
                                    f[a+1][b][e]=f[a+1][b][e-1];
                                f[a+1][b][d]=v;
                                break;
                            }
                        v=f[a][b][c]+z[a+1][b+1];
                        for (int d=1;d<=k;d++)
                            if (v>=f[a+1][b+1][d])
                            {
                                for (int e=k;e>d;e--)
                                    f[a+1][b+1][e]=f[a+1][b+1][e-1];
                                f[a+1][b+1][d]=v;
                                break;
                            }
                    }
        int cnt=0;
        for (int a=1;a<=n;a++)
            for (int b=1;b<=k;b++)
                y[++cnt]=f[n][a][b];
        sort(y+1,y+cnt+1);
        printf("%d
    ",y[cnt-k+1]);
    
        return 0;
    }
  • 相关阅读:
    NO.2
    【转载】初始化顺序
    Java中的容器
    primer看完了
    NO.1
    转 Python爬虫入门二之爬虫基础了解
    转 Python爬虫入门一之综述
    hdu 5691 Sitting in Line
    51nod 1043 幸运号码
    51nod 1624 取余最长路
  • 原文地址:https://www.cnblogs.com/hyfer/p/5978501.html
Copyright © 2011-2022 走看看