//============================================================================
// Name : 1020.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
#define MAX_RANG 11
int cake[MAX_RANG];
class Part{
private:
int* row_use;
int s;
public:
Part(int s){
this-> s = s;
row_use = new int[s];
memset(row_use, 0 ,s * sizeof(int));
}
~Part(){
delete [] row_use;
}
int getMinRow() const{
int min =row_use[0];
int index =0;
for (int i=1;i<s;i++){
if (row_use[i]< min){
min = row_use[i];
index = i;
}
}
return index;
}
int getMaxSize(int index) const{
int count =0;
int use =row_use[index];
for (int i = index;i<s;i++){
if (row_use[i]== use)
count++;
else
break;
}
return min(count, s-use);
}
void put(int index,int size){
for (int i= 0;i<size;i++){
row_use[i+index]+= size;
}
}
void reset(int index,int size){
for (int i= 0;i<size;i++){
row_use[i+index]-= size;
}
}
void output(){
for (int i= 0;i<s;i++){
cout << row_use[i]<<" ";
}
cout <<endl;
}
};
bool put(Part& part, int n) {
int index = part.getMinRow();
int size = part.getMaxSize(index);
for (int i = size; i >= 0; i--) {
if (cake[i]>0) {
if (n == 0) {
return true;
} else {
part.put(index,i);
cake[i]--;
// cout<<"put "<<i<<endl;
// part.output();
if (put(part, n - 1)) {
return true;
}else{
part.reset(index, i);
cake[i]++;
// cout<<"reset "<<i<<endl;
// part.output();
}
}
}
}
return false;
}
int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
memset(cake,0, MAX_RANG* sizeof(int));
int s, n, rang;
cin >> s >> n;
int sum = s * s;
for (int i = 0; i < n; i++) {
cin >>rang;
sum -=rang * rang;
cake[rang]++;
}
if (sum == 0) {
Part part(s);
if (put(part, n - 1)) {
cout << "KHOOOOB!" << endl;
} else {
cout << "HUTUTU!" << endl;
}
} else {
cout << "HUTUTU!" << endl;
}
}
return 0;
}
poj1020
最新推荐文章于 2018-07-05 00:10:35 发布