集训第四周(高效算法设计)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 ...
随机推荐
- 第二篇(那些JAVA程序BUG中的常见单词)
Cannot instantiate the type xxx 无法实例化类型xxx instantiate 实例化
- JavaScript 入门案例
四. JavaScript 入门案例 在看本节之前,笔者建议您先看 JavaScript 基础篇 https://www.cnblogs.com/IT-LFP/p/10945884.html 1. ...
- iOS 消除Xcode警告
- undeclared selector #pragma clang diagnostic push #pragma clang diagnostic ignored"-Wundeclar ...
- SpringMVC之HttpMessageConverter
http://blog.csdn.net/zmx729618/article/details/53034420 HttpMessageConverter接口: T read(Class<? ex ...
- DP(DAG) UVA 437 The Tower of Babylon
题目传送门 题意:给出一些砖头的长宽高,砖头能叠在另一块上要求它的长宽都小于下面的转头的长宽,问叠起来最高能有多高 分析:设一个砖头的长宽高为x, y, z,那么想当于多了x, z, y 和y, x, ...
- 背包DP HDOJ 5410 CRB and His Birthday
题目传送门 题意:有n个商店,有m金钱,一个商店买x件商品需要x*w[i]的金钱,得到a[i] * x + b[i]件商品(x > 0),问最多能买到多少件商品 01背包+完全背包:首先x == ...
- 1-最全CSS3选择器
一,CSS3 选择器分类 二,选择器语法 1,基本选择器语法 选择器 类型 功能描述 * 通配选择器 选择文档中所以HTML元素 E 元素选择器 选择指定类型的HTML元素 #id ID选择器 ...
- .net 环境下c# 通信
.net环境下通信主要掌握 通信协议(UDP&TCP). 网络抓包工具().:使用方法 点对点通信,IP组播,广播通信 c#中结构体转为字节流方式 c#结构体与c++结构体转换对应关系 开源的 ...
- select 1浅析
今天看到项目代码里有这条语句,不懂select 1 from XXXXXXX里的1是何意,查了一番才知道: 1.select 1 from mytable;与select anycol(目的表集合中的 ...
- JS 中的 JSON
JSON是JavaScript Object Notation的缩写,它是一种数据交换格式. 在JSON出现之前,大家一直用XML来传递数据.因为XML是一种纯文本格式,所以它适合在网络上交换数据.X ...