杭电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 ...
随机推荐
- jQuery---第一部分复习
jquery: 简单.粗暴 jq和js的关系 js是什么? js是一门编程语言 jq仅仅是基于js的一个库,jq可理解为就是开发js的一个工具. 概念 1. 为什么 ...
- SpringBoot之spring.factories
组件提供者如何编写出仅需系统开发者进行包引入就可以对spring进行bean注入等操作? 其实在spring库中有提供自动化配置的库spring-boot-autoconfigure,我们只需要引 ...
- C 库函数 - modf()
C 库函数 - modf() C 标准库 - <math.h> 描述 C 库函数 double modf(double x, double *integer) 返回值为小数部分(小数点后的 ...
- Vue中常见参数传递方式
文章内容:这里只有vue中父子组件传参.路由间的传参 (另外还有vuex.储存本地.中央bus等方式) 一.父子组件 1.1父传子(props) <!-- 父组件father.vue --> ...
- Oracle 数据库,远程访问 ora-12541:TNS:无监听程序
1.修改网络连接IPV4设置为固定IP IP地址:192.168.100.8子网掩码:255.255.255.0默认网关:192.168.100.1首选DNS:192.168.100.1 2.修改.. ...
- LVS笔试题!
1.集群分类有哪些及各自实现的目标?高可用:保持系统的稳定,防止系统出现单点故障.负载均衡:比如高并发状态下,多个机器分摊请求,从而达到负载均衡高性能运算集群:比如hadoop分布式计算框架,把多个机 ...
- eclipse无法启动报错、打开Eclipse报错、Eclipse无法打开
有时候在新的电脑中安装Eclipse的时候总会报一些错误,要么就是环境变量没配好.要么就是JDK没装.要么就是JDK位数与Eclipse位数不同(版本),反正会报一些奇奇怪怪恶心的问题,我第一次装的时 ...
- 【SQL】基础概念
1.. In order to find the rows where the value for a column is or is not NULL, you would use IS NULL ...
- Linux - Shell - 算术表达式 - 算数运算
概述 shell 中基于 $(()) 的 算数运算 背景 复习 shell 脚本 凑数吧 准备 环境 os centos7 1. 算数运算 代码 #!/bin/bash # $(()) 的数学运算, ...
- C++-函数与指针的关系(回调函数)
1.函数类型 C语言中的函数有自己特定的类型 函数的类型由返回值,参数类型和参数个数共同决定 ★ int add(int i, int j)的类型为int(int, int) C语言中通过typede ...