集训第四周(高效算法设计)E题 (区间覆盖问题)
UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419
只能说这道题和D题是一模一样的,不过要进行转化,这道题有一道坑,方法一需要使用scanf()输入,还需判断输入的正确性,即scanf()==3。。。。。
方法一:
#include"iostream"
#include"cstdio"
#include"algorithm"
#include"cmath"
using namespace std; const int maxn=100000+10;
double l,w; struct node
{
double x,y;
}a[maxn],b[maxn]; bool cmp(node a1,node a2)
{
return a1.x<a2.x;
} bool cm(node a1,node a2)
{
return a1.y>a2.y;
} int Init(int num)
{
double wei,r;
int c=0;
double v;
for(int i=0;i<num;i++)
{
cin>>wei>>r;
if(2*r<w) continue;
v=sqrt((r*r)-(w*w)/4);
a[c].x=wei-v;
a[c].y=wei+v;
//cout<<a[c].x<<" "<<a[c].y<<endl<<endl;
c++;
}
return c;
} int main()
{
int f,n;
while(scanf("%d%lf%lf",&n,&l,&w)==3)
{
f=Init(n);
double M=l; sort(a,a+f,cmp); if(a[0].x>0||f==0) cout<<-1<<endl;
else
{
sort(a,a+f,cm);
int ff=0,flag=0,sum=0,j=0,i;
double begi,right=-100,big;
begi=0;
while(begi<M)
{
for(int i=0;i<f;i++)
{
if(a[i].x>begi) continue;
if(a[i].y<=begi) continue;
begi=a[i].y;sum++;flag=1;break;
}
if(flag) flag=0;
else
{
ff=1;
break;
}
}
if(ff) cout<<-1<<endl;
else {
cout<<sum<<endl;
}
}
} return 0;
} 方法二:
#include"iostream"
#include"cstdio"
#include"algorithm"
#include"cmath"
using namespace std; const int maxn=100000+10;
double l,w; struct node
{
double x,y;
}a[maxn],b[maxn]; bool cmp(node a1,node a2)
{
return a1.x<a2.x;
} bool cm(node a1,node a2)
{
return a1.y>a2.y;
} int Init(int num)
{
double wei,r;
int c=0;
double v;
for(int i=0;i<num;i++)
{
cin>>wei>>r;
if(2*r<w) continue;
v=sqrt((r*r)-(w*w)/4);
a[c].x=wei-v;
a[c].y=wei+v;
//cout<<a[c].x<<" "<<a[c].y<<endl<<endl;
c++;
}
return c;
} int main()
{
int f,n;
while(cin>>n>>l>>w)
{
f=Init(n);
double M,x,a1,b1;
M=l; sort(a,a+f,cmp); if(a[0].x>0||f==0) cout<<-1<<endl; else
{
sort(a,a+f,cmp);
int cnt=0;
double left=0, right=0;
bool flag=false; if(a[0].x <= 0 ){
int i=0; while(i < f){ int j=i;
while(j<f && left>=a[j].x){
if(a[j].y > right)
right=a[j].y;
++j;
}
if(j==i) break; ++cnt;
left=right;
i=j; if(left>=l){
flag=true;
break;
}
}
}
if(flag) printf("%d\n", cnt);
else printf("-1\n");
}
}
return 0;
}
集训第四周(高效算法设计)E题 (区间覆盖问题)的更多相关文章
- 高效算法——D 贪心,区间覆盖问题
Given several segments of line (int the X axis) with coordinates [Li , Ri ]. You are to choose the m ...
- 集训第四周(高效算法设计)A题 Ultra-QuickSort
原题poj 2299:http://poj.org/problem?id=2299 题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,归 ...
- 集训第四周(高效算法设计)P题 (构造题)
Description There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<te ...
- 集训第四周(高效算法设计)O题 (构造题)
A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these intege ...
- 集训第四周(高效算法设计)N题 (二分查找优化题)
原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...
- 集训第四周(高效算法设计)M题 (扫描法)
原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...
- 集训第四周(高效算法设计)I题 (贪心)
Description Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshe ...
- 集训第四周(高效算法设计)D题 (区间覆盖问题)
原题 UVA10020 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19688 经典的贪心问题,区间上贪心当然是右区间越 ...
- 集训第四周(高效算法设计)L题 (背包贪心)
Description John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...
随机推荐
- C++this详解
以前对this指针误解挺多的,在这里单独写一篇进行总结,有不对之处,欢迎指正批评! 一.问题 1.一个类中的不同对象在调用自己的成员函数时,其实它们调用的是同一段函数代码,那么成员函数如何知道要访问哪 ...
- ROS学习笔记十一:创建URDF 文件并在RVIZ中查看模型
Unified Robot Description Format,简称为URDF(标准化机器人描述格式),是一种用于描述机器人及其部分结构.关节.自由度等的XML格式文件. 一.创建第一个URDF文件 ...
- 洛谷 P2312 解方程
题目 首先,可以确定的是这题的做法就是暴力枚举x,然后去计算方程左边与右边是否相等. 但是noip的D2T3怎么会真的这么简单呢?卡常卡的真是熟练 你需要一些优化方法. 首先可以用秦九韶公式优化一下方 ...
- hdu 1430 魔板 康托展开 + 很好的映射
http://acm.hdu.edu.cn/showproblem.php?pid=1430 如果从start ---> end,每一次都bfs进行,那么就肯定会超时. 考虑到先把start映射 ...
- Suricata是什么?
不多说,直接上干货! 见Suricata的官网 https://suricata.readthedocs.io/en/latest/what-is-suricata.html snort.suirca ...
- java之java.sql.SQLException: ResultSet is from UPDATE. No Data.
问题解释:java调用存储过程的时候,查询结果不能通过ResultSet来查询,需要通过CallableStatement来查询, 比如: ResultSet rs = callableStateme ...
- Spring && 实验IOC
一.Spring作用 1.生态体系庞大,全能型选手![springmvc是其一个子模块,jdbcTemplate能直接操作数据库!] 2.将其他组件粘合在一起 3.IOC容器和AOP[As ...
- Android开发中使用代码删除数据库
更多信息参考:Android开发中使用代码删除数据库 在Android开发中,如果用到数据库,就会有一个很麻烦的问题,就是有时候需要删除数据库很麻烦,要打开Android Device Monitor ...
- opencv4android移植到系统app
最近在尝试使用opencv4android实现投影仪的自动对焦功能,在AndroidStudio后需要将功能移到系统工程编译成系统app,仅以此文记录下移植过程中遇到的问题. 首先去opencv官网下 ...
- Objective - c Foundation 框架详解2
Objective - c Foundation 框架详解2 Collection Agency Cocoa provides a number of collection classes such ...