集训第四周(高效算法设计)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 ...
随机推荐
- Oracle11.2.0.1升级到11.2.0.3
Oracle数据库升级也并非简单的事,这篇博客,博主对Oracle那点事做了较详细的介绍: http://blog.itpub.net/9599/viewspace-473003/ 我还属于Oracl ...
- Elasticsearch的功能、使用场景以及特点
1.Elasticsearch的功能,干什么的 2.Elasticsearch的适用场景,能在什么地方发挥作用 3.Elasticsearch的特点,跟其他类似的东西不同的地方在哪里 1.Elasti ...
- php 文件锁flock解决并发
方案一:使用文件锁排它锁 flock函数用于获取文件的锁,这个锁同时只能被一个线程获取到,其它没有获取到锁的线程要么阻塞,要么获取失败 在获取到锁的时候,先查询,如果查询成功则进行操作,然后释放锁 f ...
- [POI2011]Temperature
Description The Byteotian Institute of Meteorology (BIM) measures the air temperature daily. The mea ...
- _bzoj1010 [HNOI2008]玩具装箱toy【斜率优化dp】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1010 裸的斜率优化,第一次写队首维护的时候犯了个智障错误,队首维护就是维护队首,我怎么会那队 ...
- 字符串处理 BestCoder Round #43 1001 pog loves szh I
题目传送门 /* 字符串处理:是一道水题,但是WA了3次,要注意是没有加'\0'的字符串不要用%s输出,否则在多组测试时输出多余的字符 */ #include <cstdio> #incl ...
- 题解报告:NYOJ #311完全背包(恰好装满)
描述: 直接说题意,完全背包定义有N种物品和一个容量为V的背包,每种物品都有无限件可用.第i种物品的体积是c,价值是w.求解将哪些物品装入背包可使这些物品的体积总和不超过背包容量,且价值总和最大.本题 ...
- canvas 保存状态
1.保存和恢复绘图状态: 在绘制图形时,难免会重复使用某个样式,甚至有时会在不同颜色之间来回切换. 那么为了减少代码冗余,我们可以调用画布中的save()方法,来帮我们 保存一些样式和属性,这样我们就 ...
- 关于java的arrays数组排序示例AJPFX的分享
Java API对Arrays类的说明是:此类包含用来操作数组(比如排序和搜索)的各种方法. 1.对基本数据类型的数组的排序 说明: (1)Arrays类中的sort()使用的是“经过调优的快速排序法 ...
- [BZOJ1088][SCOI2005]扫雷Mine DP
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1088 记录下每一个格子对应左边格子放的雷的情况,然后dp转移就好了. #include&l ...