LA 4253 箭术(二分枚举)
https://vjudge.net/problem/UVALive-4253
题意:
有n个平行于x轴的线段,每条线段代表一个靶子。判断是否可以站在x轴上[0,W]区间内的某个位置射箭。
思路:
二分枚举坐标点,这道题需要用到atan2函数,它返回一个角度值,对于每个靶子,利用atan2函数确定能射中靶子的区间,如果靶子之间区间没有重合部分,说明该坐标点不能射中所有靶子。在下面的代码中,如果返回1,说明需要向右移,如果返回-1,说明需要向左移。
这道题目还需要注意一点的就是精度问题。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std; const int maxn = + ; double W;
int n; struct node
{
double D, L, R;
}a[maxn]; bool cmp(node a, node b)
{
return a.D < b.D;
} int judge(double pos)
{
double R = atan2(a[].D, a[].R - pos);
double L = atan2(a[].D, a[].L - pos);
for (int i = ; i < n; i++)
{
double r = atan2(a[i].D, a[i].R - pos);
double l = atan2(a[i].D, a[i].L - pos); if (l - R < -0.000001) return -;
if (r - L > 0.000001) return ; L = min(L, l);
R = max(R, r);
}
return ;
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int T;
cin >> T;
while (T--)
{
cin >> W >> n;
for (int i = ; i < n; i++)
cin >> a[i].D >> a[i].L >> a[i].R;
sort(a, a + n, cmp);
double l = , r = W;
int ok = ;
while (r - l>0.0000001)
{
double mid = (r + l) / ;
int ans = judge(mid);
if (ans == )
{
cout << "YES" << endl;
ok = ;
break;
}
else if (ans == ) l = mid;
else r = mid;
}
if(!ok) cout << "NO" << endl;
}
}
LA 4253 箭术(二分枚举)的更多相关文章
- LA 4253 Archery 二分
题意: x轴上方有若干条平行于x轴的线段 在x轴的区间\([0, \, W]\)内找一点发射一条射线,使其穿过所有线段. 问是否存在这样的点. 分析: 我们二分射线端点的坐标,将线段按纵坐标从小到大排 ...
- Brownie Slicing(二分枚举答案)
描述 Bessie has baked a rectangular brownie that can be thought of as an RxC grid (1 <= R <= 500 ...
- FZU-2216 The Longest Straight (二分枚举)
题目大意:给n个0~m之间的数,如果是0,那么0可以变为任意的一个1~m之间的一个数.从中选出若干个数,使构成一个连续的序列.问能构成的最长序列的长度为多少? 题目分析:枚举连续序列的起点,二分枚举二 ...
- uva 12587 二分枚举
思路:维护一个森林,二分枚举最小的最大值. #include<set> #include<map> #include<cmath> #include<queu ...
- SDIBT 3237 Boring Counting( 划分树+二分枚举 )
http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3237 Problem H:Boring Counting Time Limit: 3 Sec ...
- POJ 3273 Monthly Expense 二分枚举
题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...
- POJ 2112 Optimal Milking(Floyd+多重匹配+二分枚举)
题意:有K台挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远 输入数据: 第一行三个数 K, C, M 接下来是 ...
- hdu 5248 序列变换(二分枚举)
Problem Description 给定序列A={A1,A2,...,An}, 要求改变序列A中的某些元素,形成一个严格单调的序列B(严格单调的定义为:Bi<Bi+,≤i<N). 我们 ...
- UVa LA 4254 - Processor 二分,贪心 难度: 1
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
随机推荐
- 暴力破解工具hydra
Hydra是一个并行登录的裂解装置,它支持众多的协议来攻击.新的模块很容易的添加,旁边,它是灵活的,而且速度非常快. 首先安装的是hydra的支持库包软件. yum -y install openss ...
- 110道python题+理解(不断更新)
此篇题目在网上已经广为流传,但好多都不做解释,所以我想着自己一道一道的做一遍,并将相关涉及的做个补充,个人知识毕竟片面,有不足的地方还请大家多多指正 一.请用一行代码实现1-100之和 >> ...
- SQL中 decode()函数简介(转载)
今天看别人的SQL时看这里面还有decode()函数,以前从来没接触到,上网查了一下,还挺好用的一个函数,写下来希望对朋友们有帮助哈! decode()函数简介: 主要作用:将查询结果翻译成其他值(即 ...
- oracle通过profile限制用户的恶意登录和使用期限
用户profile口令管理 1,可以把profile想象成一个数据对象(文件,规则) 案例: 允许某用户,最多尝试登录3次,如3次未登录成功,则锁定该用户,锁定后两天不能登录系统 设置语法(syste ...
- c# 读取confgi文件
引用命名空间using System.Configuration; Winform—C#读写config配置文件
- Finding files on a *nix/Linux and sorting by size
1. Finding files on *NIX and sort by size find ~ -iregex ".*/*.sh" -type f -print0 | xargs ...
- D. Little Artem and Dance---cf669D(模拟)
题目链接:http://codeforces.com/problemset/problem/669/D 给你n个数,一开始是1 2 3 4 5 6 ... n 这样的 现在有两个操作,第一个操作是所有 ...
- django的cache
使用文件缓存 #settings.py CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.filebased.F ...
- 【Maven学习】Nexus私服代理其他第三方的Maven仓库
一.背景 [Maven学习]Nexus OSS私服仓库的安装和配置 http://blog.csdn.net/ouyang_peng/article/details/78793038 [Maven学习 ...
- PAT 1079 Total Sales of Supply Chain[比较]
1079 Total Sales of Supply Chain(25 分) A supply chain is a network of retailers(零售商), distributors(经 ...