JOJ2672 Hanoi Tower Once More
来源:互联网 发布:最终消费支出数据 编辑:程序博客网 时间:2024/05/21 16:41
16 / 20
Problem I: Hanoi Tower Once More
Input File: i.in Output: stdout
Description
You know the game of Hanoi Tower, right? People stopped moving discs from
peg to peg after they know the number of steps needed to complete the entire task.
But on the other hand, they didn't not stopped thinking about similar puzzles with the
Hanoi Tower. Mr. S invented a little game on it. The game consists of N pegs and a
LOT of balls. Each ball has a different number. The balls look ordinary, but they are
actually magic. If the sum of the numbers on two balls is NOT a square number, they
will push each other with a great force when they're too closed, so they can NEVER
be put together touching each other.
The player should place one ball on the top of a peg at a time. And we must start
from the smallest number all the way to the largest .So no ball with a smaller number
should be above a ball with a larger number.
Here comes our question: Given the number on each ball, how many pegs, at
least, are needed to make all the balls placed?
Input
The input consists of T test cases. The number of test cases T is given in the first
line of the input. Each test case starts with a line containing one integers n (n≤300),
which is the amount of balls. In the next line, n different positive integers, the number
on each ball, is given.
Output
Print the minimum pegs needed to make all the balls placed.
Sample Input
2
4
1 2 3 4
4
1 3 6 10
2010 Yuanguang Cup ACM-ICPC Collegiate Programming Contest
17 / 20
Sample Output
3
1
最小路径覆盖
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
bool usedif[305];
int link[305];
bool mat[305][305];
int a[305];
int gx,gy;
bool can(int t)
{
for(int i=1; i<=gy; i++) //注意范围
{
if(usedif[i]==0&&mat[t][i]) //Y中的顶点i未匹配且t与i之间有边
{
usedif[i]=1;
if(link[i]==-1||can(link[i])) //link[i]=-1表示顶点i还未匹配
{
link[i]=t;
return true;
}
}
}
return false;
}
int MaxMatch()
{
int num=0;
memset(link,-1,sizeof(link));
for(int i=1; i<=gx; i++) //对X中的每个顶点在Y中寻找与其匹配的顶点,注意范围
{
memset(usedif,0,sizeof(usedif));
if(can(i)) num++;
}
return num;//返回最大匹配数
}
bool check(int x)
{
int t=(int) sqrt(x);
if(t*t==x) return true;
else return false;
}
bool cmp(int a,int b)
{
return a<b;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
gx=n;
gy=n;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
memset(mat,0,sizeof(mat));
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
if(check(a[i]+a[j])) mat[i][j]=1;
printf("%d/n",n-MaxMatch());
}
return 0;
}
- JOJ2672 Hanoi Tower Once More
- 珠海邀请赛个人解题报告 Problem I: Hanoi Tower Once More
- Hanoi Tower
- Hanoi Tower
- Hanoi Tower
- zoj 2954 Hanoi Tower
- THE TOWER OF HANOI
- Hanoi Tower求解
- xtu 1147 Hanoi Tower
- Tower of Hanoi
- Hanoi Tower问题分析
- 1028. Hanoi Tower Sequence
- Tower of Hanoi
- Tower of Hanoi
- 汉诺塔(Hanoi Tower)
- Tower of Hanoi问题
- codeforces Tower of Hanoi
- 1028. Hanoi Tower Sequence
- 如何让xterm正确显示中文
- 常见的链表题目
- 谨慎使用单精度/双精度数值类型(适用于数据库字段类型)
- 我的空间开通了,欢迎光临!
- OCI+OCL下BLOB
- JOJ2672 Hanoi Tower Once More
- 前缀操作符和后缀操作符的重载
- 一个IT人士的工作经历
- SDL解析——SDL简介
- android 之输入法
- Linux 2.6内核中新的锁机制--RCU
- [MARK]不使用borlndmm.dll内存管理
- Lucene的使用与优化1
- sql 2005性能调优