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 ...
随机推荐
- iOS tableview上放textfield
用UITableViewController就可以了,处理键盘弹出和消失的代码已经封装在UITableViewController里了.
- linux awk时间计算脚本
在linux如果计划时间是个麻烦事, 用awk脚本如下 BEGIN {FS=":";OFS=":"} {total_seconds=total_seconds+ ...
- interface Impl
public interface ActionBarOperations { void initSthOne(); void initSthTwo(); } public class ActionBa ...
- Scikit Learn安装教程
Windows下安装scikit-learn 准备工作 Python (>= 2.6 or >= 3.3), Numpy (>= 1.6.1) Scipy (>= 0.9), ...
- G1垃圾收集器入门-原创译文
G1垃圾收集器入门-原创译文 原文地址 Getting Started with the G1 Garbage Collector 概览 目的 本文介绍了如何使用G1垃圾收集器以及如何与Hotspot ...
- 170620、springboot编程之页面版Hello World
书接上回,把Hello World 在页面上显示! 1.在pom文件中加入web支持 <dependency> <groupId>org.springframework.boo ...
- Convolution and polynomial multiplication
https://www.mathworks.com/help/matlab/ref/conv.html?s_tid=gn_loc_drop conv Convolution and polynomia ...
- django-mvvm(django的FormObject)
MVVM简介 MVVM模式是Model-View-ViewMode模式的简称.由视图(View).视图模型(ViewModel).模型(Model)三部分组成,结构如下图.通过这三部分实现UI逻辑.呈 ...
- java-mybaits-00801-逆向工程
1.1 什么是逆向工程 参看地址:http://www.mybatis.org/generator/index.html 使用官方网站的mapper自动生成工具mybatis-generato ...
- sql小知识
1:查询某一段落内的几条数据,按时间降序. LIMIT 5,10; //检索记录行6-15 2:创建视图, 查询出某些类别的数据,保存在视图中. || 的优先级高于and ) ); 3: 查询出 ...