zoukankan      html  css  js  c++  java
  • spoj

    ACTIV - Activities

    Ana likes many activities. She likes acrobatics, alchemy, archery, art, Arabic dances, and many more. She joined a club that offers several classes. Each class has a time interval in every week. Ana wants to sign up for many classes, but since they overlap in time, she looks for a subset of non-overlapping classes to attend. A subset is non-overlapping if it does not contain two classes that overlap in time. If a class starts at the time another class ends, this is not considered overlapping.
    Ana decided to list all the non-overlapping non-empty subsets of classes. Then she will choose the subset she likes best. In order to predict the amount of paper needed to write the list, she wants you to calculate how many of these subsets there are.
    
    Input
    
    Each test case is described using several lines. The first line contains an integer N
    indicating the number of classes the club offers (1 ≤ N ≤ 105 ). Each of the next N lines
    describes a class using two integers S and E that represent the starting and ending times
    of the class, respectively (1 ≤ S < E ≤ 109 ). The end of input is indicated with a line
    containing a single −1.
    
    Output
    
    For each test case, output a single line with a single integer representing the number of
    non-overlapping non-empty subsets of classes. To make your life easier, output only the
    last 8 digits of the result. If the result has less than 8 digits, write it with leading zeros
    to complete 8 digits.
    Example
    
    Input: 
    5
    1 3
    3 5
    5 7
    2 4
    4 6
    3
    500000000 1000000000
    1 500000000
    1 500000000
    1
    999999999 1000000000
    -1
    
    Output: 
    00000012
    00000005
    00000001
    

    思路:离散化+dp
    dp[i]表示前i时刻能安排的不同方案数,那么假设当前任务为i,那么dp[a[i].y] = dp[a[i].x] + 1,需要先离散化;

    #include<stdio.h>
    #include<algorithm>
    #include<iostream>
    #include<queue>
    #include<string.h>
    #include<map>
    typedef long long LL;
    using namespace std;
    typedef struct node
    {
        int x,y;
    } ss;
    int ans[300006],bns[300006];
    ss a[300006];
    int er(int l,int r,int ask);
    bool cmp(node p,node q);
    LL dp[300006];
    LL mod = 1e8;
    int main(void)
    {
        int n;
        while(scanf("%d",&n),n!=-1)
        {
            int cn = 0;
            for(int i = 0; i < n; i++)
            {
                scanf("%d %d",&a[i].x,&a[i].y);
                ans[cn++] = a[i].x;
                ans[cn++] = a[i].y;
            }
            sort(ans,ans+cn);
            bns[1] = ans[0];int t = 1;
            for(int i = 1;i < cn;i++)
            {
                if(ans[i]!=ans[i-1])
                {
                    t++;
                    bns[t] = ans[i];
                }
            }
            for(int i = 0;i < n;i++)
            {
                a[i].x = er(1,t,a[i].x);
                a[i].y = er(1,t,a[i].y);
            }
            memset(dp,0,sizeof(dp));
            int u = 1;
            sort(a,a+n,cmp);
            for(int i = 0;i < n;i++)
            {
                while(u <= a[i].y)
                {
                   dp[u] = dp[u-1];
                   u++;
                }
                dp[u-1] = (dp[u-1] + dp[a[i].x] + 1)%mod;
            }
            printf("%08lld
    ",dp[a[n-1].y]);
        }
        return 0;
    }
    int er(int l,int r,int ask)
    {
        int mid = (l+r)/2;
        if(bns[mid] == ask)
         return mid;
        else if(bns[mid] > ask)
            return er(l,mid-1,ask);
        else return er(mid+1,r,ask);
    }
    bool cmp(node p,node q)
    {
        if(p.y == q.y)
            return p.x < q.x;
        else return p.y < q.y;
    }
    
  • 相关阅读:
    51单片机按键连击
    未知设备号创建设备节点
    C语言中enum的用法
    linux 端口IO操作
    读写控制台记录级别
    linux内核驱动中读写函数
    使用gawk记录一段时间内,某个进程占用内存和CPU的情况
    <Linux> Ubuntu error: ssh: connect to host master port 22: No route to host lost connection
    MapReduce的分区与 分组二次排序
    问题
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/6647262.html
Copyright © 2011-2022 走看看