UVA_10902_Pick-up Sticks

#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<numeric>
#pragma warning(disable:4996)
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::priority_queue;
class Point
{
public:
	double x, y;
	Point() {}
	Point(const double &X, const double &Y)
	{
		x = X;
		y = Y;
	}
};
class Line
{
public:
	double a, b, c;//ax+by=c
	double x_min, x_max, y_min, y_max;//直线的值域
	Line() {}
	Line(const Point &first, const Point &second)
	{
		x_min = std::min(first.x, second.x);
		x_max = std::max(first.x, second.x);
		y_min = std::min(first.y, second.y);
		y_max = std::max(first.y, second.y);
		if (first.x != second.x)//斜率式可行
		{
			b = 1;
			a = -(first.y - second.y) / (first.x - second.x);
			c = first.y + a*first.x;
		}
		else//k->无穷
		{
			b = 0;
			a = 1;
			c = first.x;
		}
	}
	bool lineIntersected(const Line &line)//直线相交的判定
	{
		auto D = a*line.b - line.a*b;
		if (D)
		{
			return true;
		}
		return false;
	}
	bool lineParallel(const Line&line)//判断两直线是否平行
	{
		auto D = a*line.b - line.a*b;
		if (!D)
		{
			return true;
		}
		return false;
	}
	bool lineOverlapped(const Line&line)//判断两直线是否重合(平行的特例)
	{
		auto D = a*line.b - line.a*b;
		auto Dx = c*line.b - line.c*b;
		auto Dy = a*line.c - line.a*c;
		if (!D&&!Dx&&!Dy)
		{
			return true;
		}
		return false;
	}
	Point getIntersection(const Line&line)//行列式求两直线交点
	{
		auto D = a*line.b - line.a*b;
		auto Dx = c*line.b - line.c*b;
		auto Dy = a*line.c - line.a*c;
		return{ Dx / D,Dy / D };
	}
	bool segmentIntersected(const Line &line)
	{
		if (lineIntersected(line))
		{
			auto point = getIntersection(line);
			if (point.x >= x_min&&point.x <= x_max
				&&point.y >= y_min&&point.y <= y_max
				&&point.x >= line.x_min&&point.x <= line.x_max
				&&point.y >= line.y_min&&point.y <= line.y_max
				)//交点在两线段的值域内
			{
				return true;
			}
		}
		return false;
	}
	bool segmentOverlapped(const Line &line)
	{
		if (lineOverlapped(line))
		{
			if (x_min <= line.x_max || x_max >= line.x_min)
			{
				return true;
			}
		}
		return false;
	}
};
class Rectangle
{
public:
	int left, right, top, bottom;
	bool inRectangle(const Point &point)
	{
		if (point.x >= left&&point.x <= right&&point.y >= bottom&&point.y <= top)
		{
			return true;
		}
		return false;
	}
};
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n;
	while (cin >> n&&n)
	{
		vector<Line>line(1);
		for (int i = 1; i <= n; i++)
		{
			double x1, x2, y1, y2;
			cin >> x1 >> y1 >> x2 >> y2;
			line.push_back({ { x1,y1 },{ x2,y2 } });
		}
		vector<bool>overlapped(n + 1);
		for (int i = 1; i <= n; i++)
		{
			for (int j = i+1; j <=n; j++)
			{
				if (line[i].segmentIntersected(line[j]) || line[i].segmentOverlapped(line[j]))
				{
					overlapped[i] = true;
					break;
				}
			}
		}
		cout << "Top sticks:";
		bool flag = false;
		for (int i = 1; i <= n; i++)
		{
			if (!overlapped[i])
			{
				if (flag)
				{
					cout << ',';
				}
				flag = true;
				cout << ' ' << i;
			}
		}
		cout << '.' << endl;
	}
	return 0;
}

时间: 2024-09-29 01:22:41

UVA_10902_Pick-up Sticks的相关文章

uva 10003 Cutting Sticks 简单区间dp

// uva 10003 Cutting Sticks 区间dp // 经典的区间dp // dp(i,j)表示切割小木棍i-j所需要的最小花费 // 则状态转移为dp(i,j) = min{dp(i,k) + dp(k,j) + a[j]-a[i]) // 其中k>i && k<j // a[j] - a[i] 为第一刀切割的代价 // a[0] = 0,a[n+1] = L; // dp数组初始化的时候dp[i][i+1]的值为 0,这表示 // 每一段都已经是切割了的,不

hdu1455 Sticks 深搜 强剪枝

Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6035    Accepted Submission(s): 1704 Problem Description George took sticks of the same length and cut them randomly until all parts becam

POJ-2513 Colored Sticks 【欧拉通路+Trie】

Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color? Input Input is a

POJ 2653 Pick-up sticks [线段相交 迷之暴力]

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12861   Accepted: 4847 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to fin

POJ 1011 - Sticks DFS+剪枝

POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    2. 从大到小枚举,因为小长度更灵活, 可拼接可不拼接    3. 因为每一跟木条都要用到, 故若轮到其中一根原始木段选它的第一根木条时,若第一根木条若不满足,则显然第一根木条在接下来任何一根原始木段都不会满足,故无解    4. 由于所有棒子已排序,在DFS时,若某根棒子未被选,则跳过其后面所有与

poj 1011 Sticks ,剪枝神题

木棒 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 118943 Accepted: 27429 Description 乔治拿来一组等长的木棒.将它们随机地砍断.使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你设计一个程序,帮助乔治计算木棒的可能最小长度.每一节木棍的长度都用大于零的整数表示. Input 输入包括多组数据,每组数据包括两

Pick-up sticks(判断两直线相交)

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11335   Accepted: 4250 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to fin

2015南阳CCPC D - Pick The Sticks 背包DP.

D - Pick The Sticks Description The story happened long long ago. One day, Cao Cao made a special order called "Chicken Rib" to his army. No one got his point and all became very panic. However, Cao Cao himself felt very proud of his interesting

【POJ 2513】Colored Sticks

[POJ 2513]Colored Sticks 并查集+字典树+欧拉通路 第一次做这么混的题..太混了-- 不过题不算难 字典树用来查字符串对应图中的点 每个单词做一个点(包括重复单词 题意就是每个边走且直走一次(欧拉通路 欧拉图的判定: 没有或者只有两个奇数度的点的图叫做欧拉图 有这些就可以解答此题了 另外需要注意题目范围是25W个木棍 所以最多可能有50W个点 卡了好多个RE 代码如下: #include <iostream> #include <cstdlib> #incl

POJ 2513--Colored Sticks【字典树编号 &amp;&amp; 并查集判连通 &amp;&amp; 欧拉路】

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 32351   Accepted: 8536 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a st