贪心——HDU4864

对应HDU题目:点击打开链接

Task

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3427    Accepted Submission(s): 887

Problem Description

Today the company has m tasks to complete. The ith task need xi minutes to complete. Meanwhile, this task has a difficulty level yi. The machine whose level below this task’s level yi cannot complete this task. If the company completes
this task, they will get (500*xi+2*yi) dollars.

The company has n machines. Each machine has a maximum working time and a level. If the time for the task is more than the maximum working time of the machine, the machine can not complete this task. Each machine can only complete a task one day. Each task
can only be completed by one machine.

The company hopes to maximize the number of the tasks which they can complete today. If there are multiple solutions, they hopes to make the money maximum.

Input

The input contains several test cases.

The first line contains two integers N and M. N is the number of the machines.M is the number of tasks(1 < =N <= 100000,1<=M<=100000).

The following N lines each contains two integers xi(0<xi<1440),yi(0=<yi<=100).xi is the maximum time the machine can work.yi is the level of the machine.

The following M lines each contains two integers xi(0<xi<1440),yi(0=<yi<=100).xi is the time we need to complete the task.yi is the level of the task.

Output

For each test case, output two integers, the maximum number of the tasks which the company can complete today and the money they will get.

Sample Input

1 2
100 3
100 2
100 1

Sample Output

1 50004

首先,对于任何的task,因为最后的money是 500*x+2*y,所以,在得到最多money方面,肯定是x的优先级高,x相同的时候再比较y

然后题目是要求 在保证最多任务完成量的前提下,money最多,在任务完成量方面,首先,题目一个很明显的说法是一个机器只能跟一个task对应,所以,一个task如果只是占用了刚好够自己用的机器,那绝对就是最优的,因为就算替换他,也就是1换1,没有任何区别,当然,如果他占用了比自己好很多的机器就可能有问题了,因为可能有更高级的任务会被排开,这样原本可能可以完成2件 就只能完成1件,所以,我们只要保证这个task是刚好满足这个机器,即可

于是,我们把task按照money最大的原则降序排序,把他们依次放到最适合他们的机器上(机器照同样的规则排完序,然后二分即可)。如果能放就放,不能放就扔了。根据上面的分析,这样求出来的结果必然是最优的。。而且在存在多种可能的情况下,因为我是按最大价值放的,得到的money也必定是最优的

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
const int MAXN=100000+10;
using namespace std;
int num[100];

struct node
{
	int x,y;
}mac[MAXN],task[MAXN];

bool cmp(node m1, node m2)
{
	if(m1.x!=m2.x) return m1.x>m2.x;
	return m1.y>m2.y;
}

int main()
{
	//freopen("in.txt","r",stdin);
	int n,m;
	while(scanf("%d%d", &n,&m)==2)
	{
		memset(num,0,sizeof(num));
		int i,j;
		for(i=1; i<=n; i++){
			scanf("%d%d", &mac[i].x, &mac[i].y);
		}
		for(i=1; i<=m; i++){
			scanf("%d%d", &task[i].x, &task[i].y);
		}
		sort(mac+1, mac+n+1, cmp);
		sort(task+1, task+m+1, cmp);
		int cnt=0;
		long long sum=0;
		for(i=1,j=1; i<=m; i++){
			while(task[i].x<=mac[j].x && j<=n)
			{
				num[mac[j].y]++;
				j++;
			}
			for(int l=task[i].y; l<=100; l++){
				if(num[l]){
					sum+=(long long)(500*task[i].x+2*task[i].y);
					cnt++;
					num[l]--;
					break;
				}
			}
		}
		printf("%d %I64d\n",cnt, sum);
	}
	return 0;
}

贪心——HDU4864

时间: 2024-07-30 03:17:46

贪心——HDU4864的相关文章

[HDU4864]Task (贪心)

