CLRS: online maximum (n,k)algorithm

//the first k elements interviewed and rejected,

//for  the latter n-k elements ,if value >max,return value,  else return n‘elements.

#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,n;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)buf[i]=rand()%RANDOM_SIZE;
for(i=1;i<=n;i++)printf("%d ",buf[i]);
printf("*\n");
//creat random value of buffer and print
//online maximum
int k=n/2,max=-RANDOM_SIZE;
for(i=1;i<=k;i++)
{
if(max<buf[i])max=buf[i];
}
for(i=k+1;i<=n;i++)
{
if(buf[i]>=max)
{
printf("%d\n",buf[i]);
exit(0);
}
}
printf("%d\n",buf[n]);
}
}

时间: 2024-10-12 03:17:25

CLRS: online maximum (n,k)algorithm的相关文章

[Algorithm] Maximum Contiguous Subarray algorithm implementation using TypeScript / JavaScript

The maximum subarray problem is one of the nicest examples of dynamic programming application. In this lesson we cover an example of how this problem might be presented and what your chain of thought should be to tackle this problem efficiently. /**

关于并行计算的Scan操作

simple and common parallel algorithm building block is the all-prefix-sums operation. In this chapter, we define and illustrate the operation, and we discuss in detail its efficient implementation using NVIDIA CUDA. Blelloch (1990) describes all-pref

Gym - 101845F 最大流

The UN finals are here!, the coaches/ex-coaches team is creating a new exciting contest to select which teams will compete in the Colombian finals. Ivan said: the rules are clear, the only limitation to enroll teams in the Colombian finals is to pick

pygrib学习

pygrib-2.0.3/docs/index.html 导入pygrib模块 >>> import pygrib 打开grib文件,获取grib消息迭代器 >>> grbs = pygrib.open('sampledata/flux.grb') 和打开正常的python文件对象一样,pygrib使用seek, tell, read, readline, 和close方法打开grib文件.唯一的不同的是,偏移量用grib消息衡量,而不是用字节. >>>

融合后如何如何后如何如何

http://ypk.39.net/search/all?k=%A6%C6%B6%F5%D6%DD%C4%C4%C0%EF%C2%F2%C3%C0%C9%B3%CD%AAQ%A3%BA%A3%B8%A3%B6%A3%B3%A3%B9%A3%B0%A3%B2%A3%B9%A3%B6%A3%B2%A1%C6 http://ypk.39.net/search/all?k=%A8z%BE%A3%C3%C5%C4%C4%C0%EF%C2%F2%C3%C0%C9%B3%CD%AAQ%A3%BA%A3%B8%

哥哥ukulele

http://ypk.39.net/search/all?k=%A1%FB%C6%CE%CC%EF%C4%C4%C0%EF%D3%D0%B0%B2%C0%D6%CB%C0%D2%A9%C2%F4Q%A3%BA%A3%B8%A3%B6%A3%B3%A3%B9%A3%B0%A3%B2%A3%B9%A3%B6%A3%B2%A1%BC http://ypk.39.net/search/all?k=%A1%D3%B8%A3%C7%E5%C4%C4%C0%EF%D3%D0%B0%B2%C0%D6%CB%C0

maximum sum of a subarray with at-least k elements.

// Returns maximum sum of a subarray with at-least // k elements. static int maxSumWithK(int a[], int n, int k) { // maxSum[i] is going to store maximum sum // till index i such that a[i] is part of the // sum. int maxSum[] = new int [n]; maxSum[0] =

[Algorithm] 如何正确撸&lt;算法导论&gt;CLRS

其实算法本身不难,第一遍可以只看伪代码和算法思路.如果想进一步理解的话,第三章那些标记法是非常重要的,就算要花费大量时间才能理解,也不要马马虎虎略过.因为以后的每一章,讲完算法就是这样的分析,精通的话,很快就读完了.你所说的证明和推导大概也都是在第三章介绍了,可以回过头再认真看几遍. 至于课后题,比较难,我只做了前几章,如果要做完需要更多时间和精力.这可以通过之后做算法题来弥补,可以去leetcode等网站找一些经典的算法题做一做,加深理解. Facebook的工程师写的攻略,介绍了用算法导论来

325. Maximum Size Subarray Sum Equals k

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Note: The sum of the entire nums array is guaranteed to fit within the 32-bit signed integer range. Example 1: Given