zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索

    E. Chocolate Bar

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/598/problem/E

    Description

    You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar.

    In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length.

    For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 32 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 22 = 4).

    For several given values nm and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - ksquares are not necessarily form a single rectangular piece.

    Input

    The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values nm and k to process.

    Each of the next t lines contains three integers nm and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively.

    Output

    For each nm and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly ksquares.

    Sample Input

    4
    2 2 1
    2 2 3
    2 2 2
    2 2 4

    Sample Output

    5
    5
    4
    0

    HINT

    题意

    给你n*m的巧克力,然后你想吃面积恰好为k的巧克力,问你怎么样才能花费最少吃到他

    每次花费是你切的这刀的长度的平方

    题解:

    记忆化搜索,然后中间直接暴力dfs就好了

    代码

    #include<iostream>
    #include<stdio.h>
    #include<cstring>
    using namespace std;
    int dp[50][50][50];
    
    int dfs(int n,int m,int k)
    {
        if(!k||n*m==k)return 0;
        if(dp[n][m][k]!=-1)return dp[n][m][k];
        int& minn = dp[n][m][k];
        minn = 9999999LL;
        for(int i=1;i<n;i++)
            for(int j=0;j<=k;j++)
                minn = min(minn,dfs(i,m,j)+dfs(n-i,m,k-j)+m*m);
        for(int i=1;i<m;i++)
            for(int j=0;j<=k;j++)
                minn = min(minn,dfs(n,i,j)+dfs(n,m-i,k-j)+n*n);
        return minn;
    }
    int main()
    {
        int t;scanf("%d",&t);
        memset(dp,-1,sizeof(dp));
        while(t--)
        {
            int n,m,k;
            scanf("%d%d%d",&n,&m,&k);
            printf("%d
    ",dfs(n,m,k));
        }
    
    }
  • 相关阅读:
    [2017-7-28]Android Learning Day7
    Codeforces Round #402 (Div. 2) D. String Game
    POJ2411 铺地砖 Mondriaan's Dream
    《大型网站系统架构的演化》
    Nginx 引入线程池,提升 9 倍性能
    《淘宝消息中间件概述》2015-07-11
    主从复制源代码分析
    深入剖析Redis主从复制
    主从复制配置
    Redis启动多端口、运行多实例
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4966148.html
Copyright © 2011-2022 走看看