POJ 1694 An Old Stone Game【递归+排序】
链接:
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 3132 | Accepted: 1422 |
Description
- At the beginning of the game, the player picks K stones and puts them all in one bucket.
- At each step of the game, the player can pick one stone from the bucket and put it on any empty leaf.
- When all of the r immediate children of a node p each has one stone, the player may remove all of these r stones, and put one of the stones on p. The other r - 1 stones are put back into the bucket, and can be used in the later steps of the game.
The player wins the game if by following the above rules, he succeeds to put one stone on the root of the tree.
You are to write a program to determine the least number of stones to be picked at the beginning of the game (K), so that the player can win the game on the given input tree.
Input
Root has label 1. Description of each tree starts with N in a separate line. The following N lines describe the children of all nodes in order of their labels. Each line starts with a number p (1 <= p <= N, the label of one of the nodes), r the number of the
immediate children of p, and then the labels of these r children.
Output
Sample Input
2
7
1 2 2 3
2 2 5 4
3 2 6 7
4 0
5 0
6 0
7 0
12
1 3 2 3 4
2 0
3 2 5 6
4 3 7 8 9
5 3 10 11 12
6 0
7 0
8 0
9 0
10 0
11 0
12 0
Sample Output
3
4
Source
题意:
第二行表示当前树中有 N 个节点
下面 N 行:
每一行的前两个数 index num 分别表示当前节点的编号为 index, 有 num 个叶子
剩下 num 个数表示各个叶子节点的编号
游戏规则:
从叶子节点开始放石头,每个叶子放一个
如果放满了当前节点的所有叶子节点, 那么可以拿掉所有的叶子节点的石头,
并且从拿掉的石头中取出一个放在当前节点中
问:放到根节点 1 最少需要多少个石头
算法:递归+排序
思路:
当前根的所有叶子节点所需石头数都找到
先按照每个叶子节点所需的stone从大到小排序
(1) 假设各个叶子节点所需石头
a1 > a2 > a3...【中间没有 ==】 那么根节点所需最多stone就是 a1
(2) 假设在排序后,各个叶子节点所需stone存在相等的情况
a1 >= a2 >= a3 ...那么每次遇到一个 == ,stone 就要比 a1 多+1
因为如果用 a1 个石头放满了第一个叶子后,还剩下 a1-1 个石头无法满足 a2
(3) 排序后出现 a1 >= a2 > a3 > a4 >= a5 的交叉情况,
根据填满前面的 叶子 后剩下的石头看是否满足 a 就好了,
根据 (2) 的分析不满足最多只需 +1即可
code:
/********************************************************************************
E Accepted 336 KB 0 ms C++ 1059 B 看的 MMchen 的了Orz 题意:输入的第一行是测试数据组数 T 【T棵树】
第二行表示当前树中有 N 个节点
下面 N 行:
每一行的前两个数 index num 分别表示当前节点的编号为 index, 有 num 个叶子
剩下 num 个数表示各个叶子节点的编号
游戏规则:
从叶子节点开始放石头,每个叶子放一个
如果放满了当前节点的所有叶子节点, 那么可以拿掉所有的叶子节点的石头,
并且从拿掉的石头中取出一个放在当前节点中
问:放到根节点 1 最少需要多少个石头 算法:递归+排序 思路: 从叶子节点往根找,
当前根的所有叶子节点所需石头数都找到
先按照每个叶子节点所需的stone从大到小排序
(1) 假设各个叶子节点所需石头
a1 > a2 > a3...【中间没有 ==】 那么根节点所需最多stone就是 a1
(2) 假设在排序后,各个叶子节点所需stone存在相等的情况
a1 >= a2 >= a3 ...那么每次遇到一个 == ,stone 就要比 a1 多+1
因为如果用 a1 个石头放满了第一个叶子后,还剩下 a1-1 个石头无法满足 a2
(3) 排序后出现 a1 >= a2 > a3 > a4 >= a5 的交叉情况,
根据填满前面的 叶子 后剩下的石头看是否满足 a 就好了,
根据 (2) 的分析不满足最多只需 +1即可 ********************************************************************************/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std; const int maxn = 210; int node[maxn][maxn]; bool cmp(int a, int b)
{
return a > b;
} int solve(int index) //求解编号为 index 的节点所需石头
{
if(node[index][0] == 0) //如果第index节点没有叶子,只需填满自己
{
return 1;
}
int tmp[maxn]; //暂存第 index 节点的每一个叶子所需的石头
for(int i = 1; i <= node[index][0]; i++) // 遍历第 index 节点的每一个叶子
{
tmp[i] = solve(node[index][i]); //递归求解
}
sort(tmp+1, tmp+node[index][0]+1, cmp); //按所需石头从大到小排序
int ans = tmp[1]; //排序后第一个最大
for(int i = 2; i <= node[index][0]; i++) //遍历后面的看是否满足
{
if(tmp[i] > (ans-(i-1))) ans++; //如果刚好填完前面 i-1 后不够,最多+1, 前面已经排序
}
return ans; } int main()
{
int T;
int n;
scanf("%d", &T);
while(T--)
{
memset(node, 0, sizeof(node));
scanf("%d", &n); //总节点数
int index, num;
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &index, &num); //index当前节点编号, num 当前节点拥有的叶子
node[index][0] = num; // 0下标存叶子节点数目, 剩下的存当前根叶子编号
for(int j = 1; j <= num; j++)
{
scanf("%d", &node[index][j]);
}
}
int ans = solve(1); // 求解根节点
printf("%d\n", ans);
}
return 0;
}
POJ 1694 An Old Stone Game【递归+排序】的更多相关文章
- poj 1694 An Old Stone Game 树形dp
//poj 1694 //sep9 #include <iostream> #include <algorithm> using namespace std; const in ...
- POJ 1694 An Old Stone Game
题目: Description There is an old stone game, played on an arbitrary general tree T. The goal is to pu ...
- C#算法基础之递归排序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- c++(非递归排序)
在上面一篇博客当中,我们发现普通查找和排序查找的性能差别很大.作为一个100万的数据,如果使用普通的查找方法,那么每一个数据查找平均下来就要几十万次,那么二分法的查找呢,20多次就可以搞定.这中间的差 ...
- PHP递归排序
递归算法对于任何一个编程人员来说,应该都不陌生.因为递归这个概念,无论是在PHP语言还是Java等其他编程语言中,都是大多数算法的灵魂. 对于PHP新手来说,递归算法的实现原理可能不容易理解.但是只要 ...
- POJ 1740 A New Stone Game(博弈)题解
题意:有n个石子堆,每一个都可以轮流做如下操作:选一个石堆,移除至少1个石子,然后可以把这堆石子随便拿几次,随便放到任意的其他石子数不为0的石子堆,也可以不拿.不能操作败. 思路:我们先来证明,如果某 ...
- 冒泡排序的思想 python 冒泡排序、递归排序
冒泡排序的时间复杂度是O(N^2) 冒泡排序的思想: 每次比较两个相邻的元素, 如果他们的顺序错误就把他们交换位置 比如有五个数: 12, 35, 99, 18, 76, 从大到小排序, 对相邻的两位 ...
- PHP递归排序怎么实现的?
递归算法对于任何一个编程人员来说,应该都不陌生.因为递归这个概念,无论是在PHP语言还是Java等其他编程语言中,都是大多数算法的灵魂. 对于PHP新手来说,递归算法的实现原理可能不容易理解.但是 ...
- POJ 1740 A New Stone Game
A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5453 Accepted: 2989 ...
随机推荐
- EffectiveJava(9)覆盖equals是总要覆盖hashCode
覆盖equals是总要覆盖hashCode 通过散列函数将集合中不相等的实例均匀的分布在所有可能的散列值上 1.把某个非零的常数值保存在一个名为result的int类型变量中 2.对于对象中每个关键域 ...
- S4:装饰模式 Decorator
动态的给一个对象添加额外的一些职责,就增加功能而言,比继承更具灵活性. 如果仅有一个ConcreateComponent,也可以让Decorator继承ConcreateComponent来实现装饰功 ...
- SQLite升级数据库:
SQLiteOpenHelper子类关键代码: SQLite升级数据库: SQLiteOpenHelper子类关键代码: public class MyDataHelper extends SQLit ...
- 【BIEE】01_下载安装BIEE(Business Intelligence)11g 11.1.1.9.0
环境准备 安装文件 如果操作系统是64位,则下载64位版本,我安装的系统是64位的 1.下载所有安装文件 1.1 Oracle Database 11g R2 下载地址: http://www.ora ...
- C语言 | 计算器实现(中缀表示法/后缀表示法)
———————————————————————————————————————————— 实现原理: 每个操作数都被依次压入栈中,当一个运算符到达时,从栈中弹出相应数目的操作数(对于二元运算符来说是两 ...
- .net 取当前代码的行号及类名称
取方法入口的行号及类名 //System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true); //int a ...
- 基于Android的rgb七彩环颜色采集器
代码地址如下:http://www.demodashi.com/demo/11892.html 一.前言. 在大学期间,看到这个rgb灯,蛮好奇的,这么漂亮的颜色采集,并且可以同步到设备rbg灯颜色, ...
- 【LeetCode】LeetCode——第14题:Longest Common Prefix
14. Longest Common Prefix My Submissions Question Editorial Solution Total Accepted: 97052 Total Sub ...
- xpinyin-函数返回多个值-lambda匿名函数-列表生成式-三元表达式
import xpinyinp=xpinyin.Pinyin() #实例化print(p.get_pinyin('小白','')) 函数返回多个值:1.函数如果返回多个值的话,它会把这几个值放到一个元 ...
- DelphiXe 中静态数组TByteArray和动态数组TBytes /array of byte 的区别
在应用中发现静态数组和动态数组是有区别的: procedure TForm1.Button1Click(Sender: TObject);var RsltStream: TMemoryStream; ...