HDU 4864 task (Multi-school joint training 1) (Greedy)

Source: Internet
Author: User

Address: hdu4864

This question is regarded as the minimum charge stream, but there are too many sides. It is true that after the round-robin, it keeps fighting .. I have not optimized it twice... Sad ..

After reading the problem, I found it greedy... Greedy and hard to think about. The general idea is very good, that is, sorting by time first, then traversing the task and finding matching from the machine, and find the matching with the matching level as small as possible. The breakthrough point for me is that we can find matching machines instead of finding them again every time, but directly using the ones we found previously. This avoids the complexity of N ^ 2. In this way, the time complexity is only M * 100, and obviously it will not time out.
The Code is as follows:

#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include<algorithm>using namespace std;int _hash[110];struct node{    int time, lv;} task[110000], mac[110000];int cmp(node x, node y){    if(x.time==y.time)        return x.lv>y.lv;    return x.time > y.time;}int main(){    int n, m, i, j, num;    __int64 ans;    while(scanf("%d%d",&n,&m)!=EOF)    {        num=0;        ans=0;        for(i=0; i<n; i++)        {            scanf("%d%d",&mac[i].time,&mac[i].lv);        }        for(i=0; i<m; i++)        {            scanf("%d%d",&task[i].time,&task[i].lv);        }        sort(mac,mac+n,cmp);        sort(task,task+m,cmp);        j=0;        memset(_hash,0,sizeof(_hash));        for(i=0; i<m; i++)        {            while(j<n&&mac[j].time>=task[i].time)            {                _hash[mac[j].lv]++;                j++;            }            for(int k=task[i].lv; k<=100; k++)            {                if(_hash[k])                {                    _hash[k]--;                    num++;                    ans+=500*task[i].time+2*task[i].lv;                    break;                }            }        }        printf("%d %I64d\n",num,ans);    }    return 0;}


HDU 4864 task (Multi-school joint training 1) (Greedy)

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.