二、stl ,模拟,贪心等 [Cloned] R - 贪心

原题:

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.‘

题意:

一家公司有m个任务需要完成,每件任务有一个最少工作时间和一个任务难度,同时这家公司一共有n台机器,每台对应一个最大工作时间和机器的等级,用这n台机器来完成任务。每台机器每天能完成一件任务----这件任务的需要的工作时间和任务难度分别要不超过机器的最长工作时间和机器的等级,每完成一件任务,公司获得500*time+2*level的利润。给定这些任务和机器的详细情况,问最多能完成多少件任务,利润是多少?

题解:

由题意显然可以看出完成时间的范围远远大于等级,而且时间对的因子也远大于等级,所以时间对利润的影响更显著,所以先按照time排序,time相同再按照level排序。然后遍历任务,对每个任务找出时间最小,等级最低的符合任务的机器来完成这个任务,依然是贪心。

代码:AC

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100000+10;
struct Node
{
    int time;
	int level;
};
Node machine[maxn];
Node task[maxn];
bool cmp(Node a,Node b)
{
    if(a.time==b.time)
        return a.level>b.level;
    return a.time>b.time;
}
inline int ret(int x)
{
    return task[x].time*500+task[x].level*2;
}
int cnt[105];
int main()
{
    int M,N,i,k;
    while(cin>>N>>M)
    {
        for(i=1;i<=N;i++)
        {
			cin>>machine[i].time>>machine[i].level;
        }
        for(i=1;i<=M;i++)
        {
			cin>>task[i].time>>task[i].level;
        }
        sort(machine+1,machine+N+1,cmp);
        sort(task+1,task+M+1,cmp);
        int ans=0;
        long long sum=0;
        memset(cnt,0,sizeof(cnt));
        int j=1;
        for(i=1;i<=M;i++)
        {
            while(j<=N&&machine[j].time>=task[i].time)
            {
                cnt[machine[j].level]++;
                j++;
            }
            for(k=task[i].level;k<=100;k++)
            {
                if(cnt[k])
                {
                    cnt[k]--;
                    sum+=(long long)ret(i);
                    ans++;
                    break;
                }
            }
        }
		cout<<ans<<" "<<sum<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/npuyan/article/details/81395827
今日推荐