ACM贪心总结

贪心法是一种解决问题的策略。如果策略正确,那么贪心法往往是易于描述,易于实现的。选择策略最关键的是读懂题,翻译能力和抽象能力。 如果不能提取有用的信息那么往往会将问题复杂化最后那自己绕进去。 贪心再进行操作前多会用sort排序或结构体然后对结构体的一个成员排序 根据其他成员的关系求解。

多机调度问题(上次列举了其中一种情况)
n个作业组成的作业集,可由m台相同机器加工处理。要求给出一种作业调度方案,使所给的n个作业在尽可能短的时间内由m台机器加工处理完成。作业不能拆分成更小的子作业;每个作业均可在任何一台机器上加工处理。当n<=m时,只要将作业时间区间分配给作业即可;当n>m时,首先将n个作业从大到小排序,然后依此顺序将作业分配给空闲的处理机。也就是说从剩下的作业中,选择需要处理时间最长的,然后依次选择处理时间次长的,直到所有的作业全部处理完毕,或者机器不能再处理其他作业为止。如果我们每次是将需要处理时间最短的作业分配给空闲的机器,那么可能就会出现其它所有作业都处理完了只剩所需时间最长的作业在处理的情况,这样势必效率较低

#include<iostream>    
#include<algorithm>      
using namespace std;    
int speed[10010];    
int mintime[110];    
  
bool cmp( const int &x,const int &y)    
{    
    return x>y;    
}    
  
int main()    
{    
    int n,m;           
    memset(speed,0,sizeof(speed));    
    memset(mintime,0,sizeof(mintime));    
    cin>>n>>m;    
    for(int i=0;i<n;++i) cin>>speed[i];    
    sort(speed,speed+n,cmp);    
    for(int i=0;i<n;++i)     
    {   
        *min_element(mintime,mintime+m)+=speed[i];     
    }     
    cout<<*max_element(mintime,mintime+m)<<endl;   
}  

区域覆盖问题在这里插入图片描述

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d.
We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates.
Figure A Sample Input of Radar Installations
Input
The input consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases.
The input is terminated by a line containing pair of zeros
Output
For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. “-1” installation means no solution for that case.

#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
struct yuan
{
    double l,r;
}a[1000000];
int cmp(yuan a,yuan b)
{
    return a.r<b.r;
}
int main()
{
    int m,n,c,d,p=0,s=0,o=0;
    double k;
    while(cin>>m>>n)
    {
        p=0;
        o++;
        if(m==0&&n==0)
        {
            break;
        }
        for(int i=0;i<m;i++)
        {
            cin>>c>>d;
            if(d>n)
            {
                p=1;
            }
            a[i].l=c-double(sqrt(n*n-d*d));//根据圆心计算出小岛在海岸线上的范围
            a[i].r=c+double(sqrt(n*n-d*d));
        }
        sort(a,a+m,cmp);//根据右区间的左标排序
        k=a[0].r,s=1;
        for(int i=1;i<m;i++)
        {
            if(a[i].l>k)
            {
                k=a[i].r;
                s++;//判断是否有重叠
            }
        }
        if(p==1)
            cout<<"Case "<<o<<": "<<-1<<endl;
        else
            cout<<"Case "<<o<<": "<<s<<endl;
    }
    return 0;
}

北京大学的许多研究生住在万柳校区,距离主校区 - 盐源4.5公里。万柳的学生必须乘坐公共汽车或骑自行车去学校。由于北京交通不畅,许多学生选择骑自行车。
我们可以假设除了“查理”以外的所有学生都以固定的速度从万柳到盐源。查理是一个有不同骑乘习惯的学生 - 他总是试图跟随另一个骑手,以避免单独骑车。当查理到达万柳大门时,他会找一个正在前往盐源的人。如果他找到某人,他将跟随那个骑手,如果没有,他会等待有人跟随。在从万柳到盐源的路上,如果一个更快的学生超过查理,他将随时离开他跟随的骑手并加快跟随更快的速度。我们假设查理到万柳门的时间是零。考虑到其他学生的出发时间和速度,你的任务是给Charley到达Yanyuan的时间。输入有几个测试用例。每种情况的第一行是N(1 <= N <= 10000),表示骑手的数量(不包括查理)。 N = 0结束输入。以下N行是N种不同骑手的信息,格式如下:Vi [TAB] Ti Vi是一个正整数<= 40,表示第i个骑手的速度(kph,每小时公里数)。 Ti是第i个骑手的起飞时间,它是一个整数并以秒为单位计算。在任何情况下都确保始终存在非负Ti。产量每个案例输出一行:查理的到达时间。在处理分数时向上舍入(上限)值。

这个题如果用模拟的办法将会非常复杂要考虑每次查理被超越时速度更换和每一次有人从出发到追上查理的时间(自己想着想着就把自己绕了进去)到了最后才知到,查理到达目标地点总是与最快的人时间相同。出发时间比查理早速度比查理快的查理是追不到的只需要先按时间排序先遍历将出发比查理晚速度比他慢的人删除 (也就是说查理就跟着耗时最短的)不多说了上代码。

#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
int main()
{
    int j,q=0,s=0;
    double m,n,a,b;
    double k=4.5,c,d;
    while(cin>>m)
    {
        if(m==0)
        {
            break;
        }
        c=360000;
        for(int i=0;i<m;i++)
        {
            cin>>a>>b;
            if(b>=0)
            {
                s=ceil(4.5*3600/a)+b;   //从初发到终点的时间
                if(s<c)
                {
                    c=s;
                }
            }

        }

        cout<<ceil(c)<<endl;


    }
}

猜你喜欢

转载自blog.csdn.net/u013345179/article/details/88702835