首页 > 代码库 > Task
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 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.
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.
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;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。