列表

详情


NC25139. [USACO 2006 Ope S]County Fair Events

描述

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.

输入描述

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.

输出描述

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

示例1

输入:

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

输出:

4

说明:

Graphic picture of the schedule:

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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 11ms, 内存消耗: 476K, 提交时间: 2019-06-14 11:12:42

#include<bits/stdc++.h>
using namespace std;
const int N=99999;
int n;
struct node{
	int l,r;
}a[N];
bool cmp(node x,node y){
	return x.r<y.r;
}
int main(){
	cin>>n;
	for (int i=1;i<=n;i++){
		cin>>a[i].l>>a[i].r;
		a[i].r+=a[i].l-1;
	}
	sort(a+1,a+1+n,cmp);
	int r=a[1].r,ans=1;
	for (int i=2;i<=n;i++)
		if (a[i].l>r) 
			r=a[i].r,ans++;
	cout<<ans<<endl;
	return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 11ms, 内存消耗: 504K, 提交时间: 2020-02-17 17:42:21

#include<bits/stdc++.h>
using namespace std;
int n;
struct abcd
{
	int l,r;
}a[10010];
bool cmp(abcd x,abcd y)
{
	return x.r<y.r;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	cin>>a[i].l>>a[i].r,a[i].r+=a[i].l-1;
	sort(a+1,a+1+n,cmp);
	int r=a[1].r,ans=1;
	for(int i=2;i<=n;i++)
	if(a[i].l>r) r=a[i].r,ans++;
	cout<<ans<<"\n";
	return 0;
}

上一题