zoukankan      html  css  js  c++  java
  • HDOJ 3697 Selecting courses (贪心)

    分析:只需枚举4个起始时间即可,确定起始时间后,后面的时间就都确定了,求最大值时,利用贪心策略,每次都选结束时间最早的。

    题中说的时间不会超过1000,貌似数据中有大于1000的,被坑了……

    View Code
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    #define N 300
    struct node
    {
        int x,y;
        bool operator<(const node &t)   const
        {
            return y<t.y;
        }
    };
    int n;
    node t[N];
    void read()
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&t[i].x,&t[i].y);
        }
    }
    int cal(double x)
    {
        bool vis[N];
        memset(vis,0,sizeof(vis));
        int ret=0;
        for(double d=x;d<1000;d+=5)
        {
            for(int i=0;i<n;i++)    if(!vis[i])
            {
                if(d>t[i].x && d<t[i].y)
                {
                    ret++;
                    vis[i]=1;
                    break;
                }
            }
        }
        return ret;
    }
    void solve()
    {
        sort(t,t+n);
        int ans=0;
        for(double d=0.5;d<5;d+=1)
        {
            ans=max(ans,cal(d));
        }
        printf("%d\n",ans);
    }
    int main()
    {
        while(scanf("%d",&n),n)
        {
            read();
            solve();
        }
        return 0;
    }
  • 相关阅读:
    案例分析
    202103226-1 编程作业
    阅读任务
    准备工作
    结对作业
    案列分析
    202103226-1 编程作业
    《构建之法》有感
    准备工作
    案例分析作业
  • 原文地址:https://www.cnblogs.com/algorithms/p/2685326.html
Copyright © 2011-2022 走看看