CLRS:Max_sunsequence_sum O(n*n) O(nlgn) O(n)
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ARRAY_SIZE 1000
int buf [ARRAY_SIZE];
int main()
{
srand((unsigned int )time(0));
int i,j,n;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)buf[i]=rand()%100-10;
for(i=1;i<=n;i++)printf("%d ",buf[i]);
printf("\n");
//creat random value of buffer and print
//max subsequence sum
long int max_sum=0;
int start=1,end,rs;
for(i=1;i<=n;i++)
{
long int this_sum=0;
start=i;
for(j=i;j<=n;j++)
{
this_sum+=buf[j];
if(this_sum>max_sum)
{
max_sum=this_sum;
rs=start;
end=j;
}
}
}
printf("rs=%d,end=%d,max_sum=%ld\n",rs,end,max_sum);//print result
}
}
//O(n*n) brute-force
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ARRAY_SIZE 1000
int buf [ARRAY_SIZE];
long int max3(long int a,long int b,long int c)
{
return a>b?(a>c?a:c):(b>c?b:c);
}
long int max_subsequence_sum(int left,int right)
{
if(left==right)
{
if(buf[left]>0)return buf[left];
else return 0;
}
int mid=(left+right)/2;
long int max_left_sum= max_subsequence_sum(left, mid);
long int max_right_sum= max_subsequence_sum(mid+1, right);
int i,j;
long int max_leftbordersum=0,leftbordersum=0;
for(i=mid;i>=left;i--)
{
leftbordersum+=buf[i];
if(leftbordersum>max_leftbordersum) max_leftbordersum=leftbordersum;
}
long int max_rightbordersum=0,rightbordersum=0;
for(i=mid+1;i<=right;i++)
{
rightbordersum+=buf[i];
if(rightbordersum>max_rightbordersum) max_rightbordersum=rightbordersum;
}
return max3(max_left_sum,max_right_sum,max_rightbordersum+max_leftbordersum);
}
int main()
{
srand((unsigned int )time(0));
int i,j,n;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)buf[i]=rand()%100-10;
for(i=1;i<=n;i++)printf("%d ",buf[i]);
printf("\n");
//creat random value of buffer and print
//max subsequence sum
long int result=max_subsequence_sum(1,n);
printf("result=%ld\n",result);//print result
}
}
//O(nlgn)divide and conquer
//the pity is no sign of start and end of max subsequence sum
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ARRAY_SIZE 1000
#define RANDOM_SIZE 100
int buf [ARRAY_SIZE];
int main()
{
srand((unsigned int )time(0));
int i,j,n;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)buf[i]=rand()%RANDOM_SIZE-RANDOM_SIZE/2;
for(i=1;i<=n;i++)printf("%d ",buf[i]);
printf("\n");
//creat random value of buffer and print
//max subsequence sum
long int max_sum=0;
int start=1,end,rs;
long int this_sum=0;
for(i=1;i<=n;i++)
{
this_sum+=buf[i];
if(this_sum>max_sum)
{
max_sum=this_sum;
rs=start;
end=i;
}
else
{
this_sum=0;
start=i+1;
}
}
printf("rs=%d,end=%d,max_sum=%ld\n",rs,end,max_sum);//print result
}
}
//O(n)
CLRS:Max_sunsequence_sum O(n*n) O(nlgn) O(n)的更多相关文章
- 递归O(NlgN)求解逆序数
导言 第一次了解到逆序数是在高等代数课程上.当时想计算一个数列的逆序数直觉就是用两重循环O(n^2)暴力求解.现在渐渐对归并算法有了一定的认识,因此决定自己用C++代码小试牛刀. 逆序数简介 由自然数 ...
- 一种最坏情况线性运行时间的选择算法 - The missing worst-case linear-time Select algorithm in CLRS.
一种最坏情况线性运行时间的选择算法 - The missing worst-case linear-time Select algorithm in CLRS. 选择算法也就是求一个无序数组中第K大( ...
- [Algorithm] 如何正确撸<算法导论>CLRS
其实算法本身不难,第一遍可以只看伪代码和算法思路.如果想进一步理解的话,第三章那些标记法是非常重要的,就算要花费大量时间才能理解,也不要马马虎虎略过.因为以后的每一章,讲完算法就是这样的分析,精通的话 ...
- 经典算法分析:n^2与nlgn
冒泡.插入.选择排序的时间复杂度为O(n2) Arrays.sort()时间复杂度为nlgn 具体算法实现代码: package recursion; import java.util.Arrays; ...
- 算法导论(CLRS)答案
算法导论(CLRS)答案 Chapter Section I 1 2 p II 1 2 3 p III 1 2 p IV 1 2 3 4 p V 1 2 3 4 p VI 1 2 3 4 5 p VI ...
- POJ 3670 Eating Together 二分解法O(nlgn)和O(n)算法
本题就是一题LIS(最长递增子序列)的问题.本题要求求最长递增子序列和最长递减子序列. dp的解法是O(n*n),这个应该大家都知道.只是本题应该超时了. 由于有O(nlgn)的解法. 可是因为本题的 ...
- 低水平选手的自我救赎 (1)CLRS Exercise 16.5-2
题目大意 给定正整数 $n$ 和一个由 $m$ 个正整数构成的可重集合 $A$,满足 $\forall a\in A, a\le n$ 且 $m\le n$ . 定义 $N_t(A) = |\{a\i ...
- 【Bell-Ford 算法】CLRS Exercise 24.1-4,24.1-6
本文是一篇笔记,大部分内容取自 CLRS 第三版,第 24.1 节. Exercise 24.1-4 Modify the Bellman-Ford algorithm so that it sets ...
- poj1631Bridging signals(最长单调递增子序列 nlgn)
Bridging signals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12251 Accepted: 6687 ...
随机推荐
- spring学习笔记2(转)
1.在Java开发领域,spring相对于EJB来说是一种轻量级的,非侵入性的Java开发框架,曾经有两本很畅销的书<Expert one-on-one J2EE Design and Deve ...
- 停止某个机房所有机器上包的脚本 pack_idc_stop.py
一.初衷: 鉴于公司的进程包package都是冗余多点部署的,一般一个idc机房有多台机器部署同一个package.当机房网络出问题的时候,我们不得不查到本机房部署了哪些package,并在包发布系统 ...
- 编译器错误消息: CS0234: 命名空间“Purple”中不存在类型或命名空间名称“Model”(是否缺少程序集引用?)
编译错误 “/storeimg”应用程序中的服务器错误. 编译错误 说明: 在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地修改源代码. 编译器错误消息: CS023 ...
- Yii2.0 实现三级联动 [ 2.0 版本 ]
view中代码 <?php use yii\bootstrap\ActiveForm; /* @var $this yii\web\View */ /* @var $form yii\boots ...
- Xcode7--坑无法运行iOS9以下的模拟器
Unable to open liblaunch_sim.dylib. Try reinstalling Xcode or the simulator 解决办法 一.找到目标文件 /Applicati ...
- 原始ajax发起请求并反馈
在用户登陆的时候,离开用户.密码输入框即进行验证:ajax发起请求进行验证的: login.jsp代码: <%@ page language="java" contentTy ...
- .nil? .empty? .blank? .present? in Ruby on Rails
We get confused when there are many options to choose from. Same is the case when it comes to use an ...
- wall 和panel有啥区别
Panel指的是一个面板,用它来对一些控件进行分组,就像组合框控件,即Visual Studio里面用的Group Box Control:而在一些软件界面里面也可以表现为工具条,比如编辑工具条.文件 ...
- 用浏览器打开本地html 直接到首页 的解决方法
方法1:直接把本地文件拖到浏览器就可以打开
- 9.链式A+B
题目描述 有两个用链表表示的整数,每个结点包含一个数位.这些数位是反向存放的,也就是个位排在链表的首部.编写函数对这两个整数求和,并用链表形式返回结果. 给定两个链表ListNode* A,ListN ...