网站大量收购独家精品文档,联系QQ:2885784924

poj2104分析和总结分析和总结.docx

  1. 1、本文档共3页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 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

文档评论(0)

tianya189 + 关注
官方认证
内容提供者

该用户很懒,什么也没介绍

认证主体阳新县融易互联网技术工作室
IP属地湖北
统一社会信用代码/组织机构代码
92420222MA4ELHM75D

1亿VIP精品文档

相关文档