(The 11th Blue Bridge Cup Provincial Competition) Question I: Plane Segmentation (Finding Rules)

Topic link: "Blue Bridge Cup" Practice System

Analysis: For this question, you can first try to draw a few straight lines to find the rules

These three lines form 7 areas, let's try adding a line to see what happens ( the new line is shown in red )

Since there are only three straight lines originally, there are three cases of the number of intersections formed by the newly added straight line and the original straight line (this figure does not need to consider the case where all straight lines are parallel), which are one intersection, two intersections and three intersections, Let's analyze these three cases separately:

 The first case: the new line has an intersection with the original line

 

Note that the upper and lower lines are parallel, then there are 9 areas at this time, 2 areas are added on the original basis, and the number of intersections between the newly added line and all the original lines is 1

The second case: the new line has two intersections with the original line

 

 At this time, there are 10 areas, 3 areas are added on the basis of the original, and the number of intersection points between the newly added line and all the original lines is 2

Finally, look at the situation where the newly added line forms three intersections with the original line:

 At this time, there are 11 areas, 4 areas are added on the basis of the original, and the number of intersection points between the newly added line and all the original lines is 3

After analyzing these situations, we seem to have found a rule, that is, on the basis of the original graph, if a new straight line and the original straight line form n intersections, the number of parts of the graph formed by adding the straight line will be larger than the original one. The number of parts of the graph is n+1 more

This is true for any situation. With this, we can easily deal with this problem. We first deduplicate all the lines, and then we add a line at the beginning. At this time, the plane will be divided into two parts, and then We add straight lines one by one, and determine the number of intersections with the original straight line to get the number of newly added parts. After processing all the straight lines, we can get the answer.

In the process of finding the intersection point, we need to solve the simultaneous equations. The formula derivation process is given below:

 Finally give the code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
#include<set> 
using namespace std;
const int N=1e3+10;
typedef pair<double,double>PII;
set<PII>s;//存放直线 
set<PII>points;//存放交点 
double k[N],b[N];
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		double kk,bb;
		scanf("%lf%lf",&kk,&bb);
		s.insert({kk,bb});
	}
	int tt=0;
	set<PII>::iterator it;//set迭代器 
	for(it=s.begin();it!=s.end();it++)
	{
		k[++tt]=(*it).first;
		b[tt]=(*it).second;
	}
	long long ans=2;//从第二条直线开始遍历,第一条直线将平面划分为2部分 
	for(int i=2;i<=tt;i++)
	{
		points.clear();
		for(int j=1;j<=i;j++)
		{
			if(k[i]==k[j]) continue;//两直线平行则不可能有交点 
			double x=k[i]*(b[j]-b[i])/(k[i]-k[j]);
			double y=(k[i]*b[j]-k[j]*b[i])/(k[i]-k[j]);
			points.insert({x,y});
		}
		ans+=points.size()+1;//当前直线与其他直线有m个交点那么就会多产生m+1块区域
	}
	printf("%lld",ans);
	return 0;
}

Guess you like

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