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

从3700ms到700ms,仅仅是因为变量定义的位置不同?

Posted by zhoujay1987 at 2009-04-18 11:13:40 on Problem 1054 and last updated at 2009-04-18 11:14:41
#include <iostream>
#include <algorithm>
using namespace std;

struct point {
	short x, y;
};

int comp (const void *a, const void *b)
{
	if (((point*)a)->x > ((point*)b)->x) return 1;
	if (((point*)a)->x < ((point*)b)->x) return -1;
	return ((point*)a)->y - ((point*)b)->y;
}
point p[5001];
bool tab[5001][5001];

int main()
{
//	freopen("1.txt", "r", stdin);
	short max, tmp, r, c, n, i, j, cx, cy, dx, dy;  // 如果把此处的变量定义成全局变量的话,耗时就是3700ms了
	bool flag;

	max = 0;
	cin >> r >> c >> n;
	for (i = 0; i < n; ++i)
	{
		scanf ("%d %d", &p[i].x, &p[i].y);
		tab[p[i].x][p[i].y] = true;
	}

	qsort (p, n, sizeof (point), comp);
	for (i = 0; i < n; ++i)
	{
		for (j = i + 1; j < n; ++j)
		{
			dx = p[j].x - p[i].x;
			dy = p[j].y - p[i].y;
			cx = p[i].x - dx;
			cy = p[i].y - dy;

			if (cx >= 1 && cy >= 1 && cy <= c) continue;
			if (dx && dx * max > r) continue;
			if (dy && (dy * max > c || -dy * max > c)) continue;
			
			cx = p[j].x + dx;
			cy = p[j].y + dy;
			tmp = 2;

			flag = true;
			while (cx <= r && cy <= c && cy >= 1) 
			{
				if (!tab[cx][cy]) 
				{
					flag = false;
					break;
				}
				cx += dx;
				cy += dy;
				tmp++;
			}
			if (flag && tmp > 2 && tmp > max) max = tmp;
		}
	}
	cout << max << endl;
	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