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\n",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;
}

spoj - ACTIV - Activities的更多相关文章

  1. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  2. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  3. Android Do not keep activities选项分析

    Android Do not keep activities选项分析 Developer Options里面有一项: Do not keep activities -> 不保留Activitie ...

  4. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  5. 【填坑向】spoj COT/bzoj2588 Count on a tree

    这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...

  6. SPOJ bsubstr

    题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...

  7. 【SPOJ 7258】Lexicographical Substring Search

    http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...

  8. 【SPOJ 1812】Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...

  9. 【SPOJ 8222】Substrings

    http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...

随机推荐

  1. 单片机ISP、IAP和ICP几种烧录方式的区别

    单片机ISP.IAP和ICP几种烧录方式的区别 玩单片机的都应该听说过这几个词.一直搞不太清楚他们之间的区别.今天查了资料后总结整理如下. ISP:In System Programing,在系统编程 ...

  2. 2 — springboot的原理

    1.初步探索:第一个原理:依赖管理 发现:这里面存放着各种jar包 和 版本号 这也是:我们在前面第一个springboot项目创建中勾选了那个web,然后springboot就自动帮我们导入很多东西 ...

  3. 前端4 — jQuery — 更新完毕

    1.下载jQuery 网址:Download jQuery | jQuery  最好下载最新版的,因为有什么bug问题,最新版的都会有,所以学技术就用最新版的,实际开发用的时候就要讲究了 2.开始用j ...

  4. 虚机扩大容量与vm减少所占容量

    Linux的虚拟机碎片整理 sudo dd if=/dev/zero of=/free bs=1M sudo rm -f /free 镜像压缩 移动镜像 VBoxManage internalcomm ...

  5. spring boot druid数据源

    pom.xml配置 <!-- druid --> <dependency> <groupId>com.alibaba</groupId> <art ...

  6. Declarative Pipeline 基础语法

    Declarative Pipeline(声明式)核心概念 核心概念用来组织pipeline的运行流程 1.pipeline :声明其内容为一个声明式的pipeline脚本 2.agent:执行节点( ...

  7. Log4j漏洞源码分析

    Log4j漏洞源码分析 这几天Log4j的问题消息满天飞,今天我们就一起来看看从源码角度看看这个漏洞是如何产生的. 大家都知道这次问题主要是由于Log4j中提供的jndi的功能. 具体涉及到的入口类是 ...

  8. 在【自定义列】中编辑简单运算公式(Power Query 之 M 语言)

    数据源: "品名"."数量"."单价"三列 目标: 计算销售单价(单价*1.2) 解决方案: 在[自定义列]中使用乘法四则运算 步骤: 打开 ...

  9. java 图形化小工具Abstract Window Toolit 事件处理

    事件处理设计到了三个对象: EventSource(事件源):事件发生的场所,通常就是各个组件,例如按钮.窗口,菜单等. Event (事件封装了GUI组件上发生的特定事情(通常就是一次用户操作).如 ...

  10. java 数据类型:集合接口Collection之队列Queue:PriorityQueue ;Dequeue接口和ArrayDeque实现类:

    什么是Queue集合: Queue用于模拟队列这种数据结构,队列通常是"先进先出"(FIFO)的容器.队列的头部保存在队列中存放时间最长的元素,尾部保存存放时间最短的元素.    ...