spoj - ACTIV - Activities
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的更多相关文章
- 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 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- Android Do not keep activities选项分析
Android Do not keep activities选项分析 Developer Options里面有一项: Do not keep activities -> 不保留Activitie ...
- 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 ...
- 【填坑向】spoj COT/bzoj2588 Count on a tree
这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
- SPOJ bsubstr
题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...
- 【SPOJ 7258】Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...
- 【SPOJ 1812】Longest Common Substring II
http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...
- 【SPOJ 8222】Substrings
http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...
随机推荐
- Linux升级命令yum upgrade和yum update的区别
Linux升级命令有两个分别是yum upgrade和yum update, 这个两个命令是有区别的: yum -y update 升级所有包同时也升级软件和系统内核 yum -y upgrade 只 ...
- Perl 常用的小细节总结
1.命令行:perl -c perl.pl #用来检验Perl脚本有没有错误: 2.vi perl.pl打开脚本,ESC+:set nu 回车,给每行加上行号:
- HDC2021技术分论坛:异构组网如何解决共享资源冲突?
作者:lijie,HarmonyOS软总线领域专家 相信大家对HarmonyOS的"超级终端"比较熟悉了.那么,您知道超级终端场景下的多种设备在不同环境下是如何组成一个网络的吗?这 ...
- day07 Nginx入门
day07 Nginx入门 Nginx简介 Nginx是一个开源且高性能.可靠的http web服务.代理服务 开源:直接获取源代码 高性能:支持海量开发 可靠:服务稳定 特点: 1.高性能.高并发: ...
- 【leetcode】153. Find Minimum in Rotated Sorted Array
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example ...
- ClassLoad类加载器与双亲委派模型
1. 类加载器 Class类描述的是整个类的信息,在Class类中提供的方法getName()是根据ClassPath配置的路径来进行类加载的.若类加载的路径为文件.网络等时则必须进行类加载这是就需要 ...
- Templates and Static variables in C++
Function templates and static variables: Each instantiation of function template has its own copy of ...
- Function overloading and const keyword
Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...
- Docker从入门到精通(一)——初识
1.Docker 是什么? Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容 ...
- Python matplotlib绘制圆环图
一.语法和参数简介 plt.pie(x2,labels=labels, autopct = '%0.2f%%', shadow= False, startangle =0,labeldistance= ...