POJ 一 2371 Questions and answers
来源:互联网 发布:背包推荐 知乎 编辑:程序博客网 时间:2024/06/12 07:44
Description
The database of the Pentagon contains a top-secret information. We don't know what the information is — you know, it's top-secret, — but we know the format of its representation. It is extremely simple. We don't know why, but all the data is coded by the natural numbers from 1 up to 5000. The size of the main base (we'll denote it be N) is rather big — it may contain up to 100 000 those numbers. The database is to process quickly every query. The most often query is: "Which element is i-th by its value?"— with i being a natural number in a range from 1 to N.
Your program is to play a role of a controller of the database. In the other words, it should be able to process quickly queries like this.
Your program is to play a role of a controller of the database. In the other words, it should be able to process quickly queries like this.
Input
The standard input of the problem consists of two parts. At first, a database is written, and then there's a sequence of queries. The format of database is very simple: in the first line there's a number N, in the next N lines there are numbers of the database one in each line in an arbitrary order. A sequence of queries is written simply as well: in the first line of the sequence a number of queries K (1 <= K <= 100) is written, and in the next K lines there are queries one in each line. The query "Which element is i-th by its value?" is coded by the number i. A database is separated from a sequence of queries by the string of three symbols "#".
Output
The output should consist of K lines. In each line there should be an answer to the corresponding query. The answer to the query "i" is an element from the database, which is i-th by its value (in the order from the least up to the greatest element).
Sample Input
571211237121###43325
Sample Output
1211217123题目考查的是排序。
用选择排序、冒泡排序都超时了。
最后学了快速排序,总算过了。
有一个陷阱就是“###”不能用输出的方式打印,而要用输入。
#include<stdio.h>void quicksort(int a[],int p,int r);int partition(int a[],int p,int r);int main(void) { int n,m; int i,j,temp; int a[100001]; char ch[4]; scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d",&a[i]); quicksort(a,1,n); scanf("%s",ch); scanf("%d",&m); while(m--) { scanf("%d",&i); printf("%d\n",a[i]); } return 0;}void quicksort(int a[],int p,int r){ int q; if(p<r) { q=partition(a,p,r); quicksort(a,p,q-1); quicksort(a,q+1,r); }}int partition(int a[],int p,int r){ int x,temp,i,j; x=a[r]; i=p-1; for(j=p;j<r;j++) { if(a[j]<=x) { i++; temp=a[i]; a[i]=a[j]; a[j]=temp; } } temp=a[i+1]; a[i+1]=a[r]; a[r]=temp; return i+1;}
748K
94MS- POJ 一 2371 Questions and answers
- poj 2371 Questions and answers
- poj 2371 Questions and answers
- POJ 2371 Questions and answers
- poj 2371 Questions and answers
- Poj 2371--Questions and answers(QSort)
- POJ 2371 Questions and answers(水~)
- POJ2371:Questions and answers
- POJ_2371 Questions and answers
- hdu Questions and answers
- BNU Questions and answers
- google questions and answers
- google questions and answers
- URAL 1026. Questions and answers
- gSOAP toolkit questions and answers
- .NET Interview Questions And Answers
- ural 1026 Questions and Answers
- Tibco designer questions and answers
- Android_03
- 转android gralloc流程分析for msm8960
- Opengl中光照、材质
- HDU 4602 Partition (整数拆分&找规律&快速幂取模)
- c++ primer习题9.24
- POJ 一 2371 Questions and answers
- 【Oracle】-【心境】【AWR】- 等待事件的基准时间
- oracle11G监听日志所在位置
- android 检测耳机是否插入
- 二次曲面的绘制
- H面试程序(18): 归并排序
- 2013 AWR设计论坛即将在南京和上海召开 详细议程公布
- [java]优先队列
- android的MVC设计模式