[poj1020]Anniversary Cake
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 17203   Accepted: 5619

Description

Nahid Khaleh decides to invite the kids of the "Shahr-e Ghashang" to her wedding anniversary. She wants to prepare a square-shaped chocolate cake with known size. She asks each invited person to determine the size of the piece of cake that he/she wants (which should also be square-shaped). She knows that Mr. Kavoosi would not bear any wasting of the cake. She wants to know whether she can make a square cake with that size that serves everybody exactly with the requested size, and without any waste.

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by input data for each test case. Each test case consist of a single line containing an integer s, the side of the cake, followed by an integer n (1 ≤ n ≤ 16), the number of cake pieces, followed by n integers (in the range 1..10) specifying the side of each piece.

Output

There should be one output line per test case containing one of the words KHOOOOB! or HUTUTU! depending on whether the cake can be cut into pieces of specified size without any waste or not.

Sample Input

2
4 8 1 1 1 1 1 3 1 1
5 6 3 3 2 1 1 1

Sample Output

KHOOOOB!
HUTUTU!

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest
 
题目大意:给出一个蛋糕的长,是否能切成M个给定的小正方形且不留剩余?
 
试题分析:很显然,如果所有小蛋糕的面积大于大蛋糕的面积,那么就不行
                                  剩下的枚举一下高度,尽量先放大的,往下放,维护每一列的高度就可以了
 
代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
//#include<cmath> using namespace std;
const int INF = 9999999;
#define LL long long inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
int N,M;
int T;
int a[60];
int sum;
int maxr[60];
int re[60]; bool DFS(int st){
if(st==M){return true;}
int Minn=INF,tmp;
for(int i=1;i<=N;i++){
if(maxr[i]<Minn){
tmp=i;
Minn=maxr[i];
}
}
for(int i=10;i>=1;i--){
if(!re[i]) continue;
bool canin;
if(N-maxr[tmp]>=i&&N-tmp+1>=i){
canin=true;
for(int j=tmp;j<tmp+i;j++){
if(maxr[j]>maxr[tmp]) {
canin=false;
break;
}
}
if(canin){
for(int j=tmp;j<tmp+i;j++) maxr[j]+=i;
re[i]--;
if(DFS(st+1)) return true;
re[i]++;
for(int j=tmp;j<tmp+i;j++) maxr[j]-=i;
}
}
}
return false;
} int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
T=read();
while(T--){
memset(re,0,sizeof(re));
memset(maxr,0,sizeof(maxr));
memset(a,0,sizeof(a));
sum=0;
N=read(),M=read();
for(int i=1;i<=M;i++) a[i]=read(),sum+=(a[i]*a[i]),re[a[i]]++;
if(sum!=N*N) {printf("HUTUTU!\n");continue;}
if(DFS(0)) printf("KHOOOOB!\n");
else printf("HUTUTU!\n");
}
return 0;
}

【DFS】Anniversary Cake的更多相关文章

  1. 【poj1020】 Anniversary Cake

    http://poj.org/problem?id=1020 (题目链接) 题意 有一个S*S的大蛋糕,还有许多正方形的小蛋糕,问能否将大蛋糕完整的分成所有的小蛋糕,不能有剩余. Solution 像 ...

  2. 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】

    目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...

  3. Kattis - glitchbot 【DFS】

    Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...

  4. HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. 【dfs】P1331 海战

    题目描述 在峰会期间,武装部队得处于高度戒备.警察将监视每一条大街,军队将保卫建筑物,领空将布满了F-2003飞机.此外,巡洋船只和舰队将被派去保护海岸线.不幸的是因为种种原因,国防海军部仅有很少的几 ...

  6. 【dfs】p1731 生日蛋糕

    1441:[例题2]生日蛋搞 [题目描述] 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体.设从下往上数第i(1≤i≤M)层蛋糕是半径为Ri, 高 ...

  7. 【dfs】LETTERS

    1212:LETTERS [题目描述] 给出一个roe×colroe×col的大写字母矩阵,一开始的位置为左上角,你可以向上下左右四个方向移动,并且不能移向曾经经过的字母.问最多可以经过几个字母. [ ...

  8. 洛谷P1605 迷宫【dfs】

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  9. 【dfs】BZOJ1703-[Usaco2007 Mar]Ranking the Cows 奶牛排名

    [题目大意] 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序,约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他 ...

随机推荐

  1. a标签的嵌套

    1.a标签的嵌套 a标签不能嵌套,若a标签中嵌套了a标签,浏览器会自动添加结束符号,故不能嵌套 2.例子 编辑器中的代码 <a href="#">外层a标签<a ...

  2. 无缝衔接demo

    如题. <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" con ...

  3. MySQL:Can't create test file XXX.lowe-test

    问题说明 今天部署MySQL,在使用mysql_install_db,初始化数据库时报如下错误 180622 11:36:38 mysqld_safe Starting mysqld daemon w ...

  4. openssl-0.9.8y

    openssl-0.9.8y 支持 32位和64位 编译不报错和向上兼容和向下兼容. http://www.openssl.org/source/openssl-0.9.8y.tar.gz https ...

  5. C# 数组 随机 排序

    ]; ; i < ; i++) { arrInt[i] = i; } arrInt = arrInt.OrderBy(c => Guid.NewGuid()).ToArray<int ...

  6. HDU 6119 小小粉丝度度熊 双指针

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6119 题意:中文题面. 解法:先处理可能交叉的区间,然后容易发现满足双指针的特性. //HDU 611 ...

  7. Centos7 IP地址配置方法

    1.编辑 ifcfg-eth0 文件 # vim /etc/sysconfig/network-scripts/ifcfg-eth0 2.修改如下内容 BOOTPROTO=”static” #dhcp ...

  8. 在ubuntu上配置LAMP架构

    1. 安装MySQL /* ubuntu默认进入系统是普通用户 所以在真实工作中,我们会得到root的授权. 所以我们需要用sudo做一切只有root才能完成的操作. */ [root@LAMP ~] ...

  9. [路由] -- Yii2 url地址美化与重写

    转载:http://blog.csdn.net/lmjy102/article/details/53857520

  10. Context-Aware Network Embedding for Relation Modeling

    Context-Aware Network Embedding for Relation Modeling 论文:http://www.aclweb.org/anthology/P17-1158 创新 ...