uvalive 4253(暴力)
来源:互联网 发布:修改屏幕分辨率软件 编辑:程序博客网 时间:2024/05/18 02:59
题意:有n个靶子,是平行于x轴的高度不同的线段,给出n个d(高度)、l(左端点)、r(右端点),然后给出w,让在x轴的[0,w]这个区间内射箭,问是否存在一个位置能射中所有靶子。
题解:先按高度排序,然后在[0,w]范围内二分位置,先得到射箭到第一个靶子的左右端点的斜率,然后到第二个靶子的左右端点的斜率,比较斜率范围是否可以重叠,如果不可以就在x轴上左移或右移(改变l或r),否则就缩小斜率范围,继续和下一个靶子比较。最后所有位置都不可以就输出NO。这里用了atan2(y,x)函数,表示从起点到点(x, y)这条线和x正半轴的角度,其实和atan(y/x)效果一样,只不过可以处理x==0时的特殊情况。
#include <stdio.h>#include <math.h>#include <algorithm>using namespace std;const int N = 5005;struct Point {double l, r, d;}p[N];double w;int n;bool cmp(Point a, Point b) {return a.d < b.d;}int judge(double pos) {double l = atan2(p[0].d, p[0].l - pos);double r = atan2(p[0].d, p[0].r - pos);for (int i = 1; i < n; i++) {double temp1 = atan2(p[i].d, p[i].l - pos);double temp2 = atan2(p[i].d, p[i].r - pos);if (temp2 - l > 1e-8)return 1;//右移if (r - temp1 > 1e-8)return -1;//左移l = min(l, temp1);//缩小范围r = max(r, temp2);}return 0;}int main() {int t;scanf("%d", &t);while (t--) {scanf("%lf%d", &w, &n);for (int i = 0; i < n; i++)scanf("%lf%lf%lf", &p[i].d, &p[i].l, &p[i].r);sort(p, p + n, cmp);double l = 0, r = w;int flag = 0;while (r - l > 1e-8) {double mid = (l + r) / 2;int temp = judge(mid);if (!temp) {printf("YES\n");flag = 1;break;}else if (temp == 1)l = mid;elser = mid;}if (!flag)printf("NO\n");}return 0;}
0 0
- uvalive 4253(暴力)
- UVALive - 4253 Archery 暴力
- UVALive - 2963 Hypertransmission 暴力
- UVALive 6924 暴力
- UVALive - 3695 Distant Galaxy 暴力
- UVALive - 4256 Salesmen DP+暴力
- UVALive 6432 Influence // 暴力dfs
- UVALive 6852Blanket(暴力)
- UVALive 6895Deduction(暴力)
- uvaLive 3401 Colored Cubes 暴力
- UVALive-7279 - Sheldon Numbers【暴力】
- uvalive 7365 Composition 暴力解法
- 【dp/二分+暴力】MAGRID UVALive
- UVALive 5733|Iterated Difference|暴力
- UVALive 6468 Pisano Periods 坑爹暴力
- UVALive 6089 Nine (暴力模拟)
- Vampire Numbers - UVALive 5779 暴力打表
- UVALive - 5908(UVA1517)Tracking RFIDs(暴力)
- Spring Cache注解+Memcached
- 图片防盗链的解决办法
- expandableListview实现侧滑删除
- ASP.NET运行机制原理(一)
- Android缓存!
- uvalive 4253(暴力)
- Spring的事务配置
- 关系代数对于数据库的查询优化的指导意义
- 给编辑框设置新的窗口过程,实现按下Tab键和回车键自动切换到下一个编辑框
- RFID系统组成及其功能
- 2015最新站长seo优化技巧分享
- Oracle 11g数据库及PLSQL_Developer安装配置
- Gridland
- ios界面跳转总结(一)