PLA算法:

https://blog.csdn.net/red_stone1/article/details/70866527

The problem:

Analysis:

题目链接可见:https://vjudge.net/contest/313395#problem/M

Reference codes:

 #include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; const int N = ; int n; int sgn(long long x) {
return (x > ) - (x < );
} struct P {
long long d[];
long long& operator[](int x) {
return d[x];
}
P () {}
P (long long a, long long b, long long c) {
d[] = a; d[] = b; d[] = c;
}
}; struct node {
P x;
int y;
} p[N]; P operator+(P a, P b) {
P c;
for (int i = ; i < ; i++)
c[i] = a[i] + b[i];
return c;
} P operator-(P a, P b) {
P c;
for (int i = ; i < ; i++)
c[i] = a[i] - b[i];
return c;
} P operator*(int a, P b) {
P c;
for (int i = ; i < ; i++)
c[i] = a * b[i];
return c;
} bool operator<(P a, P b) {
return a[] < b[] || (a[] == b[] && a[] < b[]);
} long long operator*(P a, P b) {
return a[] * b[] - a[] * b[];
} long long operator^(P a, P b) {
return a[] * b[] + a[] * b[];
} long long det(P a, P b, P c) {
return (b - a) * (c - a);
} struct L {
P a, b;
L () {}
L (P x, P y) {
a = x; b = y;
}
}; bool onSeg(P p, L s) {
return sgn(det(p, s.a, s.b)) == && sgn((s.a - p) ^ (s.b - p)) <= ;
} vector<P> convex(vector<P> p) {
vector<P> ans, S;
for (int i = ; i < p.size(); i++) {
while (S.size() >=
&& sgn(det(S[S.size() - ], S.back(), p[i])) <= )
S.pop_back();
S.push_back(p[i]);
}
ans = S;
S.clear();
for (int i = (int)p.size() - ; i >= ; i--) {
while (S.size() >=
&& sgn(det(S[S.size() - ], S.back(), p[i])) <= )
S.pop_back();
S.push_back(p[i]);
}
for (int i = ; i + < S.size(); i++)
ans.push_back(S[i]);
return ans;
} bool PointInPolygon(P p, vector<P> poly) {
int cnt = ;
for (int i = ; i < poly.size(); i++) {
if (onSeg(p, L(poly[i], poly[(i + ) % poly.size()]))) return true;
int k = sgn(det(poly[i], poly[(i + ) % poly.size()], p));
int d1 = sgn(poly[i][] - p[]);
int d2 = sgn(poly[(i + ) % poly.size()][] - p[]);
if (k > && d1 <= && d2 > ) cnt++;
if (k < && d2 <= && d1 > ) cnt--;
}
if (cnt != ) return true;
return false;
} bool SegmentIntersection(L l1, L l2) {
long long c1 = det(l1.a, l1.b, l2.a), c2 = det(l1.a, l1.b, l2.b);
long long c3 = det(l2.a, l2.b, l1.a), c4 = det(l2.a, l2.b, l1.b);
if (sgn(c1) * sgn(c2) < && sgn(c3) * sgn(c4) < ) return true;
if (sgn(c1) == && onSeg(l2.a, l1)) return true;
if (sgn(c2) == && onSeg(l2.b, l1)) return true;
if (sgn(c3) == && onSeg(l1.a, l2)) return true;
if (sgn(c4) == && onSeg(l1.b, l2)) return true;
return false;
} bool ConvexHullDivide(vector<P> p1, vector<P> p2) {
for (int i = ; i < p1.size(); i++)
if (PointInPolygon(p1[i], p2))
return false;
for (int i = ; i < p2.size(); i++)
if (PointInPolygon(p2[i], p1))
return false;
for (int i = ; i < p1.size(); i++)
for (int j = ; j < p2.size(); j++)
if (SegmentIntersection(L(p1[i], p1[(i + ) % p1.size()]), L(p2[j], p2[(j + ) % p2.size()])))
return false;
return true;
} bool check() {
vector<P> p1, p2;
for (int i = ; i <= n; i++) {
if (p[i].y == )
p1.push_back(p[i].x);
else
p2.push_back(p[i].x);
}
vector<P> c1, c2;
c1 = convex(p1);
c2 = convex(p2);
if (ConvexHullDivide(c1, c2)) return true;
return false;
} int f(P w, P x) {
long long sum = ;
for (int i = ; i < ; i++)
sum += w[i] * x[i];
return sgn(sum);
} void PLA() {
P w = P(, , );
int i = , cnt = ;
long long cc = ;
while (true) {
cc++;
if (f(w, p[i].x) != p[i].y) {
w = w + p[i].y * p[i].x;
cnt = ;
}
else {
if (++cnt == n) break;
}
i = i + ;
if (i > n) i -= n;
}
cout << cc << endl;
} int main() {
int testcase;
cin >> testcase;
while (testcase--) {
cin >> n;
for (int i = ; i <= n; i++) {
cin >> p[i].x[] >> p[i].x[] >> p[i].y;
p[i].x[] = ;
}
if (!check())
puts("Infinite loop!");
else {
puts("Successful!");
}
}
return ;
}

