- 1、本文档共3页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
13-6poj2104 难度 4
K-th Number
Description
You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment.
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: What would be the k-th number in a[i...j] segment, if this segment was sorted?
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3).
The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.
Input
The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 = n = 100 000, 1 = m = 5 000).
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 = i = j = n, 1 = k = j - i + 1) and represents the question Q(i, j, k).
Output
For each question output the answer to it --- the k-th number in sorted a[i...j] segment.
Sample Input
7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3
Sample Output
5
6
3
原理:考虑到时间问题,定义变量时使用了结构体类型,其中结构体类型中定义了两个变量val(值)和 pos(位置),所以排序时只要用一次快速排序就可以了,快速排序中将 val 的值进行排序,而不改变pos 的值,然后通过搜索位置(pos)找到对应的值(val).
源 代 码 : #includestdio.h struct data
{
int pos,val;
} w[100000];
int main()
{
void quicksort(struct data w[],long int l,long int r); long int n,m,i,k,cnt,a,b,p;
scanf(%d %d,n,m); for(i=1;i=n;i++)
{ scanf(%d,w[i].val); w[i].pos=i;
//函数声明
//赋值
}
quicksort(w,1,n); for(i=1;i=m;i++)
{
//进行快排
scanf(%d%d%d,a,b,k); cnt=0;
for(p=1;p=n;p++) if(a=w[p].posw[p].pos=b)
{
//查找位置
cnt++; if(cnt==k)
break;
}
}
return 0;
}
printf(%d\n,w[p].val);
void quicksort(struct data w[],lo
您可能关注的文档
- PEP小学英语五年级下册句子复习(汉译英).docx
- PEP小学英语五年级下二单元复习课教学设计教案资料.docx
- pep小学英语音标练习.docx
- PEP小学英语语音资料.docx
- pep英语四年级下册第6单元教学设计新部编版.docx
- Permanent Magnets Design and Magnetic Field Analysis of Multidisc Coreless Permanent Magnet Synchro分析和总结分析和总结.docx
- PET核心词汇21天分析和总结.docx
- PE袋初包装封口验证报告.docx
- PE工程师试题答案(精华).docx
- PE管道安装质量评定表.docx
最近下载
- 2025年河南建筑职业技术学院单招职业适应性测试题库(满分必刷).docx VIP
- 2024年高考(新高考1卷、2卷、全国甲卷)小说阅读真题讲评(结合近四年命题规律).pptx VIP
- 2024年华远国际陆港集团所属企业招聘考试真题.docx VIP
- 17J008 挡土墙(重力式、衡重式、悬臂式)(最新).pdf
- 铁路巡防员培训课件.pptx
- 2024年高考(新高考1卷、2卷、全国甲卷)语言文字运用真题讲评(结合近四年命题规律).pptx VIP
- 江苏省建设工程监理现场用表(第六版修订版).doc VIP
- TZOIA-硅光电倍增管性能测试方法.pdf
- 第2版《植物学》配套题库(考研真题+章节题).pdf
- 2024年山西省华远国际陆港集团专业技术人员招聘考试真题.docx VIP
文档评论(0)