• 任务调度问题(贪心) hdu4864


    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4864

    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.

    任务和机器都有两个属性,时长和等级。

    只有机器的时长和等级都大于等于一项任务,机器才能完成这项任务。并且,

    一项任务只能由一台机器完成,一台机器只能完成一项任务。( 每个任务有两个值花费时间x和难度y,)

    我的代码:

    #include<iostream>	//任务调度的贪心问题 
    #include<cstring>
    #include<cstdio>
    #include<algorithm>  
    using namespace std;
    typedef long long ll;  
      
    struct node  
    {  
        int x,y;  			//x:花费时间;y:难度等级。 
    }s1[100005],s2[100005];  	//机器数组s1,任务数组s2. 
    bool cmp(node a,node b)  
    {  
    	return (a.x>b.x)||(a.x==b.x && a.y>b.y);
    }//降序排序准则:按时间由大到小,时间相同则按等级由高到低。  
    //将任务按照权值1降序排序,权值1相同的按照权值二降序排序,将机器也同样是如此
    /*在给任务选择机器的时候,在满足要求的机器中(机器的两个权值都大于任务的两个权值)选择权值1最给小的分配给该任务,
    这样保证了后面任务做的可能性增大*/  
    int main()  
    {  
        int n,m,i,j,cnt;  
        ll sum;  
        while(~scanf("%d%d",&n,&m))  //输入机器数n,和任务数目m。 
        {  
            for(i=0;i<n;i++)  
                scanf("%d%d",&s1[i].x,&s1[i].y);  
            for(i=0;i<m;i++)  
                scanf("%d%d",&s2[i].x,&s2[i].y);  
            sort(s1,s1+n,cmp);  
            sort(s2,s2+m,cmp);		//按时间由大到小,时间相同则按等级由高到低,将机器和任务分别排序。
    //开始遍历任务:		  					//sum:the money they will get. 
    //找出所有xi(xi>=xj),从中选择yi最小的一个作为这个任务的运行机器, 
            cnt=sum=0;  		//cnt:the maximum number of the tasks which the company can complete today  ;
            int c[105]={0};  	//the level of the task的区间是[0,100]. c[]是任务难度等级数组。 
            for(i=0,j=0;i<m;i++) 	//j和n和机器为伍;i和m和任务为伍。 
            {  	
    								//对每一个任务而言:从第一个任务开始,挑选最合适的机器将其完成。 
                while(j<n && s1[j].x>=s2[i].x)  
                {  
                    c[s1[j].y]++;	//自增  
                    j++;  
                }
    			  
                for(int k=s2[i].y;k<=100;k++)  
                {  
                    if(c[k])  
                    {  
                        c[k]--;  //最合适的原则是:时间大于该任务且等级最低。检索完所有的任务即可得到结果。 
                        
    					sum+=(s2[i].x*500+s2[i].y*2);  
                        cnt++;  
                        break;		//break可以帮助退出这一层循环!  
                    }  
                } //就是要这么贪心滴! 
            }		  
            printf("%d %lld
    ",cnt,sum);  
        }
    	return 0;  
    }  
    

      

  • 相关阅读:
    门禁复制
    ImportError: cannot import name 'COMMAND' from 'MySQLdb.constants'
    Python3:模块:ModuleNotFoundError No module named 'MySQLdb'
    zookeeper问题:关于Unable to read additional data from server sessionid 0x0问题的解决
    Linux内存分析free与cache清理
    X-pack结合LDAP进行权限认证
    Django2.2框架:ORM数据库操作
    Django框架:模板继承和静态文件配置
    Djiango框架:模板语法
    Django2.2.x框架:基础篇(二)
  • 原文地址:https://www.cnblogs.com/dragondragon/p/11376708.html
Copyright © 2020-2023  润新知