码迷,mamicode.com
首页 > 其他好文 > 详细

Task

时间:2014-08-06 18:03:01      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:des   blog   os   io   for   ar   div   代码   

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
time:234ms
#include"iostream"
#include"cstdio"
#include"cstring"
#include"cstdio"
#include"map"
#include"algorithm"
using namespace std;
const int ms=1e5+1;
struct node
{
    int time;
    int level;
}machine[ms],task[ms];
bool cmp(const node &a,const node &b)
{
    if(a.time==b.time)    
        return a.level>b.level;
    return a.time>b.time;
}
int main()
{
    int i,j,k,n,m;
    long long ans,sum;
    map<int,int> mp;
    while(scanf("%d%d",&n,&m)==2)
    {
        for(i=0;i<n;i++)
            scanf("%d%d",&machine[i].time,&machine[i].level);
        for(i=0;i<m;i++)
            scanf("%d%d",&task[i].time,&task[i].level);
        mp.clear();
        sort(machine,machine+n,cmp);
        sort(task,task+m,cmp);
        ans=0;sum=0;j=0;
        for(i=0;i<m;i++)
        {
            while(j<n&&machine[j].time>=task[i].time)
            {
                mp[machine[j].level]++;
                j++;
            }
            map<int,int>::iterator it=mp.lower_bound(task[i].level);
            if(it!=mp.end())
            {
                ans++;
                sum+=(500*task[i].time+2*task[i].level);
                int t=it->first;
                mp[t]--;
                if(mp[t]==0)
                    mp.erase(t);
            }
        }
        printf("%I64d %I64d\n",ans,sum);
    }
    return 0;
}

超时代码:付下

/*
	Name: 
	Copyright: 
	Author: 
	Date: 06/08/14 14:38
	Description: 
*/
#include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int ms=100001;
struct node
{
	int time;
	int level;
}task[ms],machine[ms];
bool cmp(const node &a,const node &b)
{
	if(a.time==b.time)
		return a.level<b.level;
	return a.time<b.time;
}
int vis[ms];
int main()
{
	int i,j,k,n,m;
	freopen("1004.in","r",stdin);
	freopen("1004a.txt","w",stdout);
	while(scanf("%d%d",&n,&m)==2)
	{
		for(i=1;i<=n;i++)
		{
			scanf("%d%d",&machine[i].time,&machine[i].level);
		}
		for(i=1;i<=m;i++)
		{
			scanf("%d%d",&task[i].time,&task[i].level);
		}
		sort(machine+1,machine+1+n,cmp);
		sort(task+1,task+1+m,cmp);
		int ans=0;
		long long sum=0;
		memset(vis,0,sizeof(vis));
		for(i=m;i;i--)
		{
			int minv=0xffffff;
			j=n;
			int id=0;
			while(j&&machine[j].time>=task[i].time)
			{
				if(machine[j].level>=task[i].level&&vis[j]==0)
				{
					if(machine[j].level<minv){
					
					minv=machine[j].level;
					id=j;}
				}
				j--;
			}
			if(id){
			vis[id]=1;
			ans++;
			sum+=500*task[i].time+2*task[i].level;
			}
		}
		//printf("%d %d\n",ans,sum);
		cout<<ans<<" "<<sum<<endl;
	}
	return 0;
}

  

Task,布布扣,bubuko.com

Task

标签:des   blog   os   io   for   ar   div   代码   

原文地址:http://www.cnblogs.com/767355675hutaishi/p/3894916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!