杭电多校第一场-M-Code
题目描述
In the subject of Machine Learning, there is a classical classification model called perceptron, defined as follows:
Assuming we get a set of training samples: D={(x1,y1),(x2,y2),...,(xN,yN)}, with their inputs x∈Rd, and outputs y∈{−1,1}. We will try to find a function
so that f(xi)=yi,i=1,2,...,N.w,x mentioned above are all d-dimensional vectors, i.e. w=(w1,w2,...,wd), x=(x1,x2,...,xd). To simplify the question, let w0=b, x0=1, then
To solve the problem, we have a algorithm, PLA(Popcorn Label Algorithm).
Accoding to PLA, we will randomly generate w.
If f(x)=sign(wT⋅x) fails to give
any element (xi,yi)∈D the right classification, i.e. f(xi)≠yi, then we will replace w with another random vector. We will do this repeatedly until all the samples ∈D are correctly classified.
As a former-JBer, Tom excels in programming and quickly wrote the pseudocode of PLA.
w := a random vector
while true do
flag:=true
for i:=1 to N do
if f(x[ i ]) != y[ i ] then
flag:=false
break
if flag then
break
else
w := a random vector
return w
But Tom found that, in some occasions, PLA will end up into an infinite loop, which confuses him a lot. You are required to help Tom determine, when performed on a given sample set D, if PLA will end up into an infinite loop. Print Infinite loop! if so, or Successful! otherwise.
We only consider cases when d=2 for simplification.

