ACM学习历程——NOJ1113 Game I(贪心 || 线段树)
Description
尼克发明了这样一个游戏:在一个坐标轴上,有一些圆,这些圆的圆心都在x轴上,现在给定一个x轴上的点,保证该点没有在这些圆内(以及圆上),尼克可以以这个点为圆心做任意大小的圆,他想知道自己做多可以与多少个给定的圆相交(相切也算,包含不算)。
Input
输入有多组数据 输入到文件尾
每一组数据有一个整数n(1<=n<=100000),表示总共有n个圆。
接下是n行,每行两个整数xi,ri表示该圆的圆心坐标和半径。
接下来一行为一个整数x,表示尼克选取点的位置。
x xi的范围[-10^9,10^9] ri的范围[1,10^9]
总共有最多10组数据。
Output
每组数据输出一行,表示尼克最多可以覆盖多少个圆。
Sample Input
2
1 2
2 1
4
Sample Output
2
这个题目条件转换一下就是满足|r-d| <= R <= r+d的R就能与r半径的圆相交,其中d是两圆圆心的距离。
这样就变成了区间增值,然后查询区间中的最大值。
首先想到的是线段树,复杂度是O(2n*log(2n))。不过由于半径范围的值是离散的,所以采用map进行映射,使其连续。不过AC用时500ms左右。
然后发现其实直接处理后直接贪心就行。将所有区间的左右端点排序,排序时需要保存标记,用于记录这个端点是某个区间的左端点还是右端点。然后就是扫一遍,对于是左端点的自然值加一,对于右端点的自然值减一,然后贪心过程中的最大值。AC用时85ms左右。
贪心代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#define LL long long using namespace std; struct node
{
LL index;
bool isleft;
}ind[]; int n, ans;
LL x[], r[], xx; bool cmp(node a, node b)
{
return a.index < b.index;
} LL Abs(LL aa)
{
if (aa < )
return -aa;
else
return aa;
} void Init()
{
LL d, Left, Right;
for (int i = ; i < n; ++i)
{
d = Abs(x[i]-xx);
Left = Abs(r[i]-d);
Right = r[i]+d;
ind[i<<].index = Left;
ind[i<<].isleft = true;
ind[i<<|].index = Right;
ind[i<<|].isleft = false;
}
sort(ind, ind+*n, cmp);
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d", &n) != EOF)
{
for (int i = ; i < n; ++i)
{
scanf("%lld%lld", &x[i], &r[i]);
}
scanf("%lld", &xx);
Init();
int len = *n;
int now = ;
ans = ;
for (int i = ; i < len; ++i)
{
if (ind[i].isleft)
now++;
else
now--;
ans = max(ans, now);
}
printf("%d\n", ans);
}
return ;
}
线段树代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#define LL long long using namespace std; //线段树
//区间每点增值,求区间最值
const int maxn = ;
struct node
{
int lt, rt;
int val, add;
}tree[*maxn]; //向下更新
void PushDown(int id)
{
if (tree[id].add != )
{
tree[id<<].add += tree[id].add;
tree[id<<].val += tree[id].add;
tree[id<<|].add += tree[id].add;
tree[id<<|].val += tree[id].add;
tree[id].add = ;
}
} //向上更新
void PushUp(int id)
{
tree[id].val = max(tree[id<<].val, tree[id<<|].val);
} //建立线段树
void Build(int lt, int rt, int id)
{
tree[id].lt = lt;
tree[id].rt = rt;
tree[id].val = ;//每段的初值,根据题目要求
tree[id].add = ;
if (lt == rt)
{
//tree[id].add = ??;
return;
}
int mid = (lt + rt) >> ;
Build(lt, mid, id<<);
Build(mid+, rt, id<<|);
//PushUp(id);
} //增加区间内每个点固定的值
void Add(int lt, int rt, int id, int pls)
{
if (lt <= tree[id].lt && rt >= tree[id].rt)
{
tree[id].add += pls;
tree[id].val += pls;
return;
}
PushDown(id);
int mid = (tree[id].lt + tree[id].rt) >> ;
if (lt <= mid)
Add(lt, rt, id<<, pls);
if (rt > mid)
Add(lt, rt, id<<|, pls);
PushUp(id);
} //查询某段区间内的zuizhi
int Query(int lt, int rt, int id)
{
if (lt <= tree[id].lt && rt >= tree[id].rt)
return tree[id].val;
PushDown(id);
int mid = (tree[id].lt + tree[id].rt) >> ;
if (rt <= mid)
return Query(lt, rt, id<<);
if (lt > mid)
return Query(lt, rt, id<<|);
return max(Query(lt, rt, id<<), Query(lt, rt, id<<|));
} int n, cnt;
LL x[], r[], ind[], xx;
LL Left[], Right[];
map <LL, int> id; bool cmp(LL a, LL b)
{
return a < b;
} LL Abs(LL aa)
{
if (aa < )
return -aa;
else
return aa;
} void Init()
{
id.clear();
LL d;
for (int i = ; i < n; ++i)
{
d = Abs(x[i]-xx);
Left[i] = Abs(r[i]-d);
Right[i] = r[i]+d;
ind[i<<] = Left[i];
ind[i<<|] = Right[i];
}
sort(ind, ind+*n, cmp);
int len = *n;
cnt = ;
for (int i = ; i < len; ++i)
{
if (i == )
{
id[ind[]] = ;
continue;
}
if (ind[i] != ind[i-])
{
id[ind[i]] = ++cnt;
}
}
Build(, cnt, );
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d", &n) != EOF)
{
for (int i = ; i < n; ++i)
{
scanf("%lld%lld", &x[i], &r[i]);
}
scanf("%lld", &xx);
Init();
for (int i = ; i < n; ++i)
{
Add(id[Left[i]], id[Right[i]], , );
}
printf("%d\n", Query(, cnt, ));
}
return ;
}
ACM学习历程——NOJ1113 Game I(贪心 || 线段树)的更多相关文章
- ACM学习历程—POJ1151 Atlantis(扫描线 && 线段树)
Description There are several ancient Greek texts that contain descriptions of the fabled island Atl ...
- ACM学习历程——HDU3333 Turing Tree(线段树 && 离线操作)
Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...
- ACM学习历程—HihoCoder1309任务分配(排序 && 贪心)
http://hihocoder.com/problemset/problem/1309 题目大意是给定n个任务的起始时间,求问最少需要多少台机器. 有一个贪心的策略就是,如果说对于一个任务结束,必然 ...
- ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 && 动态规划 && 数论)
http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每 ...
- ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)
Description We all use cell phone today. And we must be familiar with the intelligent English input ...
- ACM学习历程—HDU2222 Keywords Search(字典树)
Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...
- BZOJ4391 High Card Low Card [Usaco2015 dec](贪心+线段树/set库
正解:贪心+线段树/set库 解题报告: 算辣直接甩链接qwq 恩这题就贪心?从前往后从后往前各推一次然后找一遍哪个地方最大就欧克了,正确性很容易证明 (这里有个,很妙的想法,就是,从后往前推从前往后 ...
- 【题解】P1712 [NOI2016]区间(贪心+线段树)
[题解]P1712 [NOI2016]区间(贪心+线段树) 一个observe是,对于一个合法的方案,将其线段长度按照从大到小排序后,他极差的来源是第一个和最后一个.或者说,读入的线段按照长度分类后, ...
- ACM学习历程—HDU5696 区间的价值(分治 && RMQ && 线段树 && 动态规划)
http://acm.hdu.edu.cn/showproblem.php?pid=5696 这是这次百度之星初赛2B的第一题,但是由于正好打省赛,于是便错过了.加上2A的时候差了一题,当时有思路,但 ...
随机推荐
- java.lang.UnsupportedClassVersionError: Unsupported major.minor version 49.0的错误 [转]
一:要解决的问题 我们在尝鲜 JDK1.5 的时候,相信不少人遇到过 Unsupported major.minor version 49.0 错误,当时定会茫然不知所措.因为刚开始那会儿,网上与此相 ...
- Jest — ElasticSearch Java 客户端
1. 介绍 任何使用过Elasticsearch的人都知道,使用基于rest的搜索API构建查询可能是单调乏味且容易出错的. 在本教程中,我们将研究Jest,一个用于Elasticsearch的HTT ...
- Ubuntu下安装和编译ffmpeg
参考:http://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu 1.安装依赖包 sudo apt-get update sudo apt-get -y ...
- Ubuntu Server 安装 NodeJS
准备命令: $ sudo apt-get install python $ sudo apt-get install build-essential $ sudo apt-get install gc ...
- jQuery Validate(一)
jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单. 但是在学习的过程中,我也遇到了疑惑,网上的很多例子貌似都是依赖jquery.metadata.js这个库, ...
- c++引用返回值
引用作为函数的返回值时,函数的返回值能够理解为函数返回了一个变量(事实上,函数返回引用时,它返回的是一个指向返回值的隐式指针),因此,值为引用的函数能够用作赋值运算符的左操作数.另外,用引用返回一个函 ...
- pooler 连接池中报错" error_no_members"
连接池驱动,pooler 在使用中会报错" error_no_members" 分析:可能有以下3个地方对其有影响: 1)member 可能没回收: 2)wait_for_p ...
- IdentityServer4 + SignalR Core +RabbitMQ 构建web即时通讯(一)
IdentityServer4 + SignalR Core +RabbitMQ 构建web即时通讯 前言 .net core 2.1已经正式发布了,signalr core1.0随之发布,是时候写个 ...
- python 基础 2.1 if 流程控制(一)
一.if else 1.if 语句 if expression: //注意if后有冒号,必须有 statement(s) //相对于if缩进4个空格 注:pytho ...
- DNS--域名系统 随笔
定义:是一种用于TCP/IP应用程序的分布式数据库.(分布式数据库:指利用高速计算机网络将物理上分散的多个数据存储单元连接起来组成一个逻辑上统一的数据库.分布式数据库的基本思想是将原来集中式数据库中的 ...