杭电oj初体验之 Code的更多相关文章

  1. C#利用POST实现杭电oj的AC自动机器人,AC率高达50%~~

    暑假集训虽然很快乐,偶尔也会比较枯燥,,这个时候就需要自娱自乐... 然后看hdu的排行榜发现,除了一些是虚拟测评机的账号以外,有几个都是AC自动机器人 然后发现有一位作者是用网页填表然后按钮模拟,, ...

  2. 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)

    今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...

  3. 杭电oj————2057(java)

    question:A+ B again 思路:额,没啥思路/捂脸,用java的long包里的方法,很简单,只是有几次WA,有几点要注意一下 注意:如果数字有加号要删除掉,这里用到了正则表达式“\\+” ...

  4. 杭电oj 2095 & 异或^符号在C/C++中的使用

    异或^符号,在平时的学习时可能遇到的不多,不过有时使用得当可以发挥意想不到的结果. 值得注意的是,异或运算是建立在二进制基础上的,所有运算过程都是按位异或(即相同为0,不同为1,也称模二加),得到最终 ...

  5. 用python爬取杭电oj的数据

    暑假集训主要是在杭电oj上面刷题,白天与算法作斗争,晚上望干点自己喜欢的事情! 首先,确定要爬取哪些数据: 如上图所示,题目ID,名称,accepted,submissions,都很有用. 查看源代码 ...

  6. 杭电oj 4004---The Frog Games java解法

    import java.util.Arrays; import java.util.Scanner; //杭电oj 4004 //解题思路:利用二分法查找,即先选取跳跃距离的区间,从最大到最小, // ...

  7. 爬取杭电oj所有题目

    杭电oj并没有反爬 所以直接爬就好了 直接贴源码(参数可改,循环次数可改,存储路径可改) import requests from bs4 import BeautifulSoup import ti ...

  8. 浅谈IT技术女转战微电商初体验

    今天闲来无事,突然想翻看下之前写的技术博客,很是意外,居然那么多阅读量,于是想想做微商也有一段时间了,决定写写初入微商的初体验. 先自我介绍一下,本人是一名理工女,做IT行业的,这个行业也许有人了解, ...

  9. 杭电OJ 输入输出练习汇总

    主题 Calculate a + b 杭电OJ-1000 Input Each line will contain two integers A and B. Process to end of fi ...

随机推荐

  1. 问题 C: To Fill or Not to Fill

    #include <cstdio> #include <vector> #include <algorithm> #include <cmath> us ...

  2. defender 月考总结

    今天是2019年5月28日,昨天月考了,也是C**生日.昨天考完之后,还是那种考完试的释然感.目前,已经批出来了数学.英语.物理三门学科的成绩,语文还没有批出来.应该明天就能够批出来吧.现在趁着休息, ...

  3. H5_0011:JS动态创建html并设置CSS属性

    1,创建html文本,并设置指定css样式 r = function(e) { var t = document.createElement("div"); t.innerHTML ...

  4. 生产环境容器落地最佳实践 --JFrog 内部K8s落地旅程

    引言 Kubernetes已经成为市场上事实上领先的编配工具,不仅对技术公司如此,对所有公司都是如此,因为它允许您快速且可预测地部署应用程序.动态地伸缩应用程序.无缝地推出新特性,同时有效地利用硬件资 ...

  5. Linux服务器部署.Net Core笔记:目录

        目录 Linux服务器部署.Net Core笔记:一.开启ssh服务 Linux服务器部署.Net Core笔记:二.安装FTP Linux服务器部署.Net Core笔记:三.安装.NetC ...

  6. JS编解码与Java编解码的对应关系

    最近前段在导出数据时会遇到“illegal character”的异常错误,结果发现是在请求地址中请求参数包含了空白字符(其编码为%C2%A0)或者是空格字符(其编码为%20),之前对空格字符情况是做 ...

  7. 微信小程序scroll-view去除滚动条

    css代码: .father{ //父元素 width: 100vw; height: 100vh; overflow-x: hidden; overflow-y: auto; } //隐藏滚动条 : ...

  8. Gin_渲染

    1. 各种数据响应格式 package main import ( "github.com/gin-gonic/gin" "github.com/gin-gonic/gi ...

  9. Unity网络通讯(一)获取计算机的MAC地址

    1 string GetMac() { string mac = ""; mac = GetMacAddressBySendARP(); return mac; } [DllImp ...

  10. C++记录(二)

    1.算术移位和逻辑移位. 逻辑移位是只补0,算术移位是看符号,负数补1,正数补0(讨论的是右移的情况下). 负数左移右边一样补0.如果遇到位运算的相关题目需要对int变量进行左移而且不知道正负,那么先 ...