HDU 4864 task greedy

Source: Internet
Author: User
Task

Time Limit: 4000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1346 accepted submission (s): 336


Problem descriptiontoday 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.

 

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

 

Outputfor 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 input1 2100 3100 2100 1

 

Sample output1 50004

 

Authorfzu

 

Source2014 multi-university training contest 1 sorts tasks and machines in the ascending order of X and Y when X is equal. Traverse the task and find the machine with the smallest y value for each task as the machine that completes the task. At that time, we thought that we were greedy for every machine and sorted it in ascending order to find the most valuable task that can be done by each machine. This idea is wrong. Let's look at a group of counterexamples: 2 31126 771204 23 1032 4977 48944 22. The optimal strategy should be (1126,77) This machine completes (977,48) this task (1204,23) complete the task (, 4. But greedy for the machine, the result is (1126,77) completed (1032,4), (1204,23) completed (944,22 ). Here, y ranges from 0 to 100, which can be sorted by count.
#include <map>#include <set>#include <cmath>#include <queue>#include <stack>#include <cstdio>#include <string>#include <vector>#include <cstring>#include <iostream>#include <algorithm>#define ll long longusing namespace std;const int maxn = 100005;struct Node{    int x, y, id;}mc[maxn], ts[maxn];int cnt[maxn];bool cmp(Node a, Node b){    if (a.x!=b.x) return a.x>b.x;    return a.y>b.y;}int main(){    int n, m;    while (scanf("%d%d", &n, &m)==2) {        for (int i=0; i<n; i++) {            scanf("%d%d", &mc[i].x, &mc[i].y);        }        for (int i=0; i<m; i++) {            scanf("%d%d", &ts[i].x, &ts[i].y);        }        sort(ts, ts+m, cmp);        sort(mc, mc+n, cmp);        memset(cnt, 0, sizeof(cnt));        int num=0;        ll ans=0;        for (int i=0, j=0; i<m; i++) {            while (j<n&&mc[j].x>=ts[i].x) {                cnt[mc[j].y]++;                j++;            }            for (int k=ts[i].y; k<=100; k++) {                if (cnt[k]) {                    cnt[k]--;                    num++;                    ans+=500*ts[i].x+2*ts[i].y;                    break;                }            }        }        printf("%d %I64d\n", num, ans);    }    return 0;}
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.