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 a280920481 at 2018-10-14 21:37:23 on Problem 1328
//注意:在输入岛的位置时,即使发现了一个岛不可能被覆盖到,也要继续把此次数据接收完
//这个代码用2个堆对岛的左交点和右交点分别排序,再利用贪心法求解

#include <math.h>
#include <iostream>
using namespace std;


struct Position
{
	int x;
	int y;
}posi;

int N = 0, D = 0;
int heap1_size = 0;
int heap2_size = 0;
struct Situ
{
	double p1;
	double p2;
	short num;
}heap1[1003], heap2[1003];

bool dp[1003];

void heap1_push(Situ a);
void heap1_pop();
void heap2_push(Situ a);
void heap2_pop();


int main()
{
	double m = 0;
	double lasttower;
	Situ mSitu;
	int case_number = 1;
	bool IsImposible = false;
	while (true)
	{
		scanf_s("%d%d", &N, &D);

		heap2_size = 0;
		heap1_size = 0;
		memset(dp, 0, sizeof(dp));
		memset(heap1, 0, sizeof(heap1));
		memset(heap2, 0, sizeof(heap2));
		IsImposible = false;

		if (N == 0)
		{
			return 0;
		}

		for (int i = 0; i < N; i++)
		{
			scanf_s("%d%d", &posi.x, &posi.y);
			if (posi.y > D)
			{
				IsImposible = true;
			}
			mSitu.num = i;
			if (!IsImposible)
			{
				m = sqrt((double)D*D - posi.y*posi.y);
			}
			mSitu.p1 = posi.x - m;
			mSitu.p2 = posi.x + m;
			heap1_push(mSitu);
			m = mSitu.p1;
			mSitu.p1 = mSitu.p2;
			mSitu.p2 = m;
			heap2_push(mSitu);
		}
		if (IsImposible)
		{
			printf_s("Case %d: -1\n", case_number);
			case_number++;
			continue;
		}


		lasttower = heap2[0].p1;
		dp[0] = true;
		int Answer = 1;

		while (true)
		{

			while (heap1[0].p1 <= lasttower)
			{
				dp[heap1[0].num] = true;
				N--;
				if (!N)
				{
					printf_s("Case %d: %d\n", case_number, Answer);
					break;
				}
				heap1_pop();
			}
			if (!N)
			{
				break;
			}
			while (dp[heap2[0].num])
			{
				heap2_pop();
			}
			dp[heap2[0].num] = true;
			lasttower = heap2[0].p1;
			Answer++;

		}
		case_number++;
	}
	return 0;
}

void heap1_push(Situ a)
{
	static Situ ls;
	static int fa;
	static int m;

	m = heap1_size;
	heap1[m] = a;
	heap1_size++;

	while (m)
	{
		fa = (m - 1) / 2;

		if (heap1[fa].p1 > heap1[m].p1)
		{
			ls = heap1[fa];
			heap1[fa] = heap1[m];
			heap1[m] = ls;
		}
		else
		{
			break;
		}
		m = fa;
	}
	return;

}

void heap1_pop()
{
	static Situ ls;
	static int son;
	static int m;

	heap1_size--;
	heap1[0] = heap1[heap1_size];
	m = 0;

	while (2 * m + 1 < heap1_size)
	{
		son = 2 * m + 1;

		if ((heap1[son].p1 > heap1[son + 1].p1) && (son + 1 < heap1_size))
		{
			son++;
		}

		if (heap1[son].p1 < heap1[m].p1)
		{
			ls = heap1[son];
			heap1[son] = heap1[m];
			heap1[m] = ls;
		}
		else
		{
			break;
		}
		m = son;
	}
	return;

}

void heap2_push(Situ a)
{
	static Situ ls;
	static int fa;
	static int m;

	m = heap2_size;
	heap2[m] = a;
	heap2_size++;

	while (m)
	{
		fa = (m - 1) / 2;

		if (heap2[fa].p1 > heap2[m].p1)
		{
			ls = heap2[fa];
			heap2[fa] = heap2[m];
			heap2[m] = ls;
		}
		else
		{
			break;
		}
		m = fa;
	}
	return;

}

void heap2_pop()
{
	static Situ ls;
	static int son;
	static int m;

	heap2_size--;
	heap2[0] = heap2[heap2_size];
	m = 0;

	while (2 * m + 1 < heap2_size)
	{
		son = 2 * m + 1;

		if ((heap2[son].p1 > heap2[son + 1].p1) && (son + 1 < heap2_size))
		{
			son++;
		}

		if (heap2[son].p1 < heap2[m].p1)
		{
			ls = heap2[son];
			heap2[son] = heap2[m];
			heap2[m] = ls;
		}
		else
		{
			break;
		}
		m = son;
	}
	return;
}

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