zoukankan      html  css  js  c++  java
  • URAL1203——DPor贪心——Scientific Conference

    Description

    Functioning of a scientific conference is usually divided into several simultaneous sections. For example, there may be a section on parallel computing, a section on visualization, a section on data compression, and so on.
    Obviously, simultaneous work of several sections is necessary in order to reduce the time for scientific program of the conference and to have more time for the banquet, tea-drinking, and informal discussions. However, it is possible that interesting reports are given simultaneously at different sections.
    A participant has written out the time-table of all the reports which are interesting for him. He asks you to determine the maximal number of reports he will be able to attend.

    Input

    The first line contains the number 1 ≤ N ≤ 100000 of interesting reports. Each of the next N lines contains two integers Ts and Teseparated with a space (1 ≤ Ts < Te ≤ 30000). These numbers are the times a corresponding report starts and ends. Time is measured in minutes from the beginning of the conference.

    Output

    You should output the maximal number of reports which the participant can attend. The participant can attend no two reports simultaneously and any two reports he attends must be separated by at least one minute. For example, if a report ends at 15, the next report which can be attended must begin at 16 or later.

    Sample Input

    inputoutput
    5
    3 4
    1 5
    6 7
    4 5
    1 3
    
    3

    大意:找出最大的不相交的区间的个数

    原文题解   传送门

    贪心:

    对于所有的时间对于end进行排序,因为结束的越早后面的就越可以加进来,每一步都是最优的

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    struct edge{
        int b;
        int e;
    }a[100010];
    bool cmp(edge i, edge j){
        if(i.e == j.e)
            return i.b < j.b;
        return i.e < j.e;
    }
    int main()
    {
        int n;
        while(~scanf("%d",&n)){
            for(int i = 1; i <= n ;i++)
                scanf("%d%d",&a[i].b,&a[i].e);
            sort(a+1,a+n+1,cmp);
            int t = 0;
            int ans = 0;
            for(int i = 1; i  <= n; i++){
                if(a[i].b >= t){
                    t = a[i].e + 1;
                    ans++;
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    

    DP:

     用dp保存为当前时间有的结束的会场数,记做1,k数组用来存储以当前时间为结束的开始的时间,对于时间进行dp,如果当前有值的话,说明可以选择更新或者不更新,得到状态转移方程 dp[i] = max(dp[i-1], dp[k[i]-1]+1),并且更新所有节点dp[i] = max(dp[i-1],dp[i])保证所有的状态都存在

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    struct edge{
        int b;
        int e;
    }a[100010];
    int dp[100010];
    int k[30010];
    bool cmp(edge i, edge j){
        if(i.e == j.e){
            return i.b < j.b;
        }
        return i.e < j.e;
    }
    int main()
    {
        int n;
        while(~scanf("%d",&n)){
            int last = -1;
            for(int i = 1; i <= n ; i++){
                scanf("%d%d",&a[i].b,&a[i].e);
                last = max(last,a[i].e);
            }
            memset(dp,0,sizeof(dp));
            sort(a+1,a+n+1,cmp);
            for(int i = 1; i <= n ; i++){
                dp[a[i].e] = 1;
                k[a[i].e] = a[i].b;
            }
            for(int i = 1; i <= last; i++){
                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;
    }
    

      

  • 相关阅读:
    TOJ3650 Legal or Not
    Fork/Join框架之双端队列
    [每日一题] OCP1z0-047 :2013-08-26 TIMESTAMP WITH LOCAL TIME ZONE....................112
    [C++基础]C++中静态成员函数如何访问非静态成员
    HDU 1504 Disk Tree
    Android Checkbox Example
    STM32F103定时器输出PWM波控制直流电机
    SICP 习题 (1.7) 解题总结
    Linux磁盘及文件系统管理 4---- Linux文件系统挂载管理
    推荐《Linux 多线程服务器端编程》
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4483394.html
Copyright © 2011-2022 走看看