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;
}

最新文章

  1. SqlDataReader和SqlDataAdapter
  2. 史上最全系列Android开发环境搭建
  3. linux:vi 替换命令
  4. linux标准daemon编写方式
  5. Perl 语法 - 高级特性
  6. 七:zookeeper与paxos的分析
  7. [转] 苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文
  8. HTML5简单入门系列(八)
  9. 设计模式笔记之四:MVP+Retrofit+RxJava组合使用
  10. Linux学习总结(十二)—— CentOS用户管理:创建用户、修改用户、修改密码、密码有效期、禁用账户、解锁账户、删除用户、查看所有用户信息
  11. 《Head First Java》读书笔记(3) - 异常和IO
  12. pytorch_SRU(Simple Recurrent Unit)
  13. 宋宝华:Docker 最初的2小时(Docker从入门到入门)【转】
  14. BZOJ4289 Tax 最短路建模
  15. poj2441状态压缩dp基础
  16. 51nod 1009 - 数字1的数量 - [数位DP][模板的应用以及解释]
  17. Java之集合(二十一)LinkedTransferQueue
  18. 【Web Shell】- 技术剖析中国菜刀 – Part I
  19. [Android] 字体使用dp单位避免设置系统字体大小对排版的影响
  20. Oracle登录失败:监听程序当前无法识别连接描述符中请求的服务

热门文章

  1. Python—python2.7.5升级到2.7.14或者直接升级到3.6.4
  2. Python序列化,json&amp;pickle&amp;shelve模块
  3. 从 ClickHouse 到 ByteHouse:实时数据分析场景下的优化实践
  4. UE4类型数据自动注册
  5. A Child&#39;s History of England.19
  6. Java Jar包压缩、解压使用
  7. Linux基础命令---exportfs管理挂载的nfs文件系统
  8. @RestController和@Controller的区别与作用
  9. Redis,Memcache,MongoDb的特点与区别
  10. 关于ssh-keygen 生成的key以“BEGIN OPENSSH PRIVATE KEY”开头