先按照结束时间进行排序,取第一个节日的结束时间作为当前时间,然后从第二个节日开始搜索,如果下一个节日的开始时间大于当前的时间,那么就参加这个节日,并更新当前时间

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e4+5;
struct node {
int be, ed; // 开始时间和结束时间
}nds[maxn];
bool cmp(node x, node y) {
return x.ed < y.ed;
}
int main() {
int n; cin >> n;
for (int i = 1; i <= n; ++i) {
int t, l; cin >> t >> l;
nds[i].be = t, nds[i].ed = t+l-1;
}
// 按照节日的结束时间从小到大进行排序
sort(nds+1,nds+1+n,cmp);
// 取第一个节日的结束时间作为当前的时间,答案赋值为1
int now_time = nds[1].ed, ans = 1;
for (int i = 2; i <= n; ++i) {
// 如果下一个节日的开始时间大于当前的时间,那么参加这个节日
// 因为是按照结束时间进行排序的,所以参加这个节日肯定最优
if (nds[i].be > now_time) {
// 答案加一
ans++;
// 更新当前时间
now_time = nds[i].ed;
}
}
cout << ans << endl;
return 0;
}

County Fair Events的更多相关文章

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

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

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

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

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

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

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

    http://www.lydsy.com/JudgeOnline/problem.php?id=1664 和之前的那题一样啊.. 只不过权值变为了1.. 同样用线段树维护区间,然后在区间范围内dp. ...

  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 1664: [Usaco2006 Open]County Fair Events 参加节日庆祝【dp+树状数组】

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

  8. usaco silver

    大神们都在刷usaco,我也来水一水 1606: [Usaco2008 Dec]Hay For Sale 购买干草   裸背包 1607: [Usaco2008 Dec]Patting Heads 轻 ...

  9. BZOJ-USACO被虐记

    bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...

随机推荐

  1. tp5.0--多个条件查询全部数据

    用where来查询的话(非主键): 查找:

  2. Android Studio常用配置

    目录 1. 主题颜色设置 2. Logcat颜色设置 3. 类注释 4. 编译器添加背景图 4.1 第一种方式 Background Image 4.2 第二种方式 Sexy Editor 5. 修改 ...

  3. CF1324 --- Maximum White Subtree

    CF1324 --- Maximum White Subtree 题干 You are given a tree consisting of \(n\) vertices. A tree is a c ...

  4. c++ 如何开N次方?速解

    c++ 如何开N次方?速解   直接上代码 #include <iostream> #include <cmath> using namespace std; typedef ...

  5. 【Linux常见命令】ip命令

    ip命令是用来配置网卡ip信息的命令,且是未来的趋势,重启网卡后IP失效. ip - show / manipulate routing, devices, policy routing and tu ...

  6. 《PostgreSQL服务器编程》一一1.3 超越简单函数

    本节书摘来自华章计算机<PostgreSQL服务器编程>一书中的第1章,第1.3节,作者:(美)Hannu Krosing, Jim Mlodgenski, Kirk Roybal 著,更 ...

  7. Math.Round和四舍五入

    Math.Round方法并不是像想象中的四舍五入, 可以从下面的输出结果看出来: Math.Round(3.44, 1); //Returns 3.4. Math.Round(3.45, 1); // ...

  8. Java实现zip文件解压[到指定目录]

    2019独角兽企业重金招聘Python工程师标准>>> package com.ljheee.ziptool.core; import java.io.File; import ja ...

  9. String(字符串) 比较大小 如果有A+B>B+A 则A>B

    题目引入: 给定N个整数,那任意顺序排列连成一个数,得到的最大的数是多少? 分析:贪心,字典序排序,都不对大小比较也不对,今天我跟别人想了很久绞尽脑汁,各种模拟都失败了.最后才发现对于俩个数a=313 ...

  10. json格式总结

    json格式分为两种: 1.键值对: 2.数组:其中元素可以是字符串.数字.数组,还可以相互嵌套 其中图片来源于:https://blog.csdn.net/huapenguag/article/de ...