HDU 1432(Lining Up)

分为两种情况:

  1. 直线斜率存在,则取斜率相同的点对的最大数量;
  2. 直线斜率不存在,则取横坐标相同的点的最大数量。

最后结果为两种最大数量的较大值。

#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
const int MAXN = 705;

map<double, int> slope; //各斜率出现的次数
map<int, int> X; //各横坐标出现的次数
int x[MAXN], y[MAXN]; //各点坐标

int main()
{
	int n;
	while (cin >> n)
	{
		X.clear();
		int ans = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> x[i] >> y[i];
			ans = max(ans, ++X[x[i]]);

			slope.clear();
			int maxSameSlope = 0; //相同斜率的最大数量
			for (int j = 0; j < i; j++)
			{
				if (x[i] == x[j])
					continue;
				double temp = 1.0 * (y[i] - y[j]) / (x[i] - x[j]);
				maxSameSlope = max(maxSameSlope, ++slope[temp]);
			}
			ans = max(ans, maxSameSlope + 1);
		}
		cout << ans << endl;
	}
	return 0;
}

继续加油。

发布了326 篇原创文章 · 获赞 1 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Intelligence1028/article/details/105307220