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)论文. 笔者翻译了该论文.由于时 ...
随机推荐
- Poj1258 Agri-Net (最小生成树 Prim算法 模板题)
题目链接:http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...
- js中获取当天的时间的年月日
var d=new Date() var day=d.getDate() var month=d.getMonth() + 1 var year=d.getFullYear() document.wr ...
- 2019/3/28 wen 继承
- Docker Kubernetes 容器扩容与缩容
Docker Kubernetes 容器扩容与缩容 环境: 系统:Centos 7.4 x64 Docker版本:18.09.0 Kubernetes版本:v1.8 管理节点:192.168.1.79 ...
- Tomcat基本
Tomcat web 应用服务器基础 jdk+tomcat安装 1.运行Tomcat为什么要装jdk? http://blog.sina.com.cn/s/blog_753bc97d0102w5rd. ...
- P4512 【模板】多项式除法
思路 多项式除法板子 多项式除法 给出\(A(x)\)和\(B(x)\),求一个\(n-m\)次的多项式\(D(x)\),一个\(m-1\)次多项式\(R(x)\),满足 \[ A(x)=B(x)D( ...
- 阿里技术专家详解Dubbo实践,演进及未来规划
https://mp.weixin.qq.com/s/9rVGHYfeE8yM2qkSVd2yEQ
- Object.assign 的问题
功能及问题 如下代码, 使用用户最后一次配置信息的同时,当用户关闭数据记录时提示用户确定关闭. export default { name: 'editPage', data() { return { ...
- Hnoi-2017 滚粗记
一路走来,OI生涯中最重要的一场比赛在10个小时的比赛后,在不止10个小时的焦急等待中,也就这么结束了呢... Day 0: 当时其实内心里面还是比较虚的,还记得在回家的路上和$DYC$大佬畅想我们省 ...
- Django2.1.3 smtp 邮件 553报警
用网易邮箱smtp发邮件时候一直报警553权限问题 smtplib.SMTPSenderRefused at: (553, b'Mail from must equal authorized user ...