(L3-012) Fruit Ninja (Thinking + Enumeration)

Topic link: PTA | Program Design Experiment Aided Teaching Platform

f

Analysis: In this question, let's first look at the special properties of a straight line that satisfies the meaning of the question. If the straight line l can pass through these n line segments, and if l does not pass through the lower endpoint of any line segment, then we can move it to Translate down until it passes the lower endpoint of a line segment. At this time, we cannot continue to translate downward, but we can rotate around the lower endpoint that we have passed until we pass the endpoint of another line segment. That is to say, if there is a solution, we can construct Find another solution to make it pass through the endpoints of the two line segments (the upper and lower endpoints do not matter) , so that the four coordinates required by the answer are all integer solutions.

What exactly does that mean? That is to say, we arbitrarily select a line segment, assuming that there is a straight line passing through its lower endpoint and satisfying the meaning of the question, and then connect with the endpoints of other line segments to obtain the slope range of the target straight line, since we already know a point that the target straight line passes through, then Connect this point and the two endpoints of a line segment to form two straight lines. These two straight lines correspond to a slope respectively. As long as the slope of the last straight line is between the slopes of the two straight lines, it can pass through the line segment, so that we can always Narrow the range of the slope of the target line. If the maximum value of the final slope is greater than the minimum value of the slope, it means that there is a line that meets the meaning of the question and passes through the lower endpoint of the line segment we assumed at the beginning . Since we are narrowing the slope range of the target line in the process, when the current line segment we are traversing reduces the slope range of the target line, it means that the endpoint of the current line segment can be used as a point passed by the target line . The slope range of the two straight lines formed by the traversal point and the current traversal line segment includes the slope range of the target straight line, so it will not have any effect on the slope range of the target straight line. We only need to record the endpoints of the update target interval during the process Yes, at the end, if the interval length of the target line is positive, output it, otherwise, continue to traverse the lower breakpoint of the next line segment until a line that satisfies the meaning of the question is found.

Here is the code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=1e4+10;
struct node{
	int x,y1,y2;
}p[N];
bool cmp(node a,node b)
{
	return a.x<b.x;
}
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		scanf("%d%d%d",&p[i].x,&p[i].y1,&p[i].y2);
	sort(p+1,p+n+1,cmp);
	for(int i=1;i<=n;i++)
	{
		double kmin=-0x3f3f3f3f,kmax=0x3f3f3f3f;
		double tmin,tmax;
		int tx,ty;
		bool flag=true;
		for(int j=1;j<=n;j++)
		{
			if(p[i].x==p[j].x) continue;
			if(i>j)
			{
				tmax=(double)(p[i].y2-p[j].y2)/(p[i].x-p[j].x);
				tmin=(double)(p[i].y2-p[j].y1)/(p[i].x-p[j].x);
				if(tmax<kmax)//斜率最大值被更新,记录更新斜率最大值的端点 
				{
					kmax=tmax;//斜率最小值被更新,记录更新斜率最小值的端点
					tx=p[j].x;ty=p[j].y2;//记录临界点坐标 
				}
				if(tmin>kmin)
				{
					kmin=tmin;
					tx=p[j].x;ty=p[j].y1;//记录临界点坐标 
				}
			}
			else
			{
				tmax=(double)(p[j].y1-p[i].y2)/(p[j].x-p[i].x);
				tmin=(double)(p[j].y2-p[i].y2)/(p[j].x-p[i].x);
				if(tmax<kmax)//斜率最大值被更新,记录更新斜率最大值的端点
				{
					kmax=tmax;
					tx=p[j].x;ty=p[j].y1;//记录临界点坐标 
				}
				if(tmin>kmin)//斜率最小值被更新,记录更新斜率最小值的端点
				{
					kmin=tmin;
					tx=p[j].x;ty=p[j].y2;//记录临界点坐标 
				}
			}
			if(tmin>kmax||tmax<kmin||kmax<kmin)//出现无解,也就是说不存在过第i条直线的下端点的直线满足题意 
			{
				flag=false;
				break;
			}
		}
		if(flag)
		{
			printf("%d %d %d %d",p[i].x,p[i].y2,tx,ty);
			break;
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/AC__dream/article/details/123877235