Problem A : 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
解题思路:一个机器只能进行一次作业,要求最终的价值最大,是贪心问题,由于0=<yi<=100,value=500*xi+2*yi;可以看出在权重方面,y对value的贡献可以忽略
因此,优先考虑x,对于符合要求的机器,取y尽可能小的。由于n、m的值比较大,考虑o(nlogn),提高算法效率。
#include<iostream>
#include<algorithm>
#include<set>
#include<cstdio>
using namespace std;
#define maxn 110000
struct note{
int x;
int y;
friend bool operator <(note a,note b){//注意此处是重载小于号,返回的应该是小于号的规则
if(a.y!=b.y)
return a.y<b.y;//此处是小于
else
return a.x<b.x;
}
}aa[maxn],bb[maxn];
int n,m;
bool cmp(note a,note b){
if(a.x!=b.x)
return a.x>b.x;
else return a.y>b.y;
}
void solve(){
multiset<note>mp;//multiset类型可以将相同的数据放进去,此处mp为note类型,排序按照note定义的规则或默认规则
sort(aa,aa+n,cmp);
sort(bb,bb+m,cmp);
int t=0,count=0;
long long sum=0;
for(int i=0;i<m;i++){ //注意if的判定条件 &&bb[i].y<=aa[t].y,不可加入
while(t<n&&bb[i].x<=aa[t].x){//注意wh ile的范围,仅是将满足条件的插入,插入之后再对每个bb[i]作比较
mp.insert(aa[t++]);}//将a[t]插入,并按照默认或改写的规则排序
multiset<note>::iterator it=mp.lower_bound(bb[i]);//it指向大于等于bb[i]的位置
/if(it!=mp.end()){
mp.erase(it);//将it扔掉
count++;
sum=sum+bb[i].x*500+bb[i].y*2;
}
}
cout<<count<<" "<<sum<<endl;
}
int main(){
while(cin>>n>>m){
for(int i=0;i<n;i++) scanf("%d%d",&aa[i].x,&aa[i].y);
for(int i=0;i<m;i++) scanf("%d%d",&bb[i].x,&bb[i].y);
solve();
}
return 0;
}
#include<algorithm>
#include<set>
#include<cstdio>
using namespace std;
#define maxn 110000
struct note{
int x;
int y;
friend bool operator <(note a,note b){//注意此处是重载小于号,返回的应该是小于号的规则
if(a.y!=b.y)
return a.y<b.y;//此处是小于
else
return a.x<b.x;
}
}aa[maxn],bb[maxn];
int n,m;
bool cmp(note a,note b){
if(a.x!=b.x)
return a.x>b.x;
else return a.y>b.y;
}
void solve(){
multiset<note>mp;//multiset类型可以将相同的数据放进去,此处mp为note类型,排序按照note定义的规则或默认规则
sort(aa,aa+n,cmp);
sort(bb,bb+m,cmp);
int t=0,count=0;
long long sum=0;
for(int i=0;i<m;i++){ //注意if的判定条件 &&bb[i].y<=aa[t].y,不可加入
while(t<n&&bb[i].x<=aa[t].x){//注意wh ile的范围,仅是将满足条件的插入,插入之后再对每个bb[i]作比较
mp.insert(aa[t++]);}//将a[t]插入,并按照默认或改写的规则排序
multiset<note>::iterator it=mp.lower_bound(bb[i]);//it指向大于等于bb[i]的位置
/if(it!=mp.end()){
mp.erase(it);//将it扔掉
count++;
sum=sum+bb[i].x*500+bb[i].y*2;
}
}
cout<<count<<" "<<sum<<endl;
}
int main(){
while(cin>>n>>m){
for(int i=0;i<n;i++) scanf("%d%d",&aa[i].x,&aa[i].y);
for(int i=0;i<m;i++) scanf("%d%d",&bb[i].x,&bb[i].y);
solve();
}
return 0;
}