B. Restaurant--cf579B (贪心)

http://codeforces.com/problemset/problem/597/B

把右节点从小到大排序  在跑一遍就行了

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <math.h>
#include <ctype.h>

using namespace std;
#define memset(a,b) memset(a,b,sizeof(a))
#define N 500100
typedef long long  ll;
const double ESP = 1e-8;
#define INF 0xfffffff

struct node
{
    int l,r;
}a[N];

int cmp(node c,node d)
{
    if(c.r!=d.r)
        return c.r<d.r;
    else
        return c.l<d.l;
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d %d",&a[i].l,&a[i].r);
        }
        sort(a,a+n,cmp);
        int sum=0;
        int ret=-1;
        for(int i=0;i<n;i++)
        {
            if(a[i].l>ret)
            {
                sum++;
                ret=a[i].r;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}
时间: 2024-12-21 22:01:18

B. Restaurant--cf579B (贪心)的相关文章

HDU 4883 TIANKENG’s restaurant (贪心)

链接:带我学习,带我飞 第一次BC,稳挂,WA n多次,今天重新做了一下 略挫 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <string> #include <vector> #include <set> #include <algorithm>

HDU4883 TIANKENG’s restaurant 【贪心】

TIANKENG's restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 324    Accepted Submission(s): 167 Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens

Codeforces 597B Restaurant(离散化 + 贪心)

题目链接 Restaurant 题目意思就是在n个区间内选出尽可能多的区间,使得这些区间互不相交. 我们先对这n个区间去重. 假如有两个区间[l1, r1],[l2, r2] 若满足l1 >= l2且 r1 <= r2,那么[l2, r2]就是可以被去掉的. 因为这两个区间里我们显然最多只能选择一个. 如果我们在答案里选择了[l2, r2],那么我们如果把[l2, r2]换成[l1, r1]的话 这个答案肯定还是满足题意的. 甚至可能腾出了可以放下其他区间的空间. 那么我们去重之后进行离散化,

HDU 4883--TIANKENG’s restaurant【区间覆盖 &amp;&amp; 暴力】

TIANKENG's restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 1366    Accepted Submission(s): 546 Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens

Restaurant

Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is charact

Testing Round #12 A,B,C 讨论,贪心,树状数组优化dp

题目链接:http://codeforces.com/contest/597 A. Divisibility time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Find the number of k-divisible numbers on the segment [a, b]. In other words you need

HDU 4883 TIANKENG’s restaurant

Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousands of customers come to have meal because of its delicious dishes. Today n groups of customers come to enjoy their meal, and there are Xi persons in the

杭电4883--TIANKENG’s restaurant(区间覆盖)

TIANKENG’s restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 1536    Accepted Submission(s): 556 Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens

【uva 1615】Highway(算法效率--贪心 区间选点问题)

题意:给定平面上N个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个店,都有一个选出的点离它的欧几里德距离不超过D. 解法:先把问题转换成模型,把对平面的点满足条件的点在x轴的直线上可得到一个个区间,这样就是选最小的点覆盖所有的区间的问题了.我之前的一篇博文有较详细的解释:关于贪心算法的经典问题(算法效率 or 动态规划).代码实现我先空着.挖坑~

【贪心+Treap】BZOJ1691-[Usaco2007 Dec]挑剔的美食家

[题目大意] 有n头奶牛m种牧草,每种牧草有它的价格和鲜嫩度.每头奶牛要求它的牧草的鲜嫩度要不低于一个值,价格也不低于一个值.每种牧草只会被一头牛选择.问最少要多少钱? [思路] 显然的贪心,把奶牛和牧草都按照鲜嫩度由大到小排序,对于每奶牛把鲜嫩度大于它的都扔进treap,然后找出后继. 不过注意后继的概念是大于它且最小的,然而我们这里是可以等于的,所以应该是找cow[i].fresh-1的后继,注意一下…… 1 #include<iostream> 2 #include<cstdio&