many activities will use the same place, every activity ai has its'  start time si and finish time fi.let the number of activities to be as many as possible.

1. dynamic programming

use ak be a knife to cut the set activities into two parts and recursive to find the max subset

c[i,j](star after ai finish and finish before aj star) = max {1+c[i,k] + c[k,j]} or 0(haven't ak);

2.greedy programming

let ai ranked by their finish time. earlier finish time ranked front than the later.

then choose the activities by its finish time, keep they are not contradictory.

 public class activity_select {
int[] s = {1,3,0,5,3,5,6,8,8,2,12};
int[] f = {4,5,6,7,9,9,10,11,12,14,16};
private static class activity{
private int sta ;
private int fin ;
public activity(){
sta = 0;
fin = 0;
}
} public activity[] select(){
activity[] act = new activity[s.length];
for(int i = 0;i<s.length;i++){ //initial
act[i] = new activity();
act[i].sta = s[i];
act[i].fin = f[i];
}
for(int i = 0;i<s.length;i++){ //insert sort from early fin to later fin
for(int j = i;j < s.length;j++){
if(act[i].fin > act[j].fin){
int testa = act[j].sta;
int tefin = act[j].fin;
act[j].sta = act[i].sta;
act[j].fin = act[i].fin;
act[i].fin = tefin;
act[i].sta = testa;
}
}
}
activity[] res = new activity[s.length];
res[0] = act[0];
int j = 0;
for(int i = 0;i < s.length -1;i++){
if(act[i+1].sta > res[j].fin){
res[++j] = act[i + 1];
}
}
activity[] res1 = new activity[j+1];
for(int i = 0;i <=j;i++){
res1[i] = res[i];
}
return res1;
} public static void main(String[] args){
activity_select ac = new activity_select();
activity[] a = ac.select();
int n = a.length;
for(int i = 0;i < n;i++){
System.out.println(a[i].sta + " " +a[i].fin);
}
} }

activity select problem(greedy algorithms)的更多相关文章

  1. Spectral Bounds for Sparse PCA: Exact and Greedy Algorithms[贪婪算法选特征]

    目录 概括 Sparse PCA Formulation 非常普遍的问题 Optimality Conditions Eigenvalue Bounds 算法 代码 概括 这篇论文,不像以往的那些论文 ...

  2. Greedy is Good

    作者:supernova 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=greedyAlg Joh ...

  3. an optimal solution to the problem

    http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Greedy/greedyIntro.htm Greedy Introdu ...

  4. Complexity and Tractability (3.44) - The Traveling Salesman Problem

    Copied From:http://csfieldguide.org.nz/en/curriculum-guides/ncea/level-3/complexity-tractability-TSP ...

  5. win32 socket之select

    之前光看理论是不行滴,一定要实践,实践啊,不然永远都是门外汉!! 嗯嗯,把找到的一段源码贴上先,稍微修改了一下: #include <winsock.h> #include <std ...

  6. [转]Using the Interop Activity in a .NET Framework 4 Workflow

    本文转自:http://msdn.microsoft.com/en-us/library/ee264174(v=vs.100).aspx This topic applies to Windows W ...

  7. (转)Awesome Courses

    Awesome Courses  Introduction There is a lot of hidden treasure lying within university pages scatte ...

  8. https那些事儿

    (一)SSL/TLS协议运行机制的概述 一.作用 不使用SSL/TLS的HTTP通信,就是不加密的通信.所有信息明文传播,带来了三大风险. (1) 窃听风险(eavesdropping):第三方可以获 ...

  9. CABaRet: Leveraging Recommendation Systems for Mobile Edge Caching

    CABaRet:利用推荐系统进行移动边缘缓存 本文为SIGCOMM 2018 Workshop (Mobile Edge Communications, MECOMM)论文. 笔者翻译了该论文.由于时 ...

随机推荐

  1. 泛型-----键值对----映射 hashmap--entry中key value 链表

    connection map 集合框架 * java.util.Collection *集合与数组相似,也是可以保存一组元素,并且提供了操作元素的相关方法. *collection是所有集合的顶级接口 ...

  2. opencv学习之路(19)、直方图

    一.概述 二.一维灰度直方图 #include "opencv2/opencv.hpp" #include<iostream> using namespace cv; ...

  3. 剑指offer(13)调整数组顺序使奇数位于偶数前面

    题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 题目分析 判断是 ...

  4. #const#const int *p 为何可以不初始化

    摘自http://www.myexception.cn/cpp/1900041.html const int *p 为什么可以不初始化?c++ primer 5th   P53 写道:const 对象 ...

  5. IIS 设备未就绪。

    看看Web.config 是否指向的磁盘在本机上不存在此磁盘

  6. 使用C#控制台应用程序完成一个2048小游戏

    曾经使用C#控制台应用程序写的一个2048,现在翻出来回顾一下 Box类是2048中每一个小格子的相关信息,包括格子的横纵坐标.格子的值和格子是否刚合并这些信息. Grid类是网格的相关信息,包括网格 ...

  7. 一: DRF web应用框架基础,及序列化器的使用

    ---恢复内容开始--- 一: web 应用模式(有两种) 1: 前后端不分离(前端从后端直接获取数据) 2: 前后端分离 二: api 接口 原因一: 为了在团队内部形成共识.防止个人习惯差异引起的 ...

  8. [原][OSG]OSG例子程序简介

    1.example_osganimate一)演示了路径动画的使用(AnimationPath.AnimationPathCallback),路径动画回调可以作用在Camera.CameraView.M ...

  9. 关于ExpressionChangedAfterItHasBeenCheckedError

    最近在stackoverflow上似乎每天都有一些关于angular报错‘ExpressionChangedAfterItHasBeenCheckedError’的问题.发生这些问题通常是由于angu ...

  10. hdoj4685

    数据: /*999993 43 1 2 42 2 32 3 4*/ #include <iostream> #include <cstdio> #include <cma ...