POJ_2371 Questions and answers
2013-05-15 19:37
387 查看
[align=center]Questions and answers[/align]
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.
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
Sample Output
题意:给出一些数,再从这些书中分别取出第K大的数。比如 {4,4,5,6,6} 取 K ={1, 2} 第一次后{4,5,6,6} 第二次后 {4,6,6}
思路:计数排序加查找,因为数据范围为1-5000,可以用基数排序,具体代码如下:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9465 | Accepted: 5027 |
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.
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
5 7 121 123 7 121 ### 4 3 3 2 5
Sample Output
121 121 7 123
题意:给出一些数,再从这些书中分别取出第K大的数。比如 {4,4,5,6,6} 取 K ={1, 2} 第一次后{4,5,6,6} 第二次后 {4,6,6}
思路:计数排序加查找,因为数据范围为1-5000,可以用基数排序,具体代码如下:
#include<iostream> #include<string> using namespace std; int main() { int n,k,tmp,i,z,sum; int a[5005]; string t; for(i = 0; i < 5005; i++) a[i] = 0; cin >> n; for(i = 1; i <= n; i++) { cin >> tmp; a[tmp]++; } cin >> t; cin >> k; for(i = 1; i <= k; i++) { cin >> tmp; sum = a[1]; z = 1; while(sum < tmp) { z++; sum += a[z]; } cout << z << endl; } return 0; }
相关文章推荐
- poj 2371 Questions and answers
- POJ 一 2371 Questions and answers
- POJ 2371 Questions and answers
- POJ 2371 Questions and answers(水~)
- (使用STL自带的排序函数7.3.4)POJ 2371 Questions and answers(sort()函数的使用)
- POJ 2371 Questions and answers G++
- poj 2371 Questions and answers
- Poj 2371--Questions and answers(QSort)
- poj 2371 Questions and answers
- poj 2371 Questions and answers
- Pku acm 2371 Questions and answers 排序算法解题报告(三)----二叉查找数(BST)排序
- SQL Server Interview Questions and Answers-Part I
- SQL Server Interview Questions and Answers – Part 2
- 【译】Core Java Questions and Answers【1-33】
- C# Interview Questions and Answers
- .NET Interview Questions And Answers
- SQL Server Interview Questions and Answers – Part 3
- Struts2 Interview Questions and Answers
- C++ Interview Questions and Answers - Part 01
- questions I have encountered and answers