programming-challenges Hanoi Tower Troubles Again! (110908) 题解

原创 2015年11月19日 23:58:00

贪心算法就可以简单的解决了。但是问题是如何我没有办法证明贪心是对的。

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <assert.h>
#include <algorithm>
#include <math.h>
#include <ctime>
#include <functional>
#include <string.h>
#include <stdio.h>
#include <numeric>
#include <float.h>

using namespace std;

bool isSquare(int num) {
	int i = sqrt(num); 
	if (num == i*i) return true;
	return false;
}

int solution(int colNum) {
	vector<int> colums(colNum); 
	int num = 0, ans = 0;
	while (true) {
		num++;
		bool ok = false; 
		for (int i = 0; i < colNum; i++) {
			if (colums[i] == 0) {
				colums[i] = num;
				ok = true;
				ans++; 
				break;
			}
			else {
				if (isSquare(colums[i] + num)) {
					colums[i] = num; 
					ok = true;
					ans++; 
					break;
				}
			}
		}
		if (!ok) break;
	}
	return ans; 
}

int main() {
	int T = 0; cin >> T; 

	for (int t = 0; t < T; t++) {
		int colNum; cin >> colNum; 
		cout << solution(colNum) << endl; 
	}

	return 0;
}


UVa Problem 10276 Hanoi Tower Troubles Again! (汉诺塔卷土重来!)

// Hanoi Tower Troubles Again! (汉诺塔卷土重来!) // PC/UVa IDs: 110908/10276, Popularity: B, Success rate: ...

joj 1031 Hanoi Tower Troubles Again!解题报告

  1031: Hanoi Tower Troubles Again!ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE3s8192K1034606S...

HDU 1329 Hanoi Tower Troubles Again!

Description People stopped moving discs from peg to peg after they know the number of steps neede...

UVa10276 HDU1329 ZOJ1239 Hanoi Tower Troubles Again!【递推函数+打表】

Hanoi Tower Troubles Again! People stopped moving discs from peg to peg after they know the numbe...

programming-challenges Adventures in Moving - Part IV (111108) 题解

解题的思路网上有很多文章。我犯的一个错误在于没有卡住油箱的上限只能是200,而用了一个更大一点的边界210,结果算出来的结果是更优的。代码写的不好,不过也懒得整理了。dp这一章的题目都ac了。 #i...

programming-challenges Automated Judge Script (110305) 题解

#include #include #include #include #include #include #include #include #include #include ...

programming-challenges ShellSort (110407) 题解

在programming challenges上没有ac,只在UVA上ac了。不过看了看别人的思路,至少思路上是没问题的。 #include #include #include #includ...

programming-challenges Longest Nap (110404) 题解

One thing to note, the items in the schedule list may not be given in the order of timeline. Here is...

programming-challenges Contest Scoreboard (110207) 题解

建立数据结构然后排序。第一次提交又犯了一个愚蠢的小错误。想想构造好的测试用例的能力也挺重要的。 #include #include #include #include #include #...

programming-challenges Shoemaker's Problem (110405) 题解

Greedy.  证明: Let's say we have job 1, 2, ..., n, and they have time and fine as t1, f1, t2...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:programming-challenges Hanoi Tower Troubles Again! (110908) 题解
举报原因:
原因补充:

(最多只允许输入30个字)