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]);
}
}
CLRS: online maximum (n,k)algorithm的更多相关文章
- [Algorithm] Maximum Contiguous Subarray algorithm implementation using TypeScript / JavaScript
Naive solution for this problem would be caluclate all the possible combinations: const numbers = [1 ...
- 1420. Build Array Where You Can Find The Maximum Exactly K Comparisons
Given three integers n, m and k. Consider the following algorithm to find the maximum element of an ...
- [转载]Maximum Flow: Augmenting Path Algorithms Comparison
https://www.topcoder.com/community/data-science/data-science-tutorials/maximum-flow-augmenting-path- ...
- [UCSD白板题] Pairwise Distinct Summands
Problem Introduction This is an example of a problem where a subproblem of the corresponding greedy ...
- 【UVA 1380】 A Scheduling Problem (树形DP)
A Scheduling Problem Description There is a set of jobs, say x1, x2,..., xn <tex2html_verbatim_ ...
- 一位学长的ACM总结(感触颇深)
发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...
- Naive Bayes Theorem and Application - Theorem
Naive Bayes Theorm And Application - Theorem Naive Bayes model: 1. Naive Bayes model 2. model: discr ...
- 关于并行计算的Scan操作
simple and common parallel algorithm building block is the all-prefix-sums operation. In this chapte ...
- HUST 1352 Repetitions of Substrings(字符串)
Repetitions of Substrings Description The “repetitions” of a string S(whose length is n) is a maximu ...
随机推荐
- 关于oc中出现的typedef的用法/定义函数指针
typedef int (^calculateBlock)(int a,int b); 这里面typedef的作用只是给 calculateBlock取了一个 别名,说明以后可以直接使用. calcu ...
- BIP_开发案例04_通过BI Publisher实现打印报表的二维码(案例)(待整理)
2014-01-01 Created BaoXinjian
- 转__Android开源项目(三 完结篇)
http://www.csdn.net/article/2013-05-21/2815370-Android-open-source-projects-finale/1 截至目前,在GitHub“最受 ...
- 打印1到最大的n位数
打印1到最大的n位数----java实现 题目:输入数字n,按顺序打印出从1到最大的n位十进制数.比如,输入3,则打印出1,2,3,.....,一直到最大的3位数即999. 分析: 1.这是一个典型的 ...
- Linux 链接(转载)
来源:http://www.cnblogs.com/sonic4x/archive/2011/08/05/2128543.html 1.Linux链接概念Linux链接分两种,一种被称为硬链接(Har ...
- android之datepicker控件用法
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- SSH学习笔记目录
1.Spring_2000_Spring_Hibernate_HibernateTemplate
- 使用BeanUtils操作Bean属性
package com.wzh.test.beanutils; import java.lang.reflect.InvocationTargetException; import java.text ...
- json字符串转json对象的方法
在使用$.ajax()方法时,我们可以设置dataType:'json'的参数,便可以拿到后台返回的json数据对应的json对象.但有时,我们拿到的是json字符串,需要将它再转成json对象来使用 ...
- SQL查询结果增加序号列
--sql 2000 ) ,学号 ,姓名 from tb t --sql 2005 select 序号 = row_number() over(order by 学号),学号 ,姓名 from tb ...