http://www.lydsy.com/JudgeOnline/problem.php?id=1664

和之前的那题一样啊。。

只不过权值变为了1.。

同样用线段树维护区间,然后在区间范围内dp。

upd:(其实权值为1的可以直接贪心。。。。右端点来就行了。。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }
#define lc x<<1
#define rc x<<1|1
#define MID (l+r)>>1
#define lson l, m, lc
#define rson m+1, r, rc
const int N=10005;
int mx[N<<8], mxi, n;
struct dat { int a, b; }a[N];
bool cmp(const dat &a, const dat &b) { return a.a<b.a; }
void pushup(int x) { mx[x]=max(mx[lc], mx[rc]); }
void update(int l, int r, int x, int key, int p) {
if(l==r) {
mx[x]=key;
return;
}
int m=MID;
if(p<=m) update(lson, key, p); else update(rson, key, p);
pushup(x);
}
int query(int l, int r, int x, int L, int R) {
if(L<=l && r<=R) return mx[x];
int m=MID, ret=0;
if(L<=m) ret=query(lson, L, R); if(m<R) ret=max(ret, query(rson, L, R));
return ret;
} int main() {
read(n);
for1(i, 1, n) read(a[i].a), read(a[i].b), mxi=max(a[i].a+a[i].b, mxi);
sort(a+1, a+1+n, cmp);
int ans;
for1(i, 1, n) {
if(a[i].a<=1) ans=0; else ans=query(1, mxi, 1, 1, a[i].a-1);
update(1, mxi, 1, ans+1, a[i].a+a[i].b-1);
}
print(query(1, mxi, 1, 1, mxi));
return 0;
}

Description

Farmer John has returned to the County Fair so he can attend the special events (concerts, rodeos, cooking shows, etc.). He wants to attend as many of the N (1 <= N <= 10,000) special events as he possibly can. He's rented a bicycle so he can speed from one event to the next in absolutely no time at all (0 time units to go from one event to the next!). Given a list of the events that FJ might wish to attend, with their start times (1 <= T <= 100,000) and their durations (1 <= L <= 100,000), determine the maximum number of events that FJ can attend. FJ never leaves an event early.

有N个节日每个节日有个开始时间,及持续时间. 牛想尽可能多的参加节日,问最多可以参加多少. 注意牛的转移速度是极快的,不花时间.

Input

* Line 1: A single integer, N.

* Lines 2..N+1: Each line contains two space-separated integers, T and L, that describe an event that FJ might attend.

Output

* Line 1: A single integer that is the maximum number of events FJ can attend.

Sample Input

7
1 6
8 6
14 5
19 2
1 8
18 3
10 6

INPUT DETAILS:

Graphic picture of the schedule:
11111111112
12345678901234567890---------这个是时间轴.
--------------------
111111 2222223333344
55555555 777777 666

这个图中1代表第一个节日从1开始,持续6个时间,直到6.

Sample Output

4

OUTPUT DETAILS:

FJ can do no better than to attend events 1, 2, 3, and 4.

HINT

Source

