2020/06-1 线性分类器

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

注意点:
题目中提到方程的三个参数的绝对值<=10^6
代入返回结果需要用long long型。
如果写成int型,测评结果为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

方法一

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000;
struct node
{
    int x, y;
    char type;
} N[maxn];

typedef long long LL;
LL caculate(int c, int a, int b, node A)
{
    return c + a * A.x + b * A.y;
}

int main()
{
    int n, m;
    cin >> n >> m;
    for(int  i = 0; i < n; i++)
    {
        cin >> N[i].x >> N[i].y >> N[i].type;
    }
    for(int k = 0; k < m; k++)
    {
        bool flag = true;
        int c, a, b; //z = c + ax + by;
        cin >> c >> a >> b;
        LL firstans = caculate(c, a, b, N[0]);
        for(int i = 1; i < n; i++)
        {
            LL ans = caculate(c, a, b, N[i]);
            if(ans * firstans > 0)
            {
                if( N[i].type != N[0].type)
                {
                    flag = false;
                    cout << "No" ;
                    if(k < m-1) cout << endl;
                    break;
                }
            }
            else if(ans * firstans < 0)
            {
                if( N[i].type == N[0].type)
                {
                    flag = false;
                    cout << "No" ;
                    if(k < m-1) cout << endl;
                    break;
                }
            }
        }
        if(flag)
        {
            cout << "Yes" ;
            if(k < m-1) cout << endl;
        }
    }
    return 0;
}

方法二

#include <bits/stdc++.h>
using namespace std;
const int maxn = 110000;
struct node
{
    int x, y;
};
queue<node> A,B; //用于存放A/B类坐标
vector<string> answer;
int result(int b, int x0, int y0, node t)
{
    int z = b + x0 * t.x + y0 * t.y;
    return z;
}
typedef long long LL;
bool istrue(queue<node> A, queue<node> B,int b, int x0, int y0)
{
    LL Afirst, Bfirst;
    LL ansA, ansB;
    Afirst = result(b,x0,y0,A.front()); //获得A第一个答案
    Bfirst = result(b, x0, y0, B.front());//获得B第一个答案
    if(Afirst * Bfirst > 0) return false;//先根据A B的第一个结果Afirst, Bfirst划分上下
    A.pop(); //从第二个开始
    while(!A.empty())
    {
        node tempA = A.front();
        ansA = result(b,x0,y0,tempA); //获得A类第一个结点结果
        A.pop();
        if(ansA * Afirst < 0) return false; //保证A容器里的符号都相同
    }
    B.pop(); //去掉第一个
    while(!B.empty())
    {
        node tempB = B.front();
        ansB = result(b,x0,y0,tempB); //获得B类第一个节点结果
        B.pop();
        if(ansB * Bfirst < 0) return false; //保证B容器里的符号都相同
    }
    return true;
}
int main()
{
    map<char,int> mp;
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
    {
        node p;
        char c;
        cin >> p.x >> p.y >> c;
        if(c == 'A') A.push(p);
        if(c == 'B') B.push(p);
    }

    for(int j = 0; j < m; j++)
    {
        int b, x0, y0; //直线 b + x0 * x + y0 * y;
        cin >> b >> x0 >> y0;
        if(istrue(A, B, b, x0, y0)) cout <<"Yes";
        else cout << "No";
        if(j < m-1) cout << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

moumoumouwang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值