简单贪心

输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。 
Output对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。Sample Input

12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0

Sample Output

5
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<algorithm>
#include<functional>
#include<sstream>
#define maxn 102
using namespace std;
struct Node {
    int l, r;
} arr[maxn];

bool cmp(Node a, Node b) {
    return a.r < b.r;
}
int main()
{
    int n, i, ans, flag;
    while (scanf("%d", &n), n) {
        for (i = 0; i < n; ++i)
            scanf("%d%d", &arr[i].l, &arr[i].r);
        sort(arr, arr + n, cmp);
        flag = arr[0].r; ans = 1;
        for (i = 1; i < n; ++i)
            if (arr[i].l >= flag) {
                ++ans; flag = arr[i].r;
            }
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-10-11 19:45:57

简单贪心的相关文章

POJ 2393 Yogurt factory(简单贪心)

http://poj.org/problem?id=2393 题意:任务规定,一个酸奶制造厂,在n个星期内,分别要向外提供y[i]unit的酸奶.已知这个制造厂第i周制造每unit酸奶的费用为c[i],储存室储存每1unit酸奶1星期的费用为s.问要完成这个任务的最小费用是多少. . . . . . . . . . . . . . 思路:简单贪心.维护一个目前最优的代价minc = min(c, minc + s),然后求和. #include <iostream> using namespa

POJ 3069 Saruman&#39;s Army (简单贪心)

Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5343   Accepted: 2733 Description Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep track of his forces, Saruman distributes se

USACO SECTION1 2.1 Milking Cows 简单贪心

题目链接: http://train.usaco.org/usacoprob2?a=p8taXWtZBpU&S=milk2 题目描述: 给出n组数, 求出最长连续有数区间与最长连续无数区间 解题思路: 简单贪心, 先排序, 然后如果有交集判断右端点,如果右端点大则延伸, 否则continue, 如果无交集更新两个ans. 代码: http://train.usaco.org/usacoprob2?a=p8taXWtZBpU&S=milk2 思考: 简单的贪心我的代码却WA了, 记住一切从简

hdu 2037简单贪心--活动安排问题

活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动能兼容地使用公共资源 设有n个活动的集合E={1,2,…,n},其中每个活动都要求使用同一资源,如演讲会场等,而在同一时间内只有一个活动能使用这一资源.每个活动i都有 一个要求使用该资源的起始时间si和一个结束时间fi,且si <fi .如果选择了活动i,则它在半开时间区间[si, fi)内占用资源

POJ-3069-Saruman&#39;s Army(Java简单贪心)

Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4699   Accepted: 2430 Description Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep track of his forces, Saruman distributes se

HDU 1009:FatMouse&amp;#39; Trade(简单贪心)

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 41982    Accepted Submission(s): 13962 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g

HDU 1009:FatMouse&#39; Trade(简单贪心)

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 41982    Accepted Submission(s): 13962 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g

ACM_ICPC hdu-2111(简单贪心算法)

一道非常简单的贪心算法,但是要注意输入的价值是单位体积的价值,并不是这个物品的总价值!#include <iostream> #include <stdio.h> #include <algorithm> using namespace std; struct CT{ int pi; int mi; }; int cmp( CT p1 , CT p2 ){ return p1.pi > p2.pi ; } int main() { int sum , V , n

UVA 11292 Dragon of Loowater(简单贪心)

Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predato

三道类似的简单贪心

这几天遇到三道相似的贪心问题. 1. 汽车加油问题(算法设计与分析基础 习题4-9) 初始时油量为n.从起点到终点之间有k个加油站,汽车油箱容量上限为n,每个加油站可无限量供应汽油. 给出n,k和相对位置(在一条直线上),求最少加油次数及对应加油记录. 贪心策略:一直走,当到不了站点 i 时,在i-1加油至容量上限n(距离超过n时无解). 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 const