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课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...
随机推荐
- lua_newthread的真正意义
lua_newthread 这个接口,存在误导性,很多人第一次试图用它来解决多线程问题时,都会入坑. 实际上,这个接口真正的用法,是给那些在lua更底层的某些行为(通常是递归)导致了lua的栈溢出而准 ...
- 省时省心DTM,广告转化无难题
内容来源:华为开发者大会2021 HMS Core 6 App Services技术论坛,主题演讲<华为分析服务,助您打造数智化运营闭环方案>. 演讲嘉宾:华为消费者云服务 分析产品总监 ...
- above, abrupt
above 近义词: over, beyond, exceeding反义词: below, beneath, under, underneath 有从右往左写的文字,没有从下往上的.above-men ...
- Vue相关,diff算法。
1. 当数据发生变化时,vue是怎么更新节点的? 要知道渲染真实DOM的开销是很大的,比如有时候我们修改了某个数据,如果直接渲染到真实dom上会引起整个dom树的重绘和重排,有没有可能我们只更新我们修 ...
- CSS系列,清除浮动方法总结
在非IE浏览器(如Firefox)下,当容器的高度为auto,且容器的内容中有浮动(float为left或right)的元素.在这种情况下,容器的高度不能自动伸长以适应内容的高度,使得内容溢出到容器外 ...
- [学习总结]5、Android的ViewGroup中事件的传递机制(二)
下面是第一篇的连接 Android的ViewGroup中事件的传递机制(一) 关于onInterceptTouchEvent和onTouchEvent的详细解释. 1 public class Mai ...
- go channel 概述
精髓 将资源读进内存-->共享内存,一个个进程/线程进行处理,这是常见模式.go channel 是一种直接在进程/线程之间传递资源的方式,即以通信来共享内存.这便是go的精髓. 扩展-一些名词 ...
- Mysql主从复制参数详解
目录 一.简介 二.例子 同步 修改 三.参数 一.简介 change master to配置和改变slave服务器用于连接master服务器的参数,以便slave服务器读取master服务器的bin ...
- GIT基本使用理解
基本区域介绍 git是一种代码管理工具,所以我们需要知道代码所在位置.分为4个区域: Workspace:工作区 Index / Stage:暂存区 Repository:本地仓库 Remote:远程 ...
- CF169A Chores 题解
Content 两兄弟要分担 \(n\) 件家务,第 \(i\) 件家务有一个复杂度 \(h_i\).兄弟俩以一个数 \(x\) 为界.所有满足复杂度 \(>x\) 的家务都给哥哥做,其余的给弟 ...