`
hellobin
  • 浏览: 65493 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

UVA 679 - Dropping Balls

    博客分类:
  • UVA
 
阅读更多

 

A number ofKballs are dropped one by one from the root of a fully binary tree structure FBT. Each time the ball being dropped first visits a non-terminal node. It then keeps moving down, either follows the path of the left subtree, or follows the path of the right subtree, until it stops at one of the leaf nodes of FBT. To determine a ball's moving direction a flag is set up in every non-terminal node with two values, eitherfalseortrue. Initially, all of the flags arefalse. When visiting a non-terminal node if the flag's current value at this node isfalse, then the ball will first switch this flag's value, i.e., from thefalseto thetrue, and then follow the left subtree of this node to keep moving down. Otherwise, it will also switch this flag's value, i.e., from thetrueto thefalse, but will follow the right subtree of this node to keep moving down. Furthermore, all nodes of FBT are sequentially numbered, starting at 1 with nodes on depth 1, and then those on depth 2, and so on. Nodes on any depth are numbered from left to right.

 


For example, Fig. 1 represents a fully binary tree of maximum depth 4 with the node numbers 1, 2, 3, ..., 15. Since all of the flags are initially set to befalse, the first ball being dropped will switch flag's values at node 1, node 2, and node 4 before it finally stops at position 8. The second ball being dropped will switch flag's values at node 1, node 3, and node 6, and stop at position 12. Obviously, the third ball being dropped will switch flag's values at node 1, node 2, and node 5 before it stops at position 10.

 

 


Fig. 1: An example of FBT with the maximum depth 4 and sequential node numbers.

 

 


Now consider a number of test cases where two values will be given for each test. The first value isD, the maximum depth of FBT, and the second one isI, theIthball being dropped. You may assume the value ofIwill not exceed the total number of leaf nodes for the given FBT.

Please write a program to determine the stop positionPfor each test case.

 


For each test cases the range of two parametersDandIis as below:

 

\begin{displaymath}2 \le D \le 20, \mbox{ and } 1 \le I \le 524288.\end{displaymath}

 

 

 

Input

Containsl+2 lines.


Line 1 		 I the number of test cases 
Line 2 		 $D_1 \ I_1$
test case #1, two decimal numbers that are separatedby one blank 
... 		 		 
Line k+1 $D_k \ I_k$
test case #k 
Line l+1 $D_l \ I_l$
test case #l 
Line l+2 -1 		 a constant -1 representing the end of the input file

 

Output

Containsllines.


Line 1 		 the stop position P for the test case #1 
... 		 
Line k the stop position P for the test case #k 
... 		 
Line l the stop position P for the test case #l

 

Sample Input

 

5
4 2
3 4
10 1
2 2
8 128
-1

 

Sample Output

12
7
512
3
255

 

 

本题必需找到数学规律(只模拟最后一个小球),而不能用模拟法,否则超时

 

#define RUN
#ifdef RUN

#include<stdio.h>
int main() {

#ifndef ONLINE_JUDGE
	freopen("679.in", "r", stdin);
	freopen("679.out", "w", stdout); 
#endif

	int cnt;
	int D, I;
  
	scanf("%d", &cnt);

	for(int t=0; t<cnt; t++){
		scanf("%d%d", &D, &I);

		//指向编号为k的小球
		int k = 1;

		//直接模拟最后一个小球的路线
		for(int i = 0; i < D-1; i++)
			//I既代表了第I个小球,又表示了第I次经过编号为k的小球
			//根据经过次数I的奇偶性判断下一个经过小球的编号k'
			//和经过小球k'的次数I'

			// 经过k编号小球奇数次则向左走到编号为2*k的小球
			// 并更新经过2*k小球的次数为(I+1)/2
			if(I%2) { k = k*2; I = (I+1)/2; }
			// 经过k编号小球偶数次则向右走到编号为2*k+1的小球
			// 并更新经过2*k+1小球的次数为I/2
			else { k = k*2+1; I /= 2; }

		printf("%d\n", k);
	}

	return 0;
}

#endif



 

 

 

模拟所有小球,超时

 

//#define RUN
#ifdef RUN

#include<stdio.h>
#include<string.h>
//最大深度
const int MAXD = 20;
//最大节点个数2^MAXD-1
int s[1<<MAXD];
int main() {

#ifndef ONLINE_JUDGE
	freopen("679.in", "r", stdin);
	freopen("679.out", "w", stdout); 
#endif

  int cnt;
  int D, I;
  scanf("%d", &cnt);

  for(int t=0; t<cnt; t++){
	scanf("%d%d", &D, &I);
	// 开关
	memset(s, 0, sizeof(s));
	/*
	for(int j=0; j<5; j++){
		printf("%d", s[j]);
	}
	printf("\n");
	*/
	int k, n = (1<<D)-1;
	for(int i = 0; i < I; i++) {
		k = 1;
		for(;;) {
		//开关转换
		s[k] = !s[k];
		//根据开关状态选择下落方向
		k = s[k] ? k*2 : k*2+1;

		//已经落出界了
		if(k > n) break;
		}
	}
	//出界前的叶子编号
	printf("%d\n", k/2);
  }

  return 0;
}

#endif



 

分享到:
评论

相关推荐

    textlint-rule-no-dropping-i:Textlint规则来检查马虎字

    textlint-rule-no-dropping-i这是一个检测丢失单词的规则。 are我们正在发展。 developing我在发展。安装npm install @textlint-ja/textlint-rule-no-dropping-i用法将@textlint-ja/textlint-rule-no-dropping-i ....

    textlint-rule-no-dropping-the-ra:Textlint规则以检出单词

    人が来れないんです安装npm install textlint-rule-no-dropping-the-ra用法将“ no-drop-the-ra” .textlintrc { " rules " : { " no-dropping-the-ra " : true }}贡献叉吧! 创建功能分支: git checkout -b my-new...

    Ball-Dropping-Game:827游戏的第一款游戏

    《Ball-Dropping-Game:827游戏的第一款游戏》是一款基于JavaScript开发的趣味小游戏,其核心玩法是操控角色抛掷小球。作为827游戏工作室的首款作品,这款游戏展示了JavaScript在创建互动娱乐体验方面的潜力。下面...

    TCL-script-to-find-wireless-packet-dropping-nodes_Windows编程_tcl/tk_

    TCL script to find wireless packet dropping nodes2609002STC12C5A60S2+2.4TFT+DS1302+DS18B20+GPS+FM 自动校时收音机电子钟 STC12C5A60S2+2.4TFT+DS1302+DS18B20+GPS+FM 自动校时收音机电子钟 捣鼓了许久

    textlint-rule-no-insert-dropping-sa:使用Textlint规则检查是否滥用表达式

    textlint规则无插入删除规则使用Textlint规则检查是否滥用表达式这是一条规则,用于检查具有额外“ ”的句子,反之则缺少“ ”。从引述。该规则包含某种感官类比,因为随着时间的推移,某些单词在带有或不带有“ sa...

    tenon:页面可视化搭建工具。A visual dragging-and-dropping page builder base on Vue. what you see is what you get

    tenon名称由来Tenon,取自 mortise and tenon(卯榫)。由木工卯榫结构(tenon structure)启发,在规则内保持灵活性。榫卯作为一种传统Craft.io是一种文化,也代表着一种工匠精神,更是一种精致的生活体现~愿景内容...

    [.Net 控件] Ajax Control Toolkit

    Using the Ajax Control Toolkit, you can build Ajax-enabled ASP.NET Web Forms applications by dragging-and-dropping Toolkit controls from the Visual Studio Toolbox onto an ASP.NET Web Forms page. ...

    Dropping-Thunder:Dropping Thunders TD模拟器

    【Dropping-Thunder:Dropping Thunders TD模拟器】 Dropping-Thunder是一个基于Java开发的塔防(TD)游戏模拟器,它允许用户创建、编辑并玩转自定义的塔防地图。塔防游戏,全称Tower Defense,是策略游戏的一种,...

    ICO编辑工具

    Plus, the program is really easy to use and supports keyboard shortcuts, drag-and-dropping files and other goodies. You'll only need to arrange the different windowed menus on the interface, and you'...

    Traveloggia-Transitional:使用新后端和新样式的旧angularjs客户端

    Traveloggia-Transitional,这是由Nginx服务器在数字海洋Droplet 64.225.39.26上托管的angular.js应用-Dropping正在运行ubuntu 连接到小滴ssh carte-blanche-user@64.225.39.26 角度网页的路径 / var / ...

    A novel differentiated services supporting scheme for optical burst switched networks

    In this letter, we analyze the drawback of tail-dropping contention resolution in optical burst switched networks. Once contention occurs, we adopt modified head-dropping policy to resolve contention....

    Condorcet with Dual Dropping-开源

    《Condorcet with Dual Dropping:开源选举决策机制解析》 在当代社会,选举和投票是决策过程中的重要环节,特别是在组织内部、社区选举以及更广泛的公共事务中。 Condorcet with Dual Dropping 是一种先进的选举...

    Advanced201905.pdf

    - jaw-dropping(令人瞠目结舌的) 5. 高频词汇:为了加强词汇学习,材料中还提到一些常见而重要的单词,例如: - wealth(财富) - intertwine(缠绕) - tingle(感到刺痛) - concoction(混合物) - ...

    Dropping Ball using javascript.zip

    "Dropping Ball using javascript.zip" 提供了一个简单的JavaScript游戏示例,名为“Balldrop”,它利用了JavaScript的核心功能来实现一个动态的球下落效果。这个项目非常适合初学者学习,因为它涉及到基础的HTML、...

    Dropping-softbody-in-2D:自学项目

    2D滴入式软体 自学项目我一直对计算机模拟物理世界充满热情。 最近,我试图在二维中模拟与刚体碰撞时的软体变形。 该程序仍未完成,但是现在我将其留在这里,过一会儿我将返回它。

    异常问题1

    描述中提到了“dmesg”日志,这是一个用于查看内核消息的实用程序,当MTK Wi-Fi出现问题时,日志显示“ieee80211 phy0: rt2x00queue_write_tx_frame: Error - Dropping frame due to full tx queue”,这表明在数据...

    Android代码-react-native-video

    react-native-video A &lt;Video&gt; component for react-native, as seen in react-native-login! Version 4.x requires react-... Google is dropping support for apps using target SDKs older than 26 as of Oc

    Raize.Components-v6.1.12 FullSource(2009-XE8) Part1/2

    * When dragging a file from a TRzShellList and dropping it onto a different folder in the associated TRzShellTree, the shell tree no longer scrolls to the source folder represented by the shell ...

    Raize.Components-v6.1.12 FullSource(2009-XE8) Part2/2

    * When dragging a file from a TRzShellList and dropping it onto a different folder in the associated TRzShellTree, the shell tree no longer scrolls to the source folder represented by the shell ...

Global site tag (gtag.js) - Google Analytics