• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

  • 联系
  • 订阅
  • 管理

View Post

zoj 1076 Gene Assembly 贪心

       这题就是任务调度,给定任务的起始和结束时间,求能完成任务的最大数目。

       贪心,先按任务的结束时间递减排序,再逐个选择。选择活动的起始时间大于已选择活动的结束时间

 

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

const int N = 50005;

struct info
{
	int start, end;
	int id;
};

info a[N];
int n;

bool cmp(const info &a, const info &b)
{
	return a.end < b.end;
}

int main()
{
	int n;

	while (scanf("%d", &n) && n)
	{
		for (int i = 0; i < n; i++)
		{
			scanf("%d%d", &a[i].start, &a[i].end);
			a[i].id = i+1;
		}

		sort(a, a+n, cmp);
		vector<int> ans;

		ans.push_back(0);
		for (int i = 1; i < n; i++)
		{
			if (a[ ans[ans.size()-1] ].end <= a[i].start)
				ans.push_back(i);
		}

		for (int i = 0; i < ans.size()-1; i++)
			cout << a[ans[i]].id << " ";
		cout << a[ ans[ans.size()-1] ].id << endl;

	}
	return 0;
}

posted on 2011-02-27 21:18  sysuwhj  阅读(501)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3