Poj 2388 Who's in the Middle

本文介绍了一个通过快速选择算法解决的问题:在一个奇数数量的奶牛群中找到产奶量中位数的奶牛。文章提供了完整的C++实现代码,并解释了如何使用随机化分区函数来提高算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Link:

http://poj.org/problem?id=2388

2.Content:

Who's in the Middle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 31854 Accepted: 18541

Description

FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'median' cow gives: half of the cows give as much or more than the median; half give as much or less.

Given an odd number of cows N (1 <= N < 10,000) and their milk output (1..1,000,000), find the median amount of milk given such that at least half the cows give the same amount of milk or more and at least half give the same or less.

Input

* Line 1: A single integer N

* Lines 2..N+1: Each line contains a single integer that is the milk output of one cow.

Output

* Line 1: A single integer that is the median milk output.

Sample Input

5
2
4
1
3
5

Sample Output

3

Hint

INPUT DETAILS:

Five cows with milk outputs of 1..5

OUTPUT DETAILS:

1 and 2 are below 3; 4 and 5 are above 3.

Source

3.Method:

 

4.Code:

 1 #include "iostream"
 2 #include "stdlib.h"
 3 #include "stdio.h"
 4 #include <time.h>
 5 #define N 10002
 6 using namespace std;
 7 int a[N];
 8 int Partition(int a[],int p,int r)
 9 {
10     int tmp = a[p];
11     while(p<r)
12     {
13         while(p<r && a[r]>=tmp) r--;
14         a[p]=a[r];
15         while(p<r && a[p]<=tmp) p++;
16         a[r]=a[p];
17     }
18     a[p]=tmp;
19     return p;
20 }
21 int RandomizePartition(int a[],int p,int r)
22 {
23     int i = rand()%(r-p+1)+p;
24     int tmp=a[i];
25     a[i]=a[p];
26     a[p]=tmp;
27     return Partition(a,p,r);
28 }
29 
30 int RandomizedSelect(int a[],int p,int r,int k)
31 {
32     if(p==r) return a[p];
33     int i = RandomizePartition(a,p,r),j = i-p+1;
34     if(k<=j) return RandomizedSelect(a,p,i,k);
35     else return RandomizedSelect(a,i+1,r,k-j);
36 }
37 int main()
38 {
39     srand(time(NULL));//初始化随机数生成器
40     int n;
41     int i;
42     scanf("%d",&n);
43     for(i=0;i<n;i++) scanf("%d",&a[i]);
44     //qsort(a,0,n-1);
45     //printf("%d\n",a[n/2]);
46     printf("%d\n",RandomizedSelect(a,0,n-1,(n+1)/2));
47     system("pause");
48     return 0;
49 }

 

5.Reference:

转载于:https://www.cnblogs.com/mobileliker/p/3937175.html

一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值