1. 程式人生 > >HDU 4864 Task(基本演算法-貪心)

HDU 4864 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.
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
Author FZU
Source
Recommend We have carefully selected several similar problems for you:  
4906
 4904 4903 4902 4901  題目大意:

有n臺機器,m個任務,每臺機器有xi,yi,每個任務也有xj,yj,當一個任務可以被處理的條件是,xj<=xi 且 yj<yi,處理完產生 500*xj+2*yj 的價值,問你最多產生的價值是多少?

解題思路:

注意y的範圍是 y<100,也就是x相差1,y不管相差多少價值都很少。

根據貪心的做法,肯定從高價值物品生產也就是按x排好序,再貪心,高價值的物品只需要在x比它大的所有機器中選擇y滿足條件的最小的那個(這個思考一下)

解題程式碼:
#include <iostream>
#include <set>
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn=110000;

struct node{
    int x,y;
    friend bool operator < (node a, node b){
        if(a.y!=b.y) return a.y<b.y;
        else return a.x<b.x;
    }
};

int n,m;
node a[maxn],b[maxn];

bool cmp(node a,node b){
    if(a.x!=b.x) return a.x>b.x;
    else return a.y>b.y;
}

void solve(){
    multiset <node> mys;
    sort(a,a+n,cmp);
    sort(b,b+m,cmp);
    int r=0,cnt=0;
    long long ans=0;
    for(int i=0;i<m;i++){
        while(r<n && a[r].x>=b[i].x) mys.insert(a[r++]);
        multiset <node>::iterator it=mys.lower_bound(b[i]);
        if(it!=mys.end()){
            mys.erase(it);
            cnt++;
            ans+=b[i].x*500+b[i].y*2;
        }
    }
    cout<<cnt<<" "<<ans<<endl;
}

int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=0;i<n;i++) scanf("%d%d",&a[i].x,&a[i].y);
        for(int i=0;i<m;i++) scanf("%d%d",&b[i].x,&b[i].y);
        solve();
    }
    return 0;
}


相關推薦

HDU 4864 Task基本演算法-貪心

Task Problem Description Today the company has m tasks to complete. The ith task need xi minutes to complete. Meanwhile, this task ha

HDU 4864 Task2014多校第一場1004貪心

題意:有n個機器和m個測試,只有機器的time和level都不小於測試的time和level才能通過測試,一個機器只能對應一個測試,一個測試只能對應一個機器。每通過一個測試可以得到金錢數:500*t

HDU 4864 Task經典貪心

傳送門: Task Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11382    Accepted Submission(s): 2782 Pr

HDU - 5884 Sort 二分答案+貪心

sca 優先 scan n) empty 每次 都是 scanf light 有n個數字,你需要把這n個數字合成一個數字,每次只能把k個數字合並成一個,花費為這k個數字的和。 給一個最大花費,問不超過這個最大花費的情況下,k的最小值。 Sample Input 1

HDU 6000 Wash 雙二分+貪心

Wash Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 64000/64000 K (Java/Others) Total Submission(s): 2613  

PCB 板邊倒圓角的實現方法基本演算法

當PCB外形是直角時,通常工程製作外形(鑼帶)時,會將直角或尖角的地方倒成圓角,主要是為了防止板邊容易劃傷板且容易扎傷人 所以當客戶沒有特殊要求時,PCB外形是直角一般會預設倒角0.5mm圓角(如下圖所示)  一.PCB板邊倒圓角點分析    原PCB外形  

HDU 4864 Task(貪心) 機器完成目標任務, 兩個權值, 小範圍打表

Task Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7171    Accepted Submiss

HDU 4864 Task (2014多校聯合訓練第一場1004) 解題報告貪心

Task Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 400    Accepted Submission(

HDU--4486 Task貪心

main 一個 一個數 pid sca 個數 lan efi scan 題目鏈接 4486 Task 按照時間從大到小排序 然後枚舉所有的y值 用一個數組存儲 符合要求就算上 #include<bits/stdc++.h> using namespa

HDU-3790 最短路徑問題 Dijkstra演算法優化

題目傳送門 題目:給你n個點,m條無向邊,每條邊都有長度d和花費p,給你起點s終點t,要求輸出起點到終點的最短距離及其花費,如果最短距離有多條路線,則輸出花費最少的。 這是一道模板題,然而卻做了整整一下午,剛開始用的Bellman-Ford的佇列優化做的,結果TLE,崩潰:(然後改成了優

串的基本操作KMP演算法實現

#include <iostream> #include <math.h> using namespace std; void StrAssign(char *T) { char ch; int i=1; cout<<"Please enter a str

HDU 1686 Oulipo hash演算法

Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2553  

HDU 4825 字典樹 +貪心

Zeus 和 Prometheus 做了一個遊戲,Prometheus 給 Zeus 一個集合,集合中包含了N個正整數,隨後 Prometheus 將向 Zeus 發起M次詢問,每次詢問中包含一個正整數 S ,之後 Zeus 需要在集合當中找出一個正整數 K ,使得 K 與 S 的異或結果最大。Pr

HDU 1009 FatMouse' Trade 簡單的貪心

Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food,

HDU 6299 Balanced Sequence流水線排程貪心

題意 給出 nn 個只包含左右括號的字串,將這些字串拼起來,要求最終拼出來的括號序列中,最長的完全匹配括號子序列的長度最長,問最長子序列長度。 輸入 第一行為一個整數 T

poj 1459 Power Network 初級->圖演算法->最大流基本演算法:增廣路

Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 19197 Accepted: 10125 Description A power network consists of nodes (pow

hdu 4864 Task 貪心

題意:有n個機器,m個任務。每個機器至多能完成一個任務。對於每個機器,有一個最大執行時間xi和等級yi,對於每個任務,也有一個執行時間xj和等級yj。只有當xi>=xj且yi>=yj的時候,機器i才能完成任務j,並獲得500*xj+2*yj金錢。問最多能完成幾個

HDU 4864 Task (貪心)

Task Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2154    Accepted Submission(

hdu 2546 飯卡01揹包+貪心

題目分析:要想使剩餘額最小,就要在就要用V=m-5的錢去買最多的菜,再用剩下的5元買最貴的菜; 先對菜價按升序排序,再用V買前i-1件 物品, #include<iostream> #include<cstdio> #include<algo

HDU 5360 Hiking優先隊列

maximum iss tle sta hit height oat spl play Hiking Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other