UVa 10474 Where is the Marble?
来源:互联网 发布:wf入门到精通源码 编辑:程序博客网 时间:2024/06/05 16:58
典型的排序检索问题,需要注意的是返回排好序后要找的第一次出现的位置(序号是从1开始数的)。
开始不知道bsearch()函数,所以自己写了个二分查找,用来用bsearch也同样A过去了。
貌似自己写的比库函数还快0.001秒,嘎嘎!
Where is the Marble?
Raju and Meenalove to play with Marbles. They have got a lot of marbles with numbers writtenon them. At the beginning, Raju would place the marbles(弹子) one afteranother in ascending order(按升序排列)of the numberswritten on them. Then Meena would ask Raju to find the first marble with acertain number. She would count 1...2...3. Raju gets one point for correctanswer, and Meena gets the point if Raju fails. After some fixed number oftrials the game ends and the player with maximum points wins. Today it's yourchance to play as Raju. Being the smart kid, you'd be taking the favor of acomputer. But don't underestimate(低估) Meena, she hadwritten a program to keep track how much time you're taking to give all theanswers. So now you have to write a program, which will help you in your roleas Raju.
Input
There can bemultiple test cases. Total no of test cases is less than 65. Each test case consistsbegins with 2 integers: N the numberof marbles and Q the numberof queries(询问) Mina would make. The next N lines would contain the numbers written on the N marbles. These marble numbers will not come in any particular order.FollowingQ lines willhave Q queries. Beassured, none of the input numbers are greater than 10000 and none of them arenegative.
Input isterminated by a test case where N = 0 and Q = 0.
Output
For each test caseoutput the serial number of the case.
For each of thequeries, print one line of output. The format of this line will depend uponwhether or not the query number is written upon any of the marbles. The twodifferent formats are described below:
- `x found at y', if the first marble with number x was found at position y. Positions are numbered 1, 2,..., N.
- `x not found', if the marble with number x is not present.
Look at the outputfor sample input for details.
SampleInput
4 1
2
3
5
1
5
5 2
1
3
3
3
1
2
3
0 0
SampleOutput
CASE# 1:
5 found at 4
CASE# 2:
2 not found
3 found at 3
Problem-setter:Monirul Hasan Tomal, Southeast University
AC代码:
//#define LOCAL#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 10000 + 5;int a[maxn], b[maxn];bool Find(int a[], int n, int num, int &pos);int cmp ( const void *a , const void *b );int main(void){#ifdef LOCALfreopen("10474in.txt", "r", stdin);#endifint n, q, i, kase = 0;while(scanf("%d %d", &n, &q) == 2 && (n + q)){printf("CASE# %d:\n", ++kase);for(i = 0; i < n; ++i)scanf("%d", &a[i]);for(i = 0; i < q; ++i)scanf("%d", &b[i]);qsort(a, n, sizeof(int),cmp);int pos;for(i = 0; i < q; ++i){if(Find(a, n, b[i], pos)){int j = pos;while(a[j] == a[pos] && j >= 0)//要找第一个数--j;printf("%d found at %d\n", b[i], j + 2);}elseprintf("%d not found\n", b[i]);}}return 0;}//二分查找bool Find(int a[], int n, int num, int &pos){int left = 0, right = n - 1;int mid = (left + right) / 2;while(left <= right){if(a[mid] == num){pos = mid;return true;}if(a[mid] < num){left = mid + 1;mid = (left + right) / 2;continue;}if(a[mid] > num){right = mid - 1;mid = (left + right) / 2;}}return false;}int cmp ( const void *a , const void *b ){return *(int *)a - *(int *)b;}
下面是百度百科中bsaerch的用法:
用bsearch的AC代码:
//#define LOCAL#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 10000 + 5;int a[maxn], b[maxn];int cmp ( const void *a , const void *b );int main(void){#ifdef LOCALfreopen("10474in.txt", "r", stdin);#endifint n, q, i, kase = 0;while(scanf("%d %d", &n, &q) == 2 && (n + q)){printf("CASE# %d:\n", ++kase);for(i = 0; i < n; ++i)scanf("%d", &a[i]);for(i = 0; i < q; ++i)scanf("%d", &b[i]);qsort(a, n, sizeof(int),cmp);int pos, *p;for(i = 0; i < q; ++i){p = (int *)bsearch(&b[i], a, n, sizeof(int), cmp);if(p != NULL){pos = p - a;int j = pos;while(a[j] == a[pos] && j >= 0)//要找第一个数--j;printf("%d found at %d\n", b[i], j + 2);}elseprintf("%d not found\n", b[i]);}}return 0;}int cmp ( const void *a , const void *b ){return *(int *)a - *(int *)b;}
- UVa 10474 Where is the Marble?
- Uva 10474 Where is the Marble?
- uva 10474 Where is the Marble?
- UVA 10474 Where is the Marble?
- UVA 10474 - Where is the Marble
- uva-10474 - Where is the Marble?
- uva 10474 - Where is the Marble?
- UVA 10474 - Where is the Marble?
- UVa 10474 - Where is the Marble?
- UVa 10474 - Where is the Marble
- UVa 10474 - Where is the Marble?
- UVA 10474 Where is the Marble?
- UVA 10474 - Where is the Marble
- UVa - 10474 - Where is the Marble?
- uva 10474 Where is the Marble?
- uva 10474 Where is the Marble?
- uva - 10474 - Where is the Marble?
- uva 10474 - Where is the Marble
- 浏览器[IE] Ajax Struts Json返回json字符串,解析不了 提示保存或下载 解决方式
- hdu-4453-Looploop-splay
- linux下IPTABLES配置详解
- Makefile规则(四):函数
- 高级PHP应用程序漏洞审核技术
- UVa 10474 Where is the Marble?
- Ubuntu打开终端的方法
- 股市24基础指标、ma均线详解、双价托
- [乐意黎原创] eclipse Kepler Selected SVN connector library is not available or cannot be loaded
- 配电板是什么?
- 文件上传利器SWFUpload入门简易教程
- 黑马程序员_java基础--多线程
- 解决Android中,禁止ScrollView内的控件改变之后自动滚动
- IOS管理文件和目录