zoukankan      html  css  js  c++  java
  • URAL 1203 Scientific Conference(贪心 || DP)

    Scientific Conference


    之前一直在刷计算几何,邀请赛连计算几何的毛都买见着,暑假这一段时间就做多校。补多校的题目。刷一下一直薄弱的DP。多校假设有计算几何一定要干掉-。-


    题意:给你N个报告会的開始时间跟结束时间。问你做多能够听几场报告会。要求报告会之间至少间隔为1。


    思路:事实上是个活动安排问题。能够用贪心也能够用DP,贪心写起来会比較简单一些。由于练习DP,所以又用DP写了一遍。


    贪心的话就是一个非常easy的活动选择问题,从结束时间入手,找每次的最优选择。


    贪心:

    struct node{
        int b, e;
    } N[100005];
    
    int cmp(node x, node y){
        if(x.e == y.e)
            return x.b < y.b;
        return x.e < y.e;
    }
    
    int n;
    int main()
    {
        scanf("%d", &n);
        for(int i = 0; i < n; ++i){
            scanf("%d%d", &N[i].b, &N[i].e);
        }
        sort(N, N+n, cmp);
        int ans = 0;
        int t = 0;
        for(int i = 0; i < n; ++i)
        {
            if(N[i].b >= t)
            {
                ans++;
                t = N[i].e+1;
            }
        }
        printf("%d
    ", ans);
    
        return 0;
    }



    DP:

    struct node{
        int b, e;
    } N[100005];
    
    int cmp(node x, node y){
        if(x.e == y.e)
            return x.b < y.b;
        return x.e < y.e;
    }
    
    int n;
    int dp[30005];
    int k[30005];
    int main()
    {
        scanf("%d", &n);
        int last = -1;
        for(int i = 0; i < n; ++i){
            scanf("%d%d", &N[i].b, &N[i].e);
            last = max(last, N[i].e);
        }
        sort(N, N+n, cmp);
        for(int i = 0; i < n; ++i)
        {
            dp[N[i].e] = 1; ///记录结束时间是在
            k[N[i].e] = N[i].b;///记录结束时间的活动相应的開始时间
                               ///之前有排序所以选择会覆盖 会是最优的
        }
        for(int i = 1; i <= last; ++i)
        {///DP时间
            if(k[i]) ///假设当前时间点有结束的活动
                dp[i] = max(dp[i-1], dp[k[i]-1]+1);
            dp[i] = max(dp[i], dp[i-1]);///假设当前时间点没有结束的活动
        }
        printf("%d
    ", dp[last]);
    
        return 0;
    }



  • 相关阅读:
    每天一道LeetCode--141.Linked List Cycle(链表环问题)
    每天一道LeetCode--119.Pascal's Triangle II(杨辉三角)
    每天一道LeetCode--118. Pascal's Triangle(杨辉三角)
    CF1277D Let's Play the Words?
    CF1281B Azamon Web Services
    CF1197D Yet Another Subarray Problem
    CF1237D Balanced Playlist
    CF1239A Ivan the Fool and the Probability Theory
    CF1223D Sequence Sorting
    CF1228D Complete Tripartite
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6739055.html
Copyright © 2011-2022 走看看