此图和上一篇博客的图一起看有奇效 题意 https://vjudge.net/problem/HDU-4864 思路 贪心 代码 by lyd 我实在是敲不来 #include <iostream> #include <cstdio> #include <cstring> #include <ctime> #include <algorithm> #include <cmath> #include <queue> #inc

hdu4864不是一般的贪心

题目表达的非常清楚,也不绕弯刚开始以为最大权匹配,仔细一想不对,这题的数据双循环建图都会爆,只能先贪心试一下,但一想贪心也要双循环啊,怎么搞? 想了好久没头绪,后来经学长提醒,可以把没用到的先记录下来嘛,以后就不用从头去找了,就像cache一样.我想了想就忽然开窍了,对啊,任务的价值只是一到一百而已,把先前遍历到的没用到的机器先记录下来,以后碰到合适的就不用再从头搜到尾了啊!这样外层100000里层100应该是能过的! 那么就用一个数组来标记机器的价值,cache[i]   表示价值i的机器有多

HDU4864:Task(贪心)

Problem Description Today the company has m tasks to complete. The ith task need xi minutes to complete. Meanwhile, this task has a difficulty level yi. The machine whose level below this task's level yi cannot complete this task. If the company comp

HDU 4864Task(多校联合训练1)(贪心)

题目地址:HDU4864 这题又是一上来认为是最小费用流,但是边太多,果然,敲完交上去后不断TLE..小优化了两次也没过...sad.. 后来看了题解才发现是贪心...贪心也不好想.大体思路是很好想的,就是先都按时间从大到小排序,再遍历任务,从机器里找能匹配的,并在能匹配的里边找等级尽量小的与之匹配.对我来说的突破点在于找能匹配的机器时不是每次都要重新找一遍,而是可以直接利用以前找到的.这就避免了n^2的复杂度.这样的时间复杂度就只有m*100,明显不会超时. 代码如下: #include <i

【uva 1615】Highway(算法效率--贪心 区间选点问题)

题意:给定平面上N个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个店,都有一个选出的点离它的欧几里德距离不超过D. 解法:先把问题转换成模型,把对平面的点满足条件的点在x轴的直线上可得到一个个区间,这样就是选最小的点覆盖所有的区间的问题了.我之前的一篇博文有较详细的解释:关于贪心算法的经典问题(算法效率 or 动态规划).代码实现我先空着.挖坑~

【贪心+Treap】BZOJ1691-[Usaco2007 Dec]挑剔的美食家

[题目大意] 有n头奶牛m种牧草,每种牧草有它的价格和鲜嫩度.每头奶牛要求它的牧草的鲜嫩度要不低于一个值,价格也不低于一个值.每种牧草只会被一头牛选择.问最少要多少钱? [思路] 显然的贪心,把奶牛和牧草都按照鲜嫩度由大到小排序,对于每奶牛把鲜嫩度大于它的都扔进treap,然后找出后继. 不过注意后继的概念是大于它且最小的,然而我们这里是可以等于的,所以应该是找cow[i].fresh-1的后继,注意一下…… 1 #include<iostream> 2 #include<cstdio&

POJ1017 Packets(贪心算法训练)

Time Limit: 1000MS          Memory Limit: 10000K          Total Submissions: 51306          Accepted: 17391 Description A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These pro

ZOJ 3946 Highway Project 贪心+最短路

题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存在一系列边(ui,v)使得dis[v]最小(dis[v]表示0到v的距离).这些边能且只能选一条,那么我们自然应该选cost最小的那个边了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #inc

CoderForce 140C-New Year Snowmen(贪心)

题目大意:有n个已知半径的雪球.堆一个雪人需要三个尺寸不同的雪球,问用这些雪球最多能堆多少个雪人? 题目分析:先统计一下每种尺寸的球的个数,从三种最多的种类中各取出一个堆成雪人,这样贪心能保证的到的数目最多. 代码如下: # include<iostream> # include<map> # include<vector> # include<cstdio> # include<queue> # include<algorithm>