Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

一份比较易读的样例代码

Posted by ajianhouyuan at 2015-12-15 11:19:54 on Problem 1925
// 一份比较易读的样例代码
// 这道题坑很多
// 1. 题目描述不清楚,蜘蛛侠每次切换大楼的高度是一致的,由第一栋build(apartment)决定
// 2. 最后一个build(tower)不一定需要荡过去,只要前一个build摆荡的终点越过了就ok
// 3. sqrt需要转换所有内部数字到double类型,否则会因为转换成int,精度损失导致wa

#include <iostream>
#include <cmath>
#include <string.h>

using namespace std;

typedef struct _BUILD_POS
{
	int x;
	int y;
}BUILD_POS;

const int MAX_BUILD_COUNT = 5000+10;
const int MAX_X_RANGE = 1000000+100;

int get_radius(BUILD_POS b1, BUILD_POS b2)
{
	return (int)sqrt((double)b2.y*(double)b2.y - (double)(b2.y - b1.y) * (double)(b2.y - b1.y));
}

int main()
{
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);

	int k;
	scanf("%d", &k);
	while(k--)
	{
		int build_count;
		scanf("%d", &build_count);

		BUILD_POS build[MAX_BUILD_COUNT];
		int dp[MAX_X_RANGE];
		int ans = 0x7fffffff;

		memset(dp, 0x4f, sizeof(dp));

		for (int i = 0; i < build_count; i++)
		{
			scanf("%d %d", &build[i].x, &build[i].y);
		}

		dp[build[0].x] = 0;

		for (int i = 1; i < build_count; i++)
		{
			int radius = get_radius(build[0], build[i]);
			for (int j = build[i].x - radius; j < build[i].x; j++)
			{
				if (j<0) continue;

				int swing_end_point = build[i].x + (build[i].x - j);

				if (swing_end_point < build[build_count-1].x && dp[swing_end_point] > dp[j] + 1)
				{
					dp[swing_end_point] = dp[j] + 1;
				}

				if (swing_end_point >= build[build_count-1].x
					&& ans > dp[j]+1)
				{
					ans = dp[j]+1;
				}
			}
		}

		if (ans >= 5001)
		{
			printf("-1\n");
		}
		else
		{
			printf("%d\n", ans);
		}

	}

	return 0;
}





Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator