[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. Spring的使用优点

    spring事物配置,声明式事务管理和基于@Transactional注解的使用 spring支持编程式事务管理和声明式事务管理两种方式. 编程式事务管理使用TransactionTemplate或者 ...

  2. 关于C++的宏:WIN32和DEBUG

    判断平台相关,判断程序是属于debug版本还是release版本,我们会这么做. #ifdef WIN32 #else #endif #ifdef DEBUG // 如果是调试版本 #else //发 ...

  3. 25个Linux相关的网站【转】

    转自:http://www.cnblogs.com/Lindaman/p/4552805.html 下面是25个最具有影响力,也是最重要的Linux网站,这些网站提供了Linux的分发包,软件,文件, ...

  4. handle_level_irq 与handle_edge_irq 的区别【转】

    转自:http://blog.csdn.net/xavierxiao/article/details/6087277 版权声明:本文为博主原创文章,未经博主允许不得转载. Linux 里, handl ...

  5. PhysX SDK

    PhysX SDK https://developer.nvidia.com/physx-sdk NVIDIA PhysX SDK Downloads http://www.nvidia.cn/obj ...

  6. Xcode7 iOS9.0 的真机调试

    Xcode7的真机调试: 1.Xcode偏好 -> 账号 -> 增加 Apple ID ->显示 free 2.Target 运行 iOS 版本号 3.修正 Team 项   选择 ...

  7. vim常用命令(复习版)(转)

    原文链接:http://blog.csdn.net/love__coder/article/details/6739670 1.光标移动 上:k 下:j 左:l 『字母L小写』 右:h 上一行行首:- ...

  8. linux命令(35):head命令

    1.命令格式: head [参数]... [文件]... 2.命令功能: head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行. 3.命令参数: -q 隐藏文件名 -v ...

  9. hdu 1829(继续扩展并查集)

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  10. 【hdoj_2391】FilthyRich

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2391 题目大意:给定一个矩阵,从左上角第一个元素开始到右下最后一个元素,寻找一条路线,使得路线经过的矩阵元 ...