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. numpy linalg模块

    # 线性代数# numpy.linalg模块包含线性代数的函数.使用这个模块,可以计算逆矩阵.求特征值.解线性方程组以及求解行列式等. import numpy as np # 1. 计算逆矩阵# 创 ...

  2. dva构建react项目

    第一步:安装 dva-cli 1 cnpm install dva-cli -g 第二步:采用dva来创建项目: 1 dva new react_two 2 cd react_two 用webstor ...

  3. RabbitMQ详解(三)------RabbitMQ的五种模式

    RabbitMQ详解(三)------RabbitMQ的五种模式 1.简单队列(模式) 上一篇文章末尾的实例给出的代码就是简单模式. 一个生产者对应一个消费者!!! pom.xml ​ 必须导入Rab ...

  4. 剑指offer(37)数字在排序数组中出现的次数。

    题目描述 统计一个数字在排序数组中出现的次数. 题目分析 这题用暴力解也可以过,不过面试官肯定期待更好的解法. 查找我们最熟悉的就是二分查找了,不过二分查找查找的数在数组中只有一个,我们这里却有很多个 ...

  5. ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails ()

    centos7.5 删除表空间文件失败 问题: mysql> alter table country discard tablespace; ERROR 1451 (23000): Cannot ...

  6. maven开发项目中遇到的问题

    java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start com ...

  7. react-router 4.0(四)页面跳转验证提示

    import React from 'react' import ReactDOM from 'react-dom' import { HashRouter, Route, Link, Prompt ...

  8. Leetcode480-Binary Tree Paths-Easy

    480. Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Example Example 1: Input: ...

  9. Linux学习进阶示意图

    Linux 基础 Linux 基础 Linux安装专题教程 Linux中文环境 Linux—从菜鸟到高手 鸟哥的Linux私房菜 基础学习篇(第二版) Ubuntu Linux入门到精通 Linux标 ...

  10. _reincarnation

    可以设定转生等级和转生需求.来奖励转生 `level`转生等级 `reqId` 转生需求 `rewId` 转生奖励 `gossipText` 菜单显示