HDU 1006 Tick and Tick 解不等式解法

一开始思考的时候觉得好难的题目,因为感觉很多情况,不知道从何入手。

想通了就不难了。

可以转化为一个利用速度建立不等式,然后解不等式的问题。

建立速度,路程,时间的模型如下:

/***************************************************************************
*								  *
*	秒钟的速度s=6°/s,分针是1/10°/s,时针是1/120°/s			  *
*	所以相对速度s_m=59/10°/s,s_h=719/120°/s,m_h=120/11°/s		  *
*	所以相差一度所需要的时间sm=10/59 s/°,sh=120/719 s/°,mh=120/11 s/°   *
*	他们差360°的周期为tsm=3600/59 s,tsh=43200/719 s,tmh=43200/11 s	  *
*	需要相差的角度为n。                                                          *
*	rsm>n → n*sm+k1*tsm < t < tsm-n*sm+k1*tsm;			  *
*	rsh>n → n*sh+k2*tsh < t < tsh-n*sh+k2*tsh;		           *
*	rmh>n → n*mh+k3*tmh < t < tmh-n*mh+k3*tmh;			  *
*	三个条件都满足所占的总时间即为时针、分针、秒针相差角度大于n的总时间          *
*								  *
***************************************************************************/

以上内容出处: http://acm.hdu.edu.cn/discuss/problem/post/reply.php?postid=4499&messageid=1&deep=0

就是利用中学的公式了:物体1的速度是V1,物体2的速度是V2,那么经过一段时间t,他们的距离差S = V1*t - V2*t

这里的物体就是时针,分针, 秒针,然后就建模成这样一个简单的物理模型。能把这个模型建立起来,一切就解决了。

当然,这也是最难的地方。

参考一下这个博客:http://www.cnblogs.com/kuangbin/archive/2011/12/04/2275470.html

下面是我的代码:

#include <algorithm>
#include <cstdio>
#include <utility>
using namespace std;

class TickAndTick_1
{
	void intersectRange(pair<double, double> &rs,
		pair<double, double> &a, pair<double, double> &b)
	{
		rs.first = max(a.first, b.first);
		rs.second = min(a.second, b.second);
		if (rs.first > rs.second) rs.first = rs.second = 0.0;
	}

	void solveInequality(double a, double b, double D, pair<double, double> &rs)
	{
		if (a == 0.0) return;

		pair<double, double> pa;
		if (a < 0.0)
		{
			pa.first = (360.0-b-D) / a;
			pa.second = (D-b) / a;
		}
		else
		{
			pa.first = (D-b) / a;
			pa.second = (360.0-b-D) / a;
		}
		pair<double, double> sixty(0.0, 60.0);
		intersectRange(rs, pa, sixty);
	}

	double happyInMinute(int h, int m, double D)
	{
		pair<double, double> pas[3][2];

		double a = 1.0 / 120.0 - 0.1;
		double b = 30.0 * h + m/2.0 - 6.0*m;
		solveInequality(a, b, D, pas[0][0]);
		solveInequality(-a, -b, D, pas[0][1]);

		a = 1.0/120 - 6.0;
		b = 30*h + m/2.0;
		solveInequality(a, b, D, pas[1][0]);
		solveInequality(-a, -b, D, pas[1][1]);

		a = 0.1 - 6;
		b = 6 * m;
		solveInequality(a, b, D, pas[2][0]);
		solveInequality(-a, -b, D, pas[2][1]);

		double ans = 0.0;
		pair<double, double> tmp_1, tmp_2;
		for (int i = 0; i < 2; i++)
		{
			for (int j = 0; j < 2; j++)
			{
				intersectRange(tmp_1, pas[0][i], pas[1][j]);

				intersectRange(tmp_2, tmp_1, pas[2][0]);
				ans += tmp_2.second - tmp_2.first;

				intersectRange(tmp_2, tmp_1, pas[2][1]);
				ans += tmp_2.second - tmp_2.first;
			}
		}
		return ans;
	}

public:
	TickAndTick_1()
	{
		double D;
		while(scanf("%lf",&D) && -1 != D)
		{
			double ans = 0.0;
			int h, m;
			for (h = 0; h < 12; h++)
			{
				for(m = 0; m < 60; m++)
				{
					ans += happyInMinute(h, m, D);
				}
			}
			printf("%.3lf\n",ans * 100.0 / (60.0 * 60.0 * 12.0));
		}
	}
};

HDU 1006 Tick and Tick 解不等式解法

时间: 2024-11-05 18:35:56

HDU 1006 Tick and Tick 解不等式解法的相关文章

HDU 1006 Tick and Tick(时钟,分钟,秒钟角度问题)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1006 Tick and Tick Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22203    Accepted Submission(s): 5877 Problem Description The three hands of the

hdu 1006 Tick and Tick 有技巧的暴力

Tick and Tick Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16707    Accepted Submission(s): 4083 Problem Description The three hands of the clock are rotating every second and meeting each ot

hdu 1006 Tick and Tick

Tick and Tick Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19764    Accepted Submission(s): 5164 Problem Description The three hands of the clock are rotating every second and meeting each ot

HDU 1006 Tick and Tick 时钟指针问题

Tick and Tick Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10194    Accepted Submission(s): 2859 Problem Description The three hands of the clock are rotating every second and meeting each ot

HDU 4006 The kth great number AVL解法

给出动态更新数据,实时问第K个大的数值是什么? 利用AVL数据结构做的一个统计数,比较高级的数据结构内容了. 不知道题目给出的数据值是否有重复,因为我下面的程序是可以处理出现数据重复的情况的. 其中的奥妙是增加了repeat的信息,可以知道出现了当前数组多少次. 主要是知道如何维护这些数据和如何查询,维护数据的函数是pushUp,查询函数是selectKth. 其他就是一般的AVL操作.个人觉得我的AVL写的还算蛮清晰的,有需要的参考一下. #include <stdio.h> inline

HDU1006 Tick and Tick【计算几何】

Tick and Tick Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10770    Accepted Submission(s): 3008 Problem Description The three hands of the clock are rotating every second and meeting each o

HDU 1698 Just a Hook 线段树解法

很经典的题目,而且是标准的线段树增加lazy标志的入门题目. 做了好久线段树,果然是practice makes perfect, 这次很畅快,打完一次性AC了. 标志的线段树函数. 主要是: 更新的时候只更新到需要的节点,然后最后的时候一次性把所以节点都更新完毕. 这也是线段树常用的技术. #include <stdio.h> const int SIZE = 100005; struct Node { bool lazy; int metal; }; const int TREESIZE

Square Coins (HDU 1398) ———母函数模板详解

Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7748    Accepted Submission(s): 5238 Problem Description People in Silverland use square coins. Not only they have square shapes but

HDU 2829 Lawrence(动态规划-四边形不等式)

Lawrence Problem Description T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in the Arabian theater and led a group of Arab nationals in guerilla strikes against the Ottoman Empire. His primary target