ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

贪心-Doing Homework again HDU - 1789

2021-11-19 08:32:16  阅读:217  来源: 互联网

标签:node again HDU 1789 int price ddl arr time


题目

https://vjudge.net/problem/HDU-1789

思路一

思路一是,让价值尽量大的作业,尽量往后安排。

为了实现思路一,我们需要从后往前遍历”时间”,在每个时间节点选择满足当前条件的最大值,其中最大值我们使用优先队列实现。

以样例3为例,如图所示

1
7
1 4 6 4 2 4 3
3 2 1 7 6 5 4    

从右往左看,黄色表示选择做的作业,灰色表示已经做了,绿色是最后没做的

代码

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

struct node {
	int time, price;
	friend bool operator < (struct node a, struct node b)
	{
		if (a.price != b.price)	return a.price < b.price;
		else return a.time < b.time;
	}//使用小于号,对于优先队列,效果是price大的靠近top;对于sort函数,会把price小的排在数组前面
};

bool cmp(node a, node b) {
	return a.time > b.time;
}

int main(void) {
	int T = 0;
	cin >> T;
	while (T--) {
		priority_queue<node> Q;
		node arr[1005];
		int N, total = 0, ddl = 0;
		cin >> N;
		
		for (int i = 0; i < N; i++) {
			cin >> arr[i].time;
			ddl = ddl > arr[i].time ? ddl : arr[i].time;
		}
		for (int i = 0; i < N; i++) {
			cin >> arr[i].price;
			total += arr[i].price;
		}
		
		sort(arr, arr + N, cmp);
		int j = 0;
		for (int i = ddl; i >= 1; i--) {//错误:i >= 0
			for (; arr[j].time >= i && j < N; j++) {//错误:1. time >= ddl 2. 缺少&& j < N
			//	printf("%d has been push\n", arr[j].time);
				Q.push(arr[j]);
			}
			if (!Q.empty()) {
			//	printf("total is %d, Qtop is %d\n", total, Q.top().price);
				total -= Q.top().price;
				Q.pop();
			}
		}
		cout << total << endl;
	}
	
	return 0;
}

思路二

思路二的核心其实和思路一一样——让价值尽量大的作业,尽量往后安排。但是实现的方法不一样,既然我们想要让价值大的往后排,那我们不是只要让它在最后一天完成就好了?但这样可能会有重复,没关系,那就让它往前一天,还是不行就两天,以此类推。为此我们需要将价值从大到小排序。

还是以样例3为例子,价值从大到小,如图所示

1
7
1 4 6 4 2 4 3
3 2 1 7 6 5 4    

代码

#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;

struct node {
	int time, price;
};

bool cmp(node a, node b) {
	return a.price > b.price;
}

int main(void) {
	int T = 0;
	cin >> T;
	while (T--) {
		priority_queue<node> Q;
		node arr[1005];
		int vis[1005];
		memset(vis, 0, sizeof(vis));
		int N, total = 0, spend = 0, ddl = 0;
		cin >> N;
		
		for (int i = 0; i < N; i++) {
			cin >> arr[i].time;
			ddl = ddl > arr[i].time ? ddl : arr[i].time;
		}
		for (int i = 0; i < N; i++) {
			cin >> arr[i].price;
			total += arr[i].price;
		}
		
		sort(arr, arr + N, cmp);
		for (int i = 0; i < N; i++) {
		//	for (int k = 1; k <= ddl; k++)	printf("%d ", vis[k]);	cout << endl;
			int j;
			for (j = arr[i].time; j >= 1 && vis[j] != 0; j--){}//这里很多i啊j啊,一定要沉下心来理清逻辑
			vis[j] = arr[i].price;
		}
		//for (int k = 1; k <= ddl; k++)	printf("%d ", vis[k]);	cout << endl;
		for (int i = 1; i <= ddl; i++) spend += vis[i];
		cout << total - spend << endl;
	}
	return 0;
}

参考博客

  1. https://www.cnblogs.com/blumia/p/hdu1789.html
  2. https://blog.csdn.net/liluoyu_1016/article/details/78938559

标签:node,again,HDU,1789,int,price,ddl,arr,time
来源: https://www.cnblogs.com/tsrigo/p/15575398.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有