集训第四周(高效算法设计)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之简单的四则运算
简单的四则运算 请你编写程序实现能处理两个数的+.-.*./.%的表达式程序.数据的输入/输出全部使用标准输入/输出,输入数据的第一行为你需要计算表达式的个数,从第2行开始,每一行为你计算的一个表达式 ...
- ACM_不知所措的统计员
Problem Description: GDUFE-GAME完美结束,按照惯例,会有一篇报道,描述活动期间的盛况,因此相关人员找到负责统计的ASDF,但是ASDF只知道第i个人在S_i时进场,在E_ ...
- Thinkpad x230设置启动顺序
设置可以从CD或者USB启动1.F1进入BIOS,Security → Secure Boot ,设置为:Disabled2.Startup → UEFI/Legacy Boot ,设置为:Both( ...
- SolrCloud索引富文本数据
solrconfig配置文件: schema配置文件: 执行目录: /opt/solr-5.5.4/server/scripts/cloud-scripts -- 下载配置文件 ./zkcli.sh ...
- AngularJS开发最常犯的10个错误
简介 AngularJS是目前最为活跃的Javascript框架之一,AngularJS的目标之一是简化开发过程,这使得AngularJS非常善于构建小型app原型,但AngularJS对于全功能的客 ...
- ADO.net数据访问
需要引用对应命名空间:System.Data.SqlClient; SqlConnection:连接对象SqlCommand:命令对象SqlDataReader:读取器对象 //造连接字符串 stri ...
- WindowsService+Quartz.NET快速搭建
新建一个Windows服务项目 nuget安装Quartz.NET,我这边使用的是2.3.3版本 1. Service改名 2. 添加安装程序,改名 3. ServiceInstaller->属 ...
- python学习笔记-02
四.函数 1.定义函数 (1)定义规则 介绍列表方法的时候已经大概说过函数,学过数学的人都知道函数,给一个参数返回一个值.函数也可以自己定义.用如下的格式: >>>def 函数名(参 ...
- java调用jacob生成pdf,word,excel横向
/* * 传进一个office文件的byte[]以及后缀,生成一个pdf文件的byte[] */ public byte[] jacob_Office2Pdf(byte[] srcFileBytes, ...
- EditControl 限制输入文本的三种方法
下边是三种限制编辑框输入内容的方法.在VS里建立基于对话框的应用程序CMyEdit,打开资源视图,删除对话框上自带的确定和取消按钮.然后添加一编辑框控件,将其ID修改为IDC_MY_EDIT,通过类向 ...