activity select problem(greedy algorithms)
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)的更多相关文章
- Spectral Bounds for Sparse PCA: Exact and Greedy Algorithms[贪婪算法选特征]
目录 概括 Sparse PCA Formulation 非常普遍的问题 Optimality Conditions Eigenvalue Bounds 算法 代码 概括 这篇论文,不像以往的那些论文 ...
- Greedy is Good
作者:supernova 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=greedyAlg Joh ...
- an optimal solution to the problem
http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Greedy/greedyIntro.htm Greedy Introdu ...
- Complexity and Tractability (3.44) - The Traveling Salesman Problem
Copied From:http://csfieldguide.org.nz/en/curriculum-guides/ncea/level-3/complexity-tractability-TSP ...
- win32 socket之select
之前光看理论是不行滴,一定要实践,实践啊,不然永远都是门外汉!! 嗯嗯,把找到的一段源码贴上先,稍微修改了一下: #include <winsock.h> #include <std ...
- [转]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 ...
- (转)Awesome Courses
Awesome Courses Introduction There is a lot of hidden treasure lying within university pages scatte ...
- https那些事儿
(一)SSL/TLS协议运行机制的概述 一.作用 不使用SSL/TLS的HTTP通信,就是不加密的通信.所有信息明文传播,带来了三大风险. (1) 窃听风险(eavesdropping):第三方可以获 ...
- CABaRet: Leveraging Recommendation Systems for Mobile Edge Caching
CABaRet:利用推荐系统进行移动边缘缓存 本文为SIGCOMM 2018 Workshop (Mobile Edge Communications, MECOMM)论文. 笔者翻译了该论文.由于时 ...
随机推荐
- jquery单击事件的写法
方式一: //点击要素,修改URL $(document).on('click',"#modUrlYs" ,function (){ //$("#modUrlYs&quo ...
- TCP三次握手--syn攻击
TCP握手协议 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接.第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确 ...
- iperf使用指南
注意:iperf板上版本和PC上版本要一致,至少都要是2或者3,不能一个是2,一个是3. You also get a "connection refused" error whe ...
- Generative Adversarial Nets
1. 基本思想 两个模型: 判别器:预测从生成器生成的样本的概率 生成器:生成样本时,最大化使得判别器发生错误的概率 最后得到唯一解:使得生成器生成的样本输入到判别器中,得到的概率全是1/2. ...
- 谷歌技术"三宝"之BigTable
转自:https://blog.csdn.net/OpenNaive/article/details/7532589 2006年的OSDI有两篇google的论文,分别是BigTable和Chubby ...
- 树莓派(centos7)安装mysql
yum install mysql mysql-server 或者 yum install mariadb mariadb-server 安装完后启动mysql systemctl start mar ...
- IdentityServer4 错误解决方案
错误 解决方案 Grant types list cannot contain both xx and xx 以下授权类型不能同时存在: Implicit AuthorizationCode Impl ...
- vivado 创建PL工程
参考来源 https://china.xilinx.com/video/hardware/i-and-o-planning-overview.html 前言 我Win10系统上的Xilinx Plat ...
- CSS基础学习(一) 之 line-height 与 height 属性区别
官方定义: height:定义了了元素的高度.默认情况下,该属性订了 content area(内容区域) 的高度.如果box-sizing属性设置为 border-box,那么height就表示bo ...
- galera+mycat高可用集群部署
环境描述 10.30.162.29 client 环境描述 10.30.162.29 client 10.30.162.72 mysql1 10.30.162.73 mysql2 10.30.162 ...