HDOJ 2037 今年暑假不AC 【暴力解決】⊙﹏⊙b汗

#include <stdio.h>
#include <algorithm>
using namespace std;
#define PLINE printf("\n------------------\n")
struct time
{
	int s,e;
}t[101];

int n, ans;

bool cmp(struct time a, struct time b)
{
	return a.s < b.s;
}
void search(int d, int num, int e)
{
	
	if(d == n)//剛開始竟然把層數寫錯了,╮(╯▽╰)╭,以後還是搞統一吧
	{
		if(num > ans)
			ans = num;
		
		return;
	}
	if(t[d].s >= e)
		search(d+1, num+1, t[d].e);
		
	search(d+1, num, e);
}
	
int main()
{
	int i;
	while(scanf("%d", &n) && n )
	{
		ans = 0;
		for(i=0; i<n; i++)
			scanf("%d%d", &t[i].s, &t[i].e);
			
		sort(t, t+n, cmp);

		search(0, 0, 0);

		printf("%d\n", ans);
	}
	return 0;
}

//貪心解法
include <stdio.h>
#include <algorithm>
using namespace std;
#define PLINE printf("\n------------------\n")
struct time
{
	int s,e;
}t[101];

bool cmp(struct time a, struct time b)
{
	return a.e < b.e;
}
	
int main()
{
	int i, n, ans, e;
	while(scanf("%d", &n) && n )
	{
		ans = 0;
		for(i=0; i<n; i++)
			scanf("%d%d", &t[i].s, &t[i].e);
			
		sort(t, t+n, cmp);
		e = 0;
		for(i=0; i<n; i++)
			if(t[i].s >= e)
			{
				e = t[i].e;
				ans++;
			}
		printf("%d\n", ans);
	}
	return 0;
}




發佈了164 篇原創文章 · 獲贊 1 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章