zoukankan      html  css  js  c++  java
  • 【DFS】Anniversary Cake

    [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!
    ");continue;}
    		if(DFS(0)) printf("KHOOOOB!
    ");
    		else printf("HUTUTU!
    ");
    	}
    	return 0;
    }
  • 相关阅读:
    中文词频统计及词云制作
    字符串操作练习:星座、凯撒密码、99乘法表、词频统计预处理
    python第二节课
    Python第一节课
    了解大数据
    pcb结构定义
    一个完整的大作业
    中文词频统计及词云制作
    字符串操作练习:星座、凯撒密码、99乘法表、词频统计预处理
    Python输入输出练习
  • 原文地址:https://www.cnblogs.com/wxjor/p/7044819.html
Copyright © 2011-2022 走看看