今年暑假不AC(贪心)

题目
时间限制:1 秒 内存限制:128 兆 特殊判题:否
题目描述:
“今年暑假不AC?”“是的。”“那你干什么呢?”“看世界杯呀,笨蛋!”“@#$%^&*%…”确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视作为球迷,一定想看尽量多的完整的比赛

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

按开始时间贪心:举例证明不行 [0,24] [1,2] [3,4]   ——只能选[0,24] 开始时间最早 能看1个; 而如果选择[1,2][3,4]虽然不是开始时间最早,但是能看2个

按持续时间长短贪心:举例证明不行 [1,4] [3,5] [4,20] ——只能选择[3,5] 持续时间最短 能看1个;而如果选择 [1,4][4,20] 能看2个

那只能按照 结束时间最短来贪心

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <stack>
using namespace std;


typedef struct TVProgram {
    int startTime;
    int endTime;
} TVProgram;


vector<TVProgram> res;

bool compare(TVProgram& a, TVProgram& b) {

    return a.endTime < b.endTime;
}


void print(vector<TVProgram> res) {
    for (int i = 0; i <= res.size() - 1; i++) {
        cout << res[i].endTime << endl;
    }

}

int main() {

    int n;
    while(cin >> n && n != 0) {
        res.clear();
        for (int i = 1; i <= n; i++) {
            TVProgram t;
            cin >> t.startTime;
            cin >> t.endTime;
            res.push_back(t);
        }

        sort(res.begin(), res.end(), compare);
       // print(res);

        int cnt = 0;
        int last = 0;
        for (int i = 0; i <= (int)res.size() - 1 ; i ++) {
            if (res[i].startTime >= last) { //注意是大于等于!!出错!!
                last = res[i].endTime;
                cnt ++;
            }
        }

        cout<<cnt<<endl;



    }



}

 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef struct Show {
	int startTime;
	int endTime;
} Show;

bool compare(Show& a,Show& b) {
	return a.endTime < b.endTime;
}

vector<Show> res;

int main() {
	int n;
	while(cin >> n && n != 0) {
		res.clear();
		Show t;
		for (int i = 1; i <= n; i++) {
			cin >> t.startTime >> t.endTime;
			res.push_back(t);
		}
		sort(res.begin(),res.end(),compare);

		int current = 0;
		int cnt = 0;
		for (int i = 0; i <= res.size() - 1; i++) {
			if (res[i].startTime >= current) {
				cnt++;
				current = res[i].endTime;
			}
		}

		cout << cnt << endl;;

	}


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值