【BZOJ】1664: [Usaco2006 Open]County Fair Events 参加节日庆祝(线段树+dp)的更多相关文章

  1. BZOJ 1664: [Usaco2006 Open]County Fair Events 参加节日庆祝( dp )

    先按时间排序( 开始结束都可以 ) , 然后 dp( i ) = max( dp( i ) , dp( j ) + 1 ) ( j < i && 节日 j 结束时间在节日 i 开 ...

  2. bzoj 1664: [Usaco2006 Open]County Fair Events 参加节日庆祝【dp+树状数组】

    把长度转成右端点,按右端点排升序,f[i]=max(f[j]&&r[j]<l[i]),因为r是有序的,所以可以直接二分出能转移的区间(1,w),然后用树状数组维护区间f的max, ...

  3. 1664: [Usaco2006 Open]County Fair Events 参加节日庆祝

    1664: [Usaco2006 Open]County Fair Events 参加节日庆祝 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 255  S ...

  4. bzoj1664 [Usaco2006 Open]County Fair Events 参加节日庆祝

    Description Farmer John has returned to the County Fair so he can attend the special events (concert ...

  5. [Usaco2006 Open]County Fair Events 参加节日庆祝

    Description Farmer John has returned to the County Fair so he can attend the special events (concert ...

  6. 【动态规划】bzoj1664 [Usaco2006 Open]County Fair Events 参加节日庆祝

    将区间按左端点排序. f(i)=max{f(j)+1}(p[j].x+p[j].y<=p[i].x && j<i) #include<cstdio> #incl ...

  7. [BZOJ 1218] [HNOI2003] 激光炸弹 【n logn 做法 - 扫描线 + 线段树】

    题目链接:BZOJ - 1218 题目分析 可以覆盖一个边长为 R 的正方形,但是不能包括边界,所以等价于一个边长为 R - 1 的正方形. 坐标范围 <= 5000 ,直接 n^2 的二维前缀 ...

  8. [BZOJ 3207] 花神的嘲讽计划Ⅰ【Hash + 可持久化线段树】

    题目链接:BZOJ - 3207 题目分析 先使用Hash,把每个长度为 k 的序列转为一个整数,然后题目就转化为了询问某个区间内有没有整数 x . 这一步可以使用可持久化线段树来做,虽然感觉可以有更 ...

  9. BZOJ.3218.a + b Problem(最小割ISAP 可持久化线段树优化建图)

    BZOJ UOJ 首先不考虑奇怪方格的限制,就是类似最大权闭合子图一样建图. 对于奇怪方格的影响,显然可以建一条边\((i\to x,p_i)\),然后由\(x\)向\(1\sim i-1\)中权值在 ...

随机推荐

  1. localhost连接慢如何解决?

    localhost连接慢如何解决 php 环境本地用ip连接mysql响应快, 今天碰到一个连接mysql的怪问题,我在php 环境本地用ip连接mysql响应快,用localhost连接慢,后来百度 ...

  2. 从头认识多线程-1.9 迫使线程停止的方法-return法

    这一章节我们来讨论一下还有一种停止线程的方法-return 1.在主线程上面return,是把全部在执行的线程都停掉 package com.ray.deepintothread.ch01.topic ...

  3. js&jquery获取指定table指定行里面的内容

      js&jquery获取指定table指定行里面的内容 CreateTime--2018年5月18日11:46:04 Author:Marydon 1.展示 代码展示 <table s ...

  4. 〖Android〗(CM10.2)Android4.2/4.3中的boot.img打包方法(合适三星exynos4 CPU)

    #!/bin/bash - #=============================================================================== # # F ...

  5. JDBC 利用反射技术将查询结果封装为对象(简单ORM实现)

    ORM(Object Relational Mapping)对象关系映射 public class ORMTest { public static void main(String[] args) t ...

  6. 站点CSS样式不起作用,或仅仅有一部分起作用?随手记

    事件:网页中使用了相同的样式,下半部分正常显示,上半部分样式所有丢失不能显示. 解决:改动了相应的CSS样式文件的编码 这个是最没有想到的解决的方法. 怎样调试:通过右键页面查看编码为UTF-8 或 ...

  7. mysql max和count、limit优化

    1.max 的优化记得要对max里面的字段使用索引,可以大大加快速度 2.count的优化,count(*)和count(id) 的结果可能是不一样的,因为可能存在null的情况 3.distinct ...

  8. GitHub上最火的Android开源项目(完结篇)

    摘要:截至目前,在GitHub“最受欢迎的开源项目”系列文章中我们已介绍了40个Android开源项目,对于如此众多的项目,你是Mark.和码友分享经验还是慨叹“活到老要学到老”?今天我们将继续介绍另 ...

  9. C#:ZedGraph画图控件(待补充)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. C union共用体

    共用体是一种特殊的数据类型,允许您在相同的内存位置存储不同的数据类型.您可以定义一个带有多成员的共用体,但是任何时候只能有一个成员带有值.共用体提供了一种使用相同的内存位置的有效方式. 共用体定义: ...