输入
Each test case begins with a line containing a single integer n(1≤n≤100), size of the set of training samples D.
Then n lines follow, the ith of which contains three integers xi,1,xi,2,yi (−105≤xi,1,xi,2≤105, yi∈{−1,1}), indicating the ith sample (xi,yi) in D, where xi=(xi,1,xi,2).
输出
题意是给你n个三元组(x1,x2,y),问你是否存在a,b,c使得对所有的三元组满足sgn(ax1+bx2+c)=y; 官方题解:
d= 时,f(x) =sgn(ax1+bx2+c),f(x) = 对应于二维平面上的一条直线,直线一侧的点取值为 ,直线另一侧的取值为 -。
故该问题等价于能否找到一条直线将平面上的两类点分开,等价于判断这两类点分别组成的两个凸包是否相交。
判断凸包是否相交,参考https://www.cnblogs.com/ITUPC/p/5987593.html
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const double eps=1e-;
const int N=;
int sgn(double x)
{
if (fabs(x)<eps)return ;
if (x<)return -;
else return ;
}
struct Point {
ll x,y;
Point() {}
Point(ll _x,ll _y)
{
x=_x; y=_y;
}
Point operator -(const Point &b)
{
return Point(x-b.x,y-b.y);
}
ll operator ^(const Point &b)
{
return x*b.y-y*b.x;
}
ll operator *(const Point &b)
{
return x*b.x+y*b.y;
}
bool operator<(const Point &b)const
{
if(fabs(y-b.y)<eps) return x<b.x;
return y<b.y;
}
};
double Length(Point A)
{
return sqrt(A*A);
}
double Angle(Point A,Point B)
{
return acos((A*B)/Length(A)/Length(B));
}
bool Inter(Point a1,Point a2,Point b1,Point b2)
{
ll c1=(a2-a1)^(b1-a1),c2=(a2-a1)^(b2-a1),c3=(b2-b1)^(a1-b1),c4=(b2-b1)^(a2-b1);
return sgn(c1)*sgn(c2)<&&sgn(c3)*sgn(c4)<;
} int Graham(Point p[],int n,Point q[])
{
int top=;
sort(p,p+n);
if (n==) return ;
q[]=p[];
if (n==) return ;
q[]=p[];
if (n==) return ;
q[]=p[];
for (int i=;i<n;i++)
{
while(top&&((q[top]-q[top-])^(p[i]-q[top-]))<=) top--;
q[++top]=p[i];
}
int len=top;
q[++top]=p[n-];
for (int i=n-;i>=;i--)
{
while (top!=len&&((q[top]-q[top-])^(p[i]-q[top-]))<=) top--;
q[++top]=p[i];
}
return top;
} bool C_S(Point ch1[],int t1,Point ch2[],int t2)
{
if (t1==) return ;
double angle[],x;
int i,j,k;
if (t1==)
{
for(i=;i<t2;i++)
{
k=sgn((ch1[]-ch1[])^(ch2[i]-ch1[]));
if (k== && (ch1[]-ch1[])*(ch2[i]-ch1[])>)
{
if (Length(ch2[i]-ch1[])<Length(ch1[]-ch1[])) break;
}
}
if (i<t2) return ;
if (t2== && Inter(ch1[],ch1[],ch2[],ch2[])) return ;
return ;
}
angle[]=;
for (int i=;i<t1;i++) angle[i-]=Angle(ch1[]-ch1[],ch1[i]-ch1[]);
for (i=;i<t2;i++)
{
j=sgn((ch1[]-ch1[])^(ch2[i]-ch1[]));
if (j< || (j== && (ch1[]-ch1[])*(ch2[i]-ch1[])<)) continue;
j=sgn((ch1[t1-]-ch1[])^(ch2[i]-ch1[]));
if (j> || (j== && (ch1[t1-]-ch1[])*(ch2[i]-ch1[])<)) continue;
x=Angle(ch1[]-ch1[],ch2[i]-ch1[]);
int m=lower_bound(angle,angle+t1-,x)-angle;
if (m==) j=; else j=m-;
k=sgn((ch1[j+]-ch2[i])^(ch1[j+]-ch2[i]));
if (k>=) break;
}
if (i<t2) return ;
return ;
}
Point p1[N],p2[N],ch1[N],ch2[N];
int T,n;
int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int cnt1=,cnt2=;
ll x,y; int z;
for(int i=;i<n;i++)
{
scanf("%lld%lld%d",&x,&y,&z);
if (z==) p1[cnt1++]=Point(x,y);
else p2[cnt2++]=Point(x,y);
}
int t1=Graham(p1,cnt1,ch1);
int t2=Graham(p2,cnt2,ch2);
if (C_S(ch1,t1,ch2,t2)&&C_S(ch2,t2,ch1,t1)) printf("Successful!\n");
else printf("Infinite loop!\n");
}
//fclose(stdin);
//fclose(stdout);
return ;
}
杭电多校第一场-M-Code的更多相关文章
- 2018 Multi-University Training Contest 1 杭电多校第一场
抱着可能杭电的多校1比牛客的多校1更恐怖的想法 看到三道签到题 幸福的都快哭出来了好吗 1001 Maximum Multiple(hdoj 6298) 链接:http://acm.hdu.edu. ...
- 2019杭电多校第一场hdu6581 Vacation
Vacation 题目传送门 update(O(n)) 看了那个O(n)的方法,感觉自己想的那个O(nlogn)的好傻,awsl. 0车最终通过停车线的时候,状态一定是某个车堵住后面的所有车(这个车也 ...
- 2019年杭电多校第一场 1009题String(HDU6586+模拟+单调栈)
题目链接 传送门 题意 给你一个字符串,要你构造一个长为\(k\)的子串使得每个字母出现的次数在\([L_i,R_i](0\leq i\leq26)\)间且字典序最小. 思路 做这种题目就是要保持思路 ...
- 2019年杭电多校第一场 1004题Vacation(HDU6581+数学)
题目链接 传送门 题意 有\(n+1\)辆车要过红绿灯,告诉你车的长度.与红绿灯的起点(题目假设红绿灯始终为绿).车的最大速度,问你第\(0\)辆车(距离最远)车头到达红绿灯起点的时间是多少(每辆车最 ...
- 2019年杭电多校第一场 1002题Operation(HDU6579+线性基)
题目链接 传送门 题意 初始时有\(n\)个数,现在有\(q\)次操作: 查询\([l,r]\)内选择一些数使得异或和最大: 在末尾加入一个数. 题目强制在线. 思路 对于\(i\)我们记录\([1, ...
- [2019杭电多校第一场][hdu6582]Path(最短路&&最小割)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6582 题意:删掉边使得1到n的最短路改变,删掉边的代价为该边的边权.求最小代价. 比赛时一片浆糊,赛后 ...
- [2019杭电多校第一场][hdu6583]Typewriter(后缀自动机&&dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6583 大致题意是说可以花费p在字符串后添加一个任意字符,或者花费q在字符串后添加一个当前字符串的子串. ...
- [2019杭电多校第一场][hdu6579]Operation(线性基)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题目大意是两个操作,1个是求[l,r]区间子序列的最大异或和,另一个是在最后面添加一个数. 如果 ...
- [2019杭电多校第一场][hdu6578]Blank(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6578 计数问题想到dp不过分吧... dp[i][j][k][w]为第1-i位置中4个数最后一次出现的 ...
- 2019杭电多校第一场hdu6579 Operation(线性基)
Operation 题目传送门 解题思路 把右边的数尽量往高位放,构造线性基的时候同时记录其在原序列中的位置,在可以插入的时候如果那个位置上存在的数字的位置比新放入的要小,就把旧的往后挤.用这种发现构 ...
随机推荐
- 嵌套循环结合修改IFS环境变量遍历文件数据中IFS的修改一致性
以下这个脚本使用了两个不同的IFS值来解析数据.第一个IFS值解析出/etc/passwd文件中的单独的行.内部for循环接着将IFS值改为冒号,云溪你从/etc/passwd的行中解析出单独的值. ...
- 2018-10-19-C#-GUID-ToString-
title author date CreateTime categories C# GUID ToString lindexi 2018-10-19 9:4:44 +0800 2018-4-1 10 ...
- 第三章 k8s的node节点配置
一.修改主机名 hostnamectl set-hostname xxx 二.修改hosts文件vim /etc/hosts 三.将写好的hosts文件拷贝到其他节点 scp /etc/hosts r ...
- xargs使用之空格处理
xargs指定分隔符为'\n' (默认用空格分隔) locate xxx | xargs -d '\n' ls -l xargs使用 -0 参数会以字符串的'\0'结尾为分隔符,可以在文本传给xarg ...
- 项目案例之Pipeline流水线发布JAVA项目(三)
项目案例之Pipeline流水线发布JAVA项目(三) 链接:https://pan.baidu.com/s/1NZZbocZuNwtQS0eGkkglXQ 提取码:z7gj 复制这段内容后打开百度网 ...
- 【LeetCode】Stack
[503] Next Greater Element II [Medium] 给一个循环数组,找到离当前元素最近的比它大的元素. Input: [1,2,1] Output: [2,-1,2] Exp ...
- DOM0级事件绑定之js的onclick事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [人物存档]【AI少女】【捏脸数据】朴素风格
点击下载(城通网盘):AISChaF_20191115113752642.png 点击下载(城通网盘):AISChaF_20191111232359711.png
- leetcode-161周赛-5248-统计【优美子数组】
题目描述: 自己的提交:超时: class Solution: def numberOfSubarrays(self, nums, k: int) -> int: dp = [0]* (len( ...
- JavaWeb开发中遇到的错误:org.apache.catalina.core.StandardWrapperValve invoke
org.apache.catalina.core.StandardWrapperValve invoke 今天写代码,竟然接连遇到这个异常好几次.debug几个小时才弄明白,晕. 上网找了些拼凑下做个 ...