杭电oj初体验之 Code
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的更多相关文章
- C#利用POST实现杭电oj的AC自动机器人,AC率高达50%~~
暑假集训虽然很快乐,偶尔也会比较枯燥,,这个时候就需要自娱自乐... 然后看hdu的排行榜发现,除了一些是虚拟测评机的账号以外,有几个都是AC自动机器人 然后发现有一位作者是用网页填表然后按钮模拟,, ...
- 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)
今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...
- 杭电oj————2057(java)
question:A+ B again 思路:额,没啥思路/捂脸,用java的long包里的方法,很简单,只是有几次WA,有几点要注意一下 注意:如果数字有加号要删除掉,这里用到了正则表达式“\\+” ...
- 杭电oj 2095 & 异或^符号在C/C++中的使用
异或^符号,在平时的学习时可能遇到的不多,不过有时使用得当可以发挥意想不到的结果. 值得注意的是,异或运算是建立在二进制基础上的,所有运算过程都是按位异或(即相同为0,不同为1,也称模二加),得到最终 ...
- 用python爬取杭电oj的数据
暑假集训主要是在杭电oj上面刷题,白天与算法作斗争,晚上望干点自己喜欢的事情! 首先,确定要爬取哪些数据: 如上图所示,题目ID,名称,accepted,submissions,都很有用. 查看源代码 ...
- 杭电oj 4004---The Frog Games java解法
import java.util.Arrays; import java.util.Scanner; //杭电oj 4004 //解题思路:利用二分法查找,即先选取跳跃距离的区间,从最大到最小, // ...
- 爬取杭电oj所有题目
杭电oj并没有反爬 所以直接爬就好了 直接贴源码(参数可改,循环次数可改,存储路径可改) import requests from bs4 import BeautifulSoup import ti ...
- 浅谈IT技术女转战微电商初体验
今天闲来无事,突然想翻看下之前写的技术博客,很是意外,居然那么多阅读量,于是想想做微商也有一段时间了,决定写写初入微商的初体验. 先自我介绍一下,本人是一名理工女,做IT行业的,这个行业也许有人了解, ...
- 杭电OJ 输入输出练习汇总
主题 Calculate a + b 杭电OJ-1000 Input Each line will contain two integers A and B. Process to end of fi ...
随机推荐
- python之路(内存,小数据池,编码等)
代码块: python真正的代码块:一个模块,一个函数,一个类,一个文件等都是一个代码块. 但是,在python终端交互模式下,每一条代码都是一个代码块 python在同一个代码块中的变量,初始化对象 ...
- 问题 D: 八皇后
#include <cstdio> #include <vector> #include <algorithm> using namespace std; cons ...
- 书写makefile的注意点
1.空格 定义一个变量时用“foo = 1”这种形式,1后面千万不可以有空格,否则,foo的值为“1 ”.等于号和1之间的空格有无,并没有关系. 2.ifeq ifeq的形式是“ifeq ($(foo ...
- SpringMVC 和 Struts2的区别
SpringMVC 和 Struts2的区别 1.Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方 ...
- IntelliJ IDEA 2019.3安装与激活(附注册码)
转载声明:本文是根据 https://segmentfault.com/a/1190000021220727?utm_source=tag-newest 并结合个人的安装完成情况进行了改动,从而生成此 ...
- 小I的小姐姐
小 I 的小姐姐 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 小 I 去天津玩啦,一路上,他跟他的同学发生了许多有趣 ...
- Alan Walker MV 合辑01 by defender 歌词
其实是歌词浏览,配套Alan Walker MV 合辑01 by defender You were the shadow to my life Did you feel us Another sta ...
- RN开发-ReactJS组件
虚拟DOM :将真实的DOM结构虚拟成json类型数据 props : 不可改变,用于数据传递 state : 组件属性,主要用来存储组件自身需要的数据,每次改变都会引起组件的更新 ...
- python之路模块
time模块 print time.time() print time.mktime(time.localtime()) print time.gmtime() #可加时间戳参数 print time ...
- 《NVMe-over-Fabrics-1_0a-2018.07.23-Ratified》阅读笔记(3)-- 命令
3 命令 Fabrics命令用于创建队列和初始化controller.Fabrics命令的Opcode字段填写0x7F.无论controller是否处于使能状态(CC.EN)Fabrics命令都会被处 ...