题目意思是给出一些开区间,这些区间有的相交,有的不相交,问你能否选出一些区间,使这些区间之间都不相交,并且选出的区间数最大。

这是个典型的贪心问题了。按区间的结束位置排序,然后顺序地选取区间,只要当前区间与之前选的区间都不相交,就加入,否则就抛弃,这样就行了。道理很简单的,想想就能明白其正确性。

/*
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
const int MAXN = ;
typedef struct Interval {
int a, b;
Interval(int aa = , int bb = ) {
a = aa;
b = bb;
}
} Interval;
inline bool operator<(const Interval &i1, const Interval &i2) {
return i1.b < i2.b;
}
inline bool isCross(const Interval &i1, const Interval &i2) {
if (i1.a == i2.a) {
return true;
}
if (i1.a < i2.a) {
return i1.b > i2.a;
}
return i2.b > i1.a;
} Interval inte[MAXN], res[MAXN]; int main() {
int n, a, b;
while (scanf("%d", &n) == && n > ) {
for (int i = ; i < n; i++) {
scanf("%d%d", &inte[i].a, &inte[i].b);
}
sort(inte, inte + n);
int I = ;
res[I++] = inte[];
for (int i = ; i < n; i++) {
int j = ;
for (;j < I; j++) {
if (isCross(inte[i], res[j])) {
break;
}
}
if (j == I) {
res[I++] = inte[i];
}
}
printf("%d\n", I);
}
return ;
}

bjfu1252 贪心的更多相关文章

  1. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  2. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【BZOJ-4245】OR-XOR 按位贪心

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 486  Solved: 266[Submit][Sta ...

  7. code vs 1098 均分纸牌(贪心)

    1098 均分纸牌 2002年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 有 N 堆纸牌 ...

  8. 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心

    SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...

  9. 【贪心】HDU 1257

    HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...

随机推荐

  1. hdu 3123 GCC

    这题分2种情况: 1) n>=m时,k!%m=0(k>=m),所以只需令n=m-1即可: 2) n<m时,正常情况处理即可. ;}

  2. hdu 3923 Invoker

    完全是套用polya模版…… ;}

  3. hdu 1275 两车追及或相遇问题

    思路:这里有2种情况: 一种是相遇:满足关系是 (va+vb)*t=L*(2*n-1) 一种是追及: 满足关系是 |va-vb|*t=L*(2*n-1) 这样求出2种情况的时间,在排序就可以了…… 链 ...

  4. eclipse 或MyEclipse将工程进行移动的时候会对@Override报错的处理方法

    有时候导入javaSE,javaEE,android 工程的时候,明明是刚刚用过的没有问题的工程,但重新导入的时候就报错. 提示The method ... must override a sperc ...

  5. http://www.aboutyun.com/thread-8792-1-1.html

    http://www.aboutyun.com/thread-8792-1-1.html

  6. Bessie的体重问题

    P1028 Bessie的体重问题 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 USACO OCT09 8TH  描述 Bessie像她的诸多姊妹一样,因 ...

  7. lintcode:递归打印数字

    题目 用递归打印数字 用递归的方法找到从1到最大的N位整数. 样例 给出 N = 1, 返回[1,2,3,4,5,6,7,8,9]. 给出 N = 2, 返回[1,2,3,4,5,6,7,8,9,10 ...

  8. Linux开机启动流程

    开机过程指的是从打开计算机电源直到LINUX显示用户登录画面的全过程:       1)加载BIOS       2)读取MBR       3)Boot Loader       4)加载内核    ...

  9. 2014-9-17二班----11 web project

    http://localhost:8080/rwkj1/indexServlet?name=zhagnsan&pwd=1234 跳  转  http://localhost:8080/rwkj ...

  10. Oracle配置详解

    [Oracle连接字符串][Oracle Net Manager 服务命名配置][PL/SQL 登陆数据库] 连接数据库的几个重要参数: 1. 登陆用户名:user: 2. 登录密码:password ...