CSP202006-1线性分类器

试题编号: 202006-1
试题名称: 线性分类器
时间限制: 1.0s
内存限制: 512.0MB

#include<iostream>
#define N 1001
#define M 21
using namespace std;
typedef struct point{
    int x;
    int y;
    char type;
}point;//记录点的x,y,type

typedef struct fxy{
    long long int a;
    long long int b;
    long long int c;
}fxy; //记录线性函数的三个参数 西塔012

point dian[N]; //点数组
fxy f[M];      //函数数组

int n,m; //点 查询的个数

int judgeP(long long int *a, int len){ //判断数组中数据是否都大于0
    for(int i=0;i<len;i++){
        if(a[i] < 0){
            return 0;
        }
    }
    return 1;
}

int judgeN(long long int *a, int len){ //判断数组中数组是否都小于0
    for(int i=0;i<len;i++){
        if(a[i] > 0){
            return 0;
        }
    }
    return 1;
}

int judge(point *a, fxy *b, int i){ //分类A,B点; i为第i个查询的个数
    int j,l=1,p=1,flag1,flag2,flag3,flag4;
    long long int A[N] = {0};
    long long int B[N] = {0};
    
    for(j = 1;j<=n;j++){
        if(a[j].type == 'A'){ //若是A点,将其对应函数值存入数组A
            A[l] = b[i].a + b[i].b * a[j].x + b[i].c * a[j].y;
            l++;
        }
        if(a[j].type == 'B'){ //若是B点
            B[p] = b[i].a + b[i].b * a[j].x + b[i].c * a[j].y;
            p++;
        }
    }
    flag1 = judgeP(A,l);
    flag2 = judgeP(B,p);
    flag3 = judgeN(A,l);
    flag4 = judgeN(B,p);
    
    if((flag1 && flag4) || (flag2 && flag3)){
        return 1;
    }else{
        return -1;
    }
}

int main(int argc,char** argv){
    cin>>n>>m;
    int i,j;
    for(i=1;i<=n;i++){
        cin>>dian[i].x>>dian[i].y;
        cin>>dian[i].type;
    }
    
    for(j=1;j<=m;j++){
        cin>>f[j].a>>f[j].b>>f[j].c;    
    }
    
    for(i=1;i<=m;i++){
        if(judge(dian,f,i) == 1){
            cout<<"Yes"<<endl;
        }else{
            cout<<"No"<<endl;
        }
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_18287147/article/details/107595880