集训第四周(高效算法设计)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 ...
随机推荐
- EL表达式(详解)
EL表达式 1.EL基本内容 1)语法结构 ${expression} 2)[]与.运算符 EL 提供.和[]两种运算符来存取数据. 当要存取的属性名称中包含一些特殊 ...
- php实现rpc简单的方法
rpc是啥这不多解释,php扩展实现rpc yar是鸟哥的写的扩展,实现简单的rpc.比较很好理解 windows安装yar http://pecl.php.net/package/yar/2.0.4 ...
- 【BZOJ3514】Codechef MARCH14 GERALD07加强版(LCT_主席树)
题目: BZOJ3514 分析: 看到这题真的是一脸懵逼无从下手,只好膜题解.看到「森林的联通块数 = 点数 - 边数」这一句话就立刻什么都会了 QAQ . 这题最重要的就是意识到上面那个式子(正确性 ...
- 51nod 1098 最小方差
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; ty ...
- 使用Navicat迁移MySQL数据至Oracle时大小写原因报“表或视图不存在”问题处理
使用Navicat提供的数据传输工具将JEECMSv9的MySQL的数据迁移至Oracle数据库,数据迁移成功表都存在,但是在程序启动时提示表或视图不存在. Caused by: java.sql.S ...
- AFNetworking2.5使用-转
来自:http://blog.csdn.net/daiyelang/article/details/38434023 官网下载2.5版本:http://afnetworking.com/ 此文章是基于 ...
- 443 String Compression 压缩字符串
给定一组字符,使用原地算法将其压缩.压缩后的长度必须始终小于或等于原数组长度.数组的每个元素应该是长度为1 的字符(不是 int 整数类型).在完成原地修改输入数组后,返回数组的新长度.进阶:你能否仅 ...
- Java 线程 —— Wait (等待)和 Notify(唤醒)
Wait (等待)和 Notify(唤醒) 这里讲了一个Wait (等待)和 Notfity(唤醒),下面这个实例(工厂,商店,消费者) 额,然后,你就知道了,需要写三个类:工厂类,Shop类,消费者 ...
- IO流的原理和概念
在程序中如何读写文件?不同的编程语言有不同的方式,而 JAVA 则提出了“流”的概念,通过“流”来读写文件 什么是流: 流(Stream)是指一连串的数据(字符或字节),是以先进先出的方式发送信息的通 ...
- Python学习 Day 12 调试 断言 logging pdb pdb.set_trace
调试 第一种方法简单直接粗暴有效,就是用print把可能有问题的变量打印出来看看: >>> def foo(s): n= int(s) print '>>> n = ...