集训第四周(高效算法设计)D题 (区间覆盖问题)
原题 UVA10020 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19688
经典的贪心问题,区间上贪心当然是右区间越右越好了,当然做区间要小于等于当前待覆盖的位置,我们可以用一个变量begin代表当前待覆盖的区间的左值,每一次贪心成功就更新begin的值,然后再进行类似的重复动作
我之前写的版本.两次排序。
#include"iostream"
#include"cstdio"
#include"algorithm"
using namespace std; const int maxn=100000+10; struct node
{
int 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 main()
{
int T,t;
cin>>T;
t=T;
while(T--)
{
if(T!=t-1) cout<<endl;
int M,f,l,r,x,a1,b1;
cin>>M;
f=0;
while(cin>>a[f].x>>a[f].y&&(a[f].x||a[f].y))
{
if((a[f].x<0&&a[f].y<0)||(a[f].x>M&&a[f].y>M))
continue;
f++;
}
// cout<<f<<endl;
sort(a,a+f,cmp); if(a[0].x>0||f==0) cout<<0<<endl; else
{ sort(a,a+f,cm);
int ff=0,flag=0,sum=0,begi;
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;flag=1;sum++;b[sum].x=a[i].x;b[sum].y=a[i].y;break;
}
//cout<<begi<<endl;getchar();
if(flag)
{
flag=0;
}
else
{ff=1;break;}
}
if(ff) cout<<0<<endl;
else {
cout<<sum<<endl;
for(int i=1;i<=sum;i++)
cout<<b[i].x<<' '<<b[i].y<<endl;
}
}
}
return 0;
} 也可以只排序一次,这样的话需要记录当前已贪心到的数组下标位置,可以摒弃掉那些已经遍历过的那些数组元素,也是一样的时间
#include"iostream"
#include"cstdio"
#include"algorithm"
using namespace std; const int maxn=100000+10; struct node
{
int 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 main()
{
int T,t;
cin>>T;
t=T;
while(T--)
{
if(T!=t-1) cout<<endl;
int M,f;
cin>>M;
f=0;
while(cin>>a[f].x>>a[f].y&&(a[f].x||a[f].y))
{
if((a[f].x<0&&a[f].y<0)||(a[f].x>M&&a[f].y>M))
continue;
f++;
} sort(a,a+f,cmp); if(a[0].x>0||f==0) cout<<0<<endl; else
{
int ff=0,sum=0,begi,r;
begi=0;r=-1;
int i=0,j;
while(i<f)
{
j=i;
while(a[j].x<=begi &&j<f)
{
if(r<a[j].y)
{
r=a[j].y;
b[sum].x=a[j].x;
b[sum].y=a[j].y;
}
j++;
}
if(j==i) break;
i=j;
sum++;
begi=r;
if(begi>=M) {ff=1;break;}
}
if(ff==0) cout<<0<<endl;
else {
cout<<sum<<endl;
for(int i=0;i<sum;i++)
cout<<b[i].x<<' '<<b[i].y<<endl;
}
}
}
return 0;
}
集训第四周(高效算法设计)D题 (区间覆盖问题)的更多相关文章
- 高效算法——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 ...
- 集训第四周(高效算法设计)E题 (区间覆盖问题)
UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419 只能说这道题和D题是一模一样的,不过要进行转化, ...
- 集训第四周(高效算法设计)L题 (背包贪心)
Description John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...
随机推荐
- shiro之jdbcRealm
Shiro认证过程 创建SecurityManager--->主体提交认证--->SecurityManager认证--->Authenticsto认证--->Realm验证 ...
- Ignatius and the Princess III HDU - 1028 || 整数拆分,母函数
Ignatius and the Princess III HDU - 1028 整数划分问题 假的dp(复杂度不对) #include<cstdio> #include<cstri ...
- 判素数+找规律 BestCoder Round #51 (div.2) 1001 Zball in Tina Town
题目传送门 /* 题意: 求(n-1)! mod n 数论:没啥意思,打个表能发现规律,但坑点是4时要特判! */ /***************************************** ...
- 模拟/字符串处理 UVALive 6833 Miscalculatio
题目传送门 /* 模拟/字符串处理:主要是对*的处理,先把乘的预处理后再用加法,比如说是:1+2*3+4 = 1+..6+4 = 11 */ #include <cstdio> #incl ...
- 转 PHP抽象类:无法实例化 (不错)
http://blog.csdn.net/kaituozheboke/article/details/52183726 一.抽象类: 无法实例化 类前加 abstract, 此类就成为抽象类,无法实例 ...
- JavaScript异常处理和事件处理
异常捕获 1.异常: 当JavaScript引擎执行JavaScript代码时,发生了错误,导致程序停止运行 2.异常抛出: 当异常产生,并且将这个异常生成一个错误信息 3.异常捕 ...
- CF949A/950C Zebras
思路: 贪心乱搞. 实现: #include <bits/stdc++.h> using namespace std; vector<vector<int>> v; ...
- Win10 1803更新UWP无法安装的解决办法|错误代码0x80073D0D
升级Win10 1803后,出现了之前安装的UWP.应用无法更新,再此安装失败的现象. 应用商店错误代码为:0x80073D0D,尝试卸载重装商店,清除应用缓存也无法解决. 最终解决办法: 下载Eve ...
- 怎么学好XXX
怎么学好数据库是一个比较大题目,数据库不仅仅是写SQL那么简单,即使知道了SQL怎么写,还需要很清楚的知道这条SQL他大概扫描了多少数据,返回多少数据,是否需要创建索引.至于SQL优化是一个比较专业的 ...
- OpenGL Column-Major Matrix 使用注意事项
这column major的矩阵是彻底把我搞晕了,以后右乘规则下的矩阵应该这么用 假设我想创建一个2x2的矩阵,数学上我这么写: 1 2 3 4 用代码创建的话这么写 // 按照 row major ...