【BZOJ】1664: [Usaco2006 Open]County Fair Events 参加节日庆祝(线段树+dp)

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

posted @ 2014-09-09 13:09  iwtwiioi  阅读(487)  评论(0编辑  收藏  举报