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));    
		}    
	}
};




  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是hdu4310的Java解法: ```java import java.util.*; import java.io.*; public class Main { static int MAXN = 100010; static int MAXM = 200010; static int INF = 0x3f3f3f3f; static int n, m, s, t, cnt; static int[] head = new int[MAXN]; static int[] dis = new int[MAXN]; static boolean[] vis = new boolean[MAXN]; static int[] pre = new int[MAXN]; static int[] cur = new int[MAXN]; static class Edge { int to, next, cap, flow, cost; public Edge(int to, int next, int cap, int flow, int cost) { this.to = to; this.next = next; this.cap = cap; this.flow = flow; this.cost = cost; } } static Edge[] edge = new Edge[MAXM]; static void addEdge(int u, int v, int cap, int flow, int cost) { edge[cnt] = new Edge(v, head[u], cap, flow, cost); head[u] = cnt++; edge[cnt] = new Edge(u, head[v], 0, 0, -cost); head[v] = cnt++; } static boolean spfa() { Arrays.fill(dis, INF); Arrays.fill(vis, false); Queue<Integer> q = new LinkedList<>(); q.offer(s); dis[s] = 0; vis[s] = true; while (!q.isEmpty()) { int u = q.poll(); vis[u] = false; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if (edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost) { dis[v] = dis[u] + edge[i].cost; pre[v] = u; cur[v] = i; if (!vis[v]) { vis[v] = true; q.offer(v); } } } } return dis[t] != INF; } static int[] MCMF(int s, int t) { int flow = 0, cost = 0; while (spfa()) { int f = INF; for (int u = t; u != s; u = pre[u]) { f = Math.min(f, edge[cur[u]].cap - edge[cur[u]].flow); } for (int u = t; u != s; u = pre[u]) { edge[cur[u]].flow += f; edge[cur[u] ^ 1].flow -= f; cost += edge[cur[u]].cost * f; } flow += f; } return new int[]{flow, cost}; } public static void main(String[] args) { Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int T = in.nextInt(); for (int cas = 1; cas <= T; cas++) { n = in.nextInt(); m = in.nextInt(); s = 0; t = n + m + 1; cnt = 0; Arrays.fill(head, -1); for (int i = 1; i <= n; i++) { int c = in.nextInt(); addEdge(s, i, c, 0, 0); } for (int i = 1; i <= m; i++) { int c = in.nextInt(); addEdge(i + n, t, c, 0, 0); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int c = in.nextInt(); addEdge(i, j + n, INF, 0, c); } } int[] ans = MCMF(s, t); System.out.printf("Case #%d: %d\n", cas, ans[1]); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值