https://www.hackerrank.com/contests/infinitum-aug14/challenges/jim-beam

学习了线段相交的判断法。首先是叉乘,叉乘的几何意义是有向的平行四边形的面积(除以2就是三角形的面积)。如果ABD和ABC正负相反,说明C和D在AB两侧,同样的,再判断A和B是否在CD两侧即可。当某三角形面积为0时,需要判断是否在线段上。

#include <iostream>
using namespace std; typedef long long LL;
LL cross_product(LL ax, LL ay, LL bx, LL by, LL cx, LL cy) {
return (bx - ax) * (cy - ay) - (by - ay) * (cx - ax);
} bool inside(LL ax, LL ay, LL bx, LL by, LL cx, LL cy) {
return (min(ax, bx) <= cx && cx <= max(ax, bx)
&& min(ay, by) <= cy && cy <= max(ay, by));
} bool intersect(LL ax, LL ay, LL bx, LL by, LL cx, LL cy) {
LL abc = cross_product(ax, ay, bx, by, cx, cy);
LL abd = cross_product(ax, ay, bx, by, 0, 0);
LL cda = cross_product(cx, cy, 0, 0, ax, ay);
LL cdb = cross_product(cx, cy, 0, 0, bx, by);
if (((abc > 0 && abd < 0)
|| (abc < 0 && abd > 0))
&& ((cda > 0 && cdb < 0)
|| (cda < 0 && cdb > 0)))
return true;
if (abc == 0 && inside(ax, ay, bx, by, cx, cy)) return true;
if (abd == 0 && inside(ax, ay, bx, by, 0, 0)) return true;
if (cda == 0 && inside(cx, cy, 0, 0, ax, ay)) return true;
if (cdb == 0 && inside(cx, cy, 0, 0, bx, by)) return true; return false;
} void solve() {
int x1, y1, x2, y2, x, y;
cin >> x1 >> y1 >> x2 >> y2 >> x >> y;
if (intersect(x1, y1, x2, y2, x, y))
cout << "NO" << endl;
else
cout << "YES" << endl;
} int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}

  

*[hackerrank]Jim Beam的更多相关文章

  1. 转自知乎,亲民好酒推荐 分类: fool_tree的笔记本 2014-11-08 17:37 652人阅读 评论(0) 收藏

    这里尽量为大家推荐一些符合大众喜好.业内公认好评."即使你不喜欢,你也会承认它不错"的酒款.而且介绍到的酒款还会有一个共同的特征,就是能让你方便的在网上买到. 大概会分为烈酒,利口 ...

  2. Spring Security LDAP简介

    1.概述 在本快速教程中,我们将学习如何设置Spring Security LDAP. 在我们开始之前,了解一下LDAP是什么? - 它代表轻量级目录访问协议.它是一种开放的,与供应商无关的协议,用于 ...

  3. Beam编程系列之Apache Beam WordCount Examples(MinimalWordCount example、WordCount example、Debugging WordCount example、WindowedWordCount example)(官网的推荐步骤)

    不多说,直接上干货! https://beam.apache.org/get-started/wordcount-example/ 来自官网的: The WordCount examples demo ...

  4. Beam Search(集束搜索/束搜索)

    找遍百度也没有找到关于Beam Search的详细解释,只有一些比较泛泛的讲解,于是有了这篇博文. 首先给出wiki地址:http://en.wikipedia.org/wiki/Beam_searc ...

  5. 日常小测:颜色 && Hackerrank Unique_colors

    题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...

  6. HackerRank "Square Subsequences" !!!

    Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...

  7. HackerRank "Minimum Penalty Path"

    It is about how to choose btw. BFS and DFS. My init thought was to DFS - TLE\MLE. And its editorial ...

  8. HackerRank "TBS Problem" ~ NPC

    It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/Charl ...

  9. 关于Beam Search

    Wiki定义:In computer science, beam search is a heuristic search algorithm that explores a graph by exp ...

随机推荐

  1. linux解压zip、bz、bz2、z、gz、tar(解包)

    zip: 压缩: zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$][-b <工作目录>][-ll][-n <字尾字符串>][-t <日期时间>] ...

  2. C# Winform中DataGridView的DataGridViewCheckBoxColumn使用方法

    下面介绍Winform中DataGridView的DataGridViewCheckBoxColumn使用方法: DataGridViewCheckBoxColumn CheckBox是否选中 在判断 ...

  3. TOMCAT内存大小调整

    Tomcat本身不能直接在计算机上运行,需要依赖于硬件基础之上的操作系统和一个java虚拟机.JAVA程序启动时JVM都会分配一个初始内存和最大内存给这个应用程序.这个初始内存和最大内存在一定程度都会 ...

  4. Silverlight动画的基本知识、关键帧动画

    基础知识 (一)动画:是快速播放一系列图像(其中每个图像与下一个图像略微不同)给人造成的一种幻觉 (二)动画类型:两类    (1)From/To/By动画:在起始值和结束值之间进行动画处理.     ...

  5. android 在标题栏加上按钮

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowF ...

  6. highchars

    var drawChart = function(sourceUrl) { $.ajax({ "type" : "post", "url" ...

  7. Oracle 动态视图4 V$SESSION_WAIT & V$SESSION_EVENT

    一.视图V$SESSION_WAIT显示了session的当前等待事 Column Datatype Description SID NUMBER Session identifier SEQ# NU ...

  8. 【html5】这些新类型 能提高生产力

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  9. 游刃于MVC、WCF中的Autofac

    为了程序的健壮性.扩展性.可维护性,依赖抽象而不是具体实现类等等,于是我选择了Autofac依赖注入容器 就是这个工厂来降低耦合.之前买东西是自己去超市,现在呢 我需要什么东西,他们给送过来直接拿到了 ...

  10. java 简单搜索算法

    --无序查找 public static int Search(int[] a,int key){ for(int i=0;i<a.length;i++){ if(key==a[i]){ ret ...