题目链接:http://acm.tju.edu.cn/toj/showp.php?pid=2867

题目大意:给定一系列活动的开始时间和结束时间,问最多能参加的活动数目

思路:// 本题属于最大区间调度问题,即数轴上有n个区间,选出最多的区间,使这些区间互相不重叠。算法:按右端点坐标排序,然后依次按后者的开始时间是否大于前者的结束时间(注意更新前者的下标)选择所有能选的区间。

代码:

//   本题属于最大区间调度问题,即数轴上有n个区间,选出最多的区间,使这些区间互相不重叠。
//    算法:按右端点坐标排序,然后依次按后者的开始时间是否大于前者的结束时间(注意更新前者的下标)选择所有能选的区间。
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
int s,e;
}activity[10000];
bool cmp(node a,node b)
{
 return a.e<b.e;  //按结束时间排序
}

int main()
{
  int n,d,st,i,ct,m;
  while(cin>>n&&n)
  {
    ct = 1;          //最多能参加的活动数  初始化为1!!
 for(i=0;i<n;i++)
 {
  cin>>st>>d;
  activity[i].s = st;
  activity[i].e = st+d;
 }
 sort(activity,activity+n,cmp);
 m=0;           
 for(i=1;i<n;i++)
 {
  if(activity[i].s>=activity[m].e)
  {
    ct++;   //如果后者的开始时间大于前者的结束时间,表明没有重合,能参加的活动数目加1
    m=i;    //后者和前者比,记着更新
  }
 }

 cout<< ct<<endl;
  }
  return 0;
}

toj2867 Picking Problem的更多相关文章

  1. VTK拾取网格模型上的可见点

    消隐与Z-Buffer 使用缓冲器记录物体表面在屏幕上投影所覆盖范围内的全部像素的深度值,依次访问屏幕范围内物体表面所覆盖的每一像素,用深度小(深度用z值表示,z值小表示离视点近)的像素点颜色替代深度 ...

  2. Google Code Jam 2010 Round 1B Problem B. Picking Up Chicks

    https://code.google.com/codejam/contest/635101/dashboard#s=p1   Problem A flock of chickens are runn ...

  3. Google Code Jam 2010 Round 1A Problem A. Rotate

    https://code.google.com/codejam/contest/544101/dashboard#s=p0     Problem In the exciting game of Jo ...

  4. Google Code Jam 2014 Round 1 A:Problem C. Proper Shuffle

    Problem A permutation of size N is a sequence of N numbers, each between 0 and N-1, where each numbe ...

  5. The Art of Picking Intel Registers Intel寄存器的艺术

    https://www.swansontec.com/sregisters.html I wrote this article for an online magazine called Scene ...

  6. Problem I: Ingenious Lottery Tickets

    Problem I: Ingenious Lottery Tickets Your friend Superstitious Stanley is always getting himself int ...

  7. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  8. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  9. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

随机推荐

  1. java匿名内部类,多态,接口练习

    1多态以及接口调用方法: public class Interface { public static void main(String[] args) { Al x = new Al(); jian ...

  2. ZooKeeper的学习与应用

    近期大概学习了一下ZooKeeper,本身并没有深入.LGG尝试着在虚拟机里面搭了平台,看了看一些教材,从网上到处看别人的博文并引用之,还请各位大牛们谅解我的剽窃.现总结例如以下. 1. ZooKee ...

  3. 浅谈Android系统开发中LOG的使用

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6581828 在程序开发过程中,LOG是广泛使用 ...

  4. 设计模式之八:外观模式(Facade)

    外观模式: 为子系统中的一系列接口提供了一个统一的界面.外观模式定义了一个高层次的接口以使子系统更加easy使用. Provide a unified interface to a set of in ...

  5. oracle函数Lpad与Rpad

    函数介绍 lpad函数从左边对字符串使用指定的字符进行填充.从其字面意思也可以理解,l是left的简写,pad是填充的意思,所以lpad就是从左边填充的意思. 语法格式如下: lpad( string ...

  6. [HeadFirst-HTMLCSS学习笔记][第十四章交互活动]

    表单 <form action="http://wickedlysmart.com/hfhtmlcss/contest.php" method="POST" ...

  7. 无法登陆mysql服务器

    解决 .#2002 无法登录 MySQL 服务器 将config.sample.inc.php复制成config.inc.php 出现这个错误,表示没有连接到数据库.修改config.inc.php文 ...

  8. AJAX 控件集之TextBoxWatermark(水印文本框)控件

    功能:       可以让TextBox控件初始化的时候拥有水印文字.属性:    TargetControlID :要使用具有水印效果的TextBox控件ID.    WatermarkCssCla ...

  9. asp.net 页面上传文件控件后台代码Request.Files获取不到

    今天开发中遇到页面文件上传控件选择了文件,而后台Request.Files.Count取值为0,之前开发中遇到过几次,老是忘掉,今天记下来. html: <input type="fi ...

  10. 0128——手势Gesture

    UIGestureRecognizer: 1.locationinView 获取手势在某个视图里面的坐标位置 2.delegate监听手势的行为 3.state状态 开始:UIGestureRecog ...