CSP 202006-1 线性分类器

  • 题目链接:线性分类器

  • 题目描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 分析:

    • 判断一群点是否在线的同一侧,可以把他们的坐标带入线解析式,如果都大于0或都小于0,可以判断它们在同一侧
    • 数据结构上,可以用pair存点,用vector<pair>存一群点
  • 满分代码

    #include<iostream>
    #include<vector>
    #include<map>
    #include<string> 
    using namespace std;
    
    vector<pair<int,int>> A;	//A点集 
    vector<pair<int,int>> B;	//B点集
    
    int main()
    {
          
          
    	ios::sync_with_stdio(0);
    	cin.tie(0);
    	cout.tie(0);
    	
    	int n,m;	//点个数、查询个数 
    	cin>>n>>m;
    	
    	int x,y;
    	char type;
    	for(int i=0;i<n;i++)
    	{
          
          	
    		cin>>x>>y>>type;
    		type == 'A' ? A.push_back(make_pair(x,y)) : B.push_back(make_pair(x,y));		
    	}
    
    	int t[3];	//线解析式 
    	bool flag,res;	
    	for(int q=0;q<m;q++)
    	{
          
          
    		cin>>t[0]>>t[1]>>t[2];
    		res = true;
    		
    		if(!A.empty())
    		{
          
          
    			x = A[0].first; y = A[0].second;
    			flag = t[0]+x*t[1]+y*t[2] > 0;	
    		}
    		else
    		{
          
          
    			x = B[0].first; y = B[0].second;
    			flag = t[0]+x*t[1]+y*t[2] < 0;
    		}
    		
    		//判断A点集是否在同一侧 
    		for(int i=1;i<A.size();i++)
    		{
          
          
    			x = A[i].first; y = A[i].second;
    			if(t[0]+x*t[1]+y*t[2]>0 != flag)
    			{
          
          
    				res = false;
    				break; 
    			}	
    		}
    		
    		//判断B点集是否在同一侧 
    		if(res)
    		{
          
          
    			for(int i=0;i<B.size();i++)
    			{
          
          
    				x = B[i].first; y = B[i].second;
    				if(t[0]+x*t[1]+y*t[2]>0 == flag)
    				{
          
          
    					res = false;
    					break; 
    				}	
    			}
    		}
    		
    		if(!res)
    			cout<<"No"<<endl;
    		else
    			cout<<"Yes"<<endl;
    	}
    	
    	return 0;	
    } 
    /*
    9 3
    1 1 A
    1 0 A
    1 -1 A
    2 2 B
    2 3 B
    0 1 A
    3 1 B
    1 3 B
    2 0 A
    0 2 -3
    -3 0 2
    -3 1 1 
    
    */
    

猜你喜欢

转载自blog.csdn.net/wxc971231/article/details/108531001