贪心FB

hdu 2037  今年暑假不AC

定义结构体,排序,然后贪心。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct T{
    int s,e;
};
T program[105];
bool  cmp(struct T a,struct T b)
{
    if(a.e != b.e)
        return a.e < b.e;
}

bool Selected[105];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF && n)
    {
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d",&program[i].s,&program[i].e);
        }
        sort(program,program+n,cmp);
        Selected[0] = true;
        int j = 0;
        for(int i = 1; i < n; i++)
        {
            if(program[i].s >= program[j].e)
            {
                Selected[i] = true;
                j = i;
            }
            else Selected[i] = false;
        }
        int sum = 0;
        for(int i = 0; i < n; i++)
        {
            if(Selected[i])
                sum++;
        }
        printf("%d\n",sum);
        memset(Selected,0,sizeof(Selected));
        memset(program,0,sizeof(program));
    }
    return 0;
}
时间: 2024-10-05 17:28:06

贪心FB的相关文章

BZOJ 1229 [USACO2008 Nov]toy 玩具(三分+贪心)

[题木链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1229 [题目大意] 每天对玩具都有一定的需求ni,每天可以花f价值每条购买玩具, 当天用完的玩具可以花费fA价值每个通过快消毒在A天之后得到一条可用的, 也可以通过花费fB价值每个,通过慢消毒在B天之后获得可用的 问满足每天需求所用的最小花费. [题解] 这是纸巾问题的费用流模型,费用流做法见 BZOJ 1221 [HNOI2001] 软件开发 但是我们发现N=100000的规模完全

hdu-5661 Claris and XOR(贪心)

题目链接: Claris and XOR Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description Claris loves bitwise operations very much, especially XOR, because it has many beautiful features. He gets four positive int

zoj 3659 Conquer a New Region 并查集+贪心

点击打开链接题目链接 Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history rolling forward, our king conquered a new region in a distant continent. There are N towns (numbered from 1 to N) in this region connected by s

集训第四周(高效算法设计)I题 (贪心)

Description Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is ei

CCPC2018 桂林 A: Array Merge(贪心、带权并查集合并)

时间限制: 1 Sec  内存限制: 128 MB提交: 37  解决: 3[提交] [状态] [命题人:admin] 题目描述 Given two arrays A, B of length n and m separately, you have to merge them into only one array C (of length n + m) obeying the rule that the relative order of numbers in the same origin

【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&

POJ1017 Packets(贪心算法训练)

Time Limit: 1000MS          Memory Limit: 10000K          Total Submissions: 51306          Accepted: 17391 Description A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These pro

ZOJ 3946 Highway Project 贪心+最短路

题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存在一系列边(ui,v)使得dis[v]最小(dis[v]表示0到v的距离).这些边能且只能选一条,那么我们自然应该选cost最小的那个边了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #inc