清华学堂 LightHouse
灯塔(LightHouse)
Description
As shown in the following figure, If another lighthouse is in gray area, they can beacon each other.
For example, in following figure, (B, R) is a pair of lighthouse which can beacon each other, while (B, G), (R, G) are NOT.
Input
1st line: N
2nd ~ (N + 1)th line: each line is X Y, means a lighthouse is on the point (X, Y).
Output
How many pairs of lighthourses can beacon each other
( For every lighthouses, X coordinates won't be the same , Y coordinates won't be the same )
Example
Input
3
2 2
4 3
5 1
Output
1
Restrictions
For 90% test cases: 1 <= n <= 3 * 105
For 95% test cases: 1 <= n <= 106
For all test cases: 1 <= n <= 4 * 106
For every lighthouses, X coordinates won't be the same , Y coordinates won't be the same.
1 <= x, y <= 10^8
Time: 2 sec
Memory: 256 MB
Hints
The range of int is usually [-231, 231 - 1], it may be too small.
视频里有讲解我尽然没看到,醉了。
就是求一下逆序对就行。
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define LL long long
const int max_size = * 1e6;
LL y_val[max_size];
LL tmp_arry[max_size];
///加速代码,why 百度
const int SZ = <<;
struct fastio{
char inbuf[SZ];
char outbuf[SZ];
fastio(){
setvbuf(stdin,inbuf,_IOFBF,SZ);
setvbuf(stdout,outbuf,_IOFBF,SZ);
}
}io;
struct Point
{
LL x, y;
} p[max_size]; int cmp(const void *a, const void *b)
{
struct Point *c = (Point *)a;
struct Point *d = (Point *)b;
if(c->x != d->x)
return c->x - d->x;
else
return d->y - c->y;
} LL Merge(LL *arr, LL beg, LL mid, LL end, LL *tmp_arr)
{
memcpy(tmp_arr+beg, arr+beg, sizeof(LL)*(end - beg + ));
LL i = beg;
LL j = mid+;
LL k = beg;
LL inversion = ;
while(i <= mid && j <= end)
{
if(tmp_arr[i] <= tmp_arr[j])
arr[k++] = tmp_arr[i++];
else
{
arr[k++] = tmp_arr[j++];
inversion += mid - i + ;
}
} while(i <= mid)
arr[k++] = tmp_arr[i++];
while(j <= end)
arr[k++] = tmp_arr[j++];
return inversion;
} LL MergeInversion(LL *arr, LL beg, LL end, LL *tmp_arr)
{
LL inversions = ;
if(beg < end)
{
LL mid = (beg + end) >> ;
inversions += MergeInversion(arr, beg, mid, tmp_arr);
inversions += MergeInversion(arr, mid+, end, tmp_arr);
inversions += Merge(arr, beg, mid, end, tmp_arr);
}
return inversions;
} int main()
{
LL n;
cin >> n;
for(int i = ; i < n; i++)
{
cin >> p[i].x >> p[i].y;
} qsort(p, n, sizeof(p[]), cmp);
for(int i = ; i < n; i++)
{
y_val[i] = p[i].y;
} memcpy(tmp_arry, y_val, sizeof(LL)*n);
cout << n*(n-) / - MergeInversion(y_val, , n-, tmp_arry) << endl;
return ;
}
然后再对代码进行优化吧,我的这段过不了最后一个点。因为多做了排序操作,没必要。
清华学堂 LightHouse的更多相关文章
- 清华学堂 列车调度(Train)
列车调度(Train) Description Figure 1 shows the structure of a station for train dispatching. Figure 1 In ...
- 清华学堂 Range
Descriptioin Let S be a set of n integral points on the x-axis. For each given interval [a, b], you ...
- 记2014“蓝桥杯全国软件大赛"决赛北京之行
5月29,30日 最终到了这一天.晚上有数据结构课,10点多的火车,我们就没有去上课,下午在宿舍里收拾东西,晚上8点左右从南校出发,9点半多到达火车站和老师学长学姐们会和. 第一次去北京,第一次买的卧 ...
- WEB入门三 CSS样式表基础
学习内容 Ø CSS的基本语法 Ø CSS选择器 Ø 常见的CSS样式 Ø 网页中3种使用CSS的方式 能力目标 Ø 理解CSS的 ...
- 【推荐】适合本科生的网络公开课(MOOC为主),不断更新……
题记:身在海大(湛江),是幸运还是不幸,每一个人有自己的定义.人生不能再来一次,唯有把握当下.提高自己,才可能在不能拼爹的年代靠自身实力前行.或许,我们做不了富二代.但我们每一个人.都有机会成为富二代 ...
- 教育O2O在学校落地,学堂在线瞄准混合式教学
(大讲台—国内首个it在线教育混合式自适应学习平台.) 进入2015年,互联网教育圈最火的词非“教育O2O”莫属.不断刷新的融资金额和速度,不断曝光的正面和负面新闻,都让教育O2O公司赚足了眼球.然并 ...
- 新上市Lighthouse专用芯片TS3633规格介绍
背景介绍 Valve 有远大的愿景.它决心要把 SteamVR 追踪系统推向世界,从虚拟现实里的空间定位,到机器人领域,Valve 想为各种环境下的跟踪应用提供支持. 上个月,Valve 方面宣布会把 ...
- HTML5学堂,感谢您一年的陪伴(上)
在HTML学堂将满一周岁之际,感谢再过去的一年里支持和关注它的每一个小伙伴.有了你们的支持,HTML5学堂才能更好的走下去.我们将会把这一年的积累重新体现在HTML5学堂的官网上.HTML5学堂将会全 ...
- 【Java】深深跪了,OJ题目Java与C运行效率对比(附带清华北大OJ内存计算的对比)
看了园友的评论之后,我也好奇清橙OJ是怎么计算内存占用的.重新测试的情况附在原文后边. -------------------------------------- 这是切割线 ----------- ...
随机推荐
- centos 7.0 nginx 1.7.9成功安装过程
centos 7.0根目录 的目录构成 [root@localhost /]# lsbin dev home lib64 mnt proc run srv tmp varboot etc lib me ...
- getCanonicalName和getSimpleName getName的区别与应用
接口: package com.test; public interface Fruit { } 一个实现类: package com.test; public class Apple impleme ...
- 学习MySQL之数据库操作(一)
所有代码,均为自学时用到的测试与注释,知识细节或知识点不会面面俱到,亦不会有任何讲解,只做为自己学习复习用. ##数据库操作 ##创建数据库 myTest ,并将数据库字符集设为GBK CREATE ...
- 惊艳!9个不可思议的 HTML5 Canvas 应用试验
HTML5 <canvas> 元素给网页中的视觉展示带来了革命性的变化.Canvas 能够实现各种让人惊叹的视觉效果和高效的动画,在这以前是需要 Flash 支持或者 JavaScript ...
- MongoDB系列一:CentOS7.2下安装mongoDB3.2.8
最近在又在倒腾MongoDB,把安装配置的相关命令贴出来 1.下载 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70- ...
- 2016年11月27日--面向对象:多态、类库、委托、is和as运算符、泛型集合
1.虚方法 virtual 重写 override 父类中的方法,在子类中并不适用,那么子类需要自主更改继承的方法或者是属性,那父类中加了virtual关键字的方法才可以被子类重写,子类重写父类的方法 ...
- Flask 吐血400错误
的确地址或者method不匹配,这个容易解决 在后端中取到了form中不存在的元素! 这个很麻烦,特别form里面的信息比较多时!这个需要一一排查.另外取元素时最好用 request.form.get ...
- Python pydoc.py
1. 查看帮助,我们可以在python命令行交互环境下用 help函数,比如: 查看 math 模块: >>> help('math')Help on built-in module ...
- jstl param url redirect import
import标签 import标签用来导入其他页面 属性: * url :引入页面的路径 * context :工程名 * var :将引入的页面保存到一个变量中 * scope :保存到一个作用域中 ...
- 开源项目在真机调试(Coding iOS 客户端为例)
一.前言 iOS 13学习系列:如何在github下载开源项目到本地(Coding iOS 客户端为例)已经把 Coding iOS 客户端源码下载到本地. 但项目进行真机调试遇到很多问题. 二.问题 ...