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课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...
随机推荐
- CMSIS-RTOS 信号量Semaphores
信号量Semaphores 和信号类似,信号量也是一种同步多个线程的方式,简单来讲,信号量就是装有一些令牌的容器.当一个线程在执行过程中,就可能遇到一个系统调用来获取信号量令牌,如果这个信号量包含多个 ...
- 『与善仁』Appium基础 — 18、元素定位工具(二)
目录 1.Appium Inspector介绍 2.Appium Inspector打开方式 3.Appium Inspector布局介绍 4.Appium Inspector工具的配置 5.Appi ...
- MYSQL获取更新行的主键ID 【转】
在某些情况下我们需要向数据表中更新一条记录的状态,然后再把它取出来,但这时如果你在更新前并没有一个确认惟一记录的主键就没有办法知道哪条记录被更新了. 举例说明下: 有一个发放新手卡的程序,设计数据库时 ...
- android 下动态获取控件的id
有时候我们需要动态的取得一个一个控件的id,然后进行操作,经过在网上查找,找到了一下方法getResources().getIdentifier("textView01", &qu ...
- Camera、音频录制与Vitamio框架
一.Camera 1.概述 Android框架包含了各种相机哥相机功能的支持,是你可以在应用中捕获图像和视频. 在应用能使用设备上的相机之前,先想一想将来会如何使用此硬件: (1)Camera 应该 ...
- 超过三张表禁止join
一. 问题提出 <阿里巴巴JAVA开发手册>里面写超过三张表禁止join,这是为什么? 二.问题分析 对这个结论,你是否有怀疑呢?也不知道是哪位先哲说的不要人云亦云,今天我设计sql,来验 ...
- Actuator监控器
一.简介 Actuator(激励者;执行器)是Spring Boot提供的一个可挺拔模块,用于对工程进行监控.其通过不同的监控终端实现不同的监控功能.其功能与Dubbo的监控中心类似,不同的是,Dub ...
- mybatis处理集合、数组参数使用in查询等语句的两种方法
对于mybatis的参数类型是集合数组的时候进行查询. 第一种:参数list使用mybatis的标签 SELECT * FROM TABLE_NAME AS a <where> <i ...
- numpy基础教程--将二维数组转换为一维数组
1.导入相应的包,本系列教程所有的np指的都是numpy这个包 1 # coding = utf-8 2 import numpy as np 3 import random 2.将二维数组转换为一维 ...
- useEffect无限调用问题
1.useEfect()的基本用法 const [test,setTest] = useState(1) const init=()=>{ setTest(2) } useEffect(()=& ...