uva10474 (二分查找)
2016-03-22 20:17
239 查看
uva10474 (二分查找)
10474Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginning, Raju would place the marbles one after another in ascending order of the numbers written on them. Then Meena would ask Raju to find the first marble with a certain number. She would count 1…2…3. Raju gets one point for correct answer, and Meena gets the point if Raju fails. After some fixed number of trials the game ends and the player with maximum points wins. Today it’s your chance to play as Raju. Being the smart kid, you’d be taking the favor of a computer. But don’t underestimate Meena, she had written a program to keep track how much time you’re taking to give all the answers. So now you have to write a program, which will help you in your role as Raju.
input
There can be multiple test cases. Total no of test cases is less than 65. Each test case consists begins with 2 integers: N the number of marbles and Q the number of 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. Following Q lines will have Q queries. Be assured, none of the input numbers are greater than 10000 and none of them are negative.
Input is terminated by a test case where N = 0 and Q = 0.
output
For each test case output the serial number of the case.
For each of the queries, print one line of output. The format of this line will depend upon whether or not the query number is written upon any of the marbles. The two different 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 output for sample input for details.
sample intput
4 1
2
3
5
1
5
5 2
1
3
3
3
1
2
3
0 0
sample output
CASE# 1:
5 found at 4
CASE# 2:
2 not found
3 found at 3
个人感想
• 其实这算是uva中很简单的一道题,只是用到了排序和二分查找。 • 题目意思就是给出两个数m和n下面输入m个数,再依次输入n个数,查找n个数在前面的m个数中是第几大。 例如:sample 1 排序后: 1 2 3 5 , 5为第4大。 sample 2 排序后: 1 1 3 3 3 , 3为第三大。(并不是第二大,因为本题1不重叠)
代码
#include<iostream> #include<algorithm> #define maxn 10010 using namespace std; int num[maxn]; int num1[maxn]; int search(int begin,int end,int a) { int m; while(begin<end) { m=begin+(end-begin)/2; if(num[m]>=a)end=m; else begin=m+1; } return num[begin]==a? begin:-1; } int main() { int n1,n2; int A=0; while(cin>>n1>>n2) { A++; if(n1==0&&n2==0)break; for(int i=0;i<n1;i++) cin>>num[i]; for(int i=0;i<n2;i++) cin>>num1[i]; sort(num,num+n1); cout<<"CASE# "<<A<<":"<<endl; for(int i=0;i<n2;i++) { int m=search(0,n1-1,num1[i]); if(-1==m)cout<<num1[i]<<" not found"<<endl; else cout<<num1[i]<<" found at "<<m+1<<endl; } } return 0; }
#
• 第一次写博客,感觉markdown编译器很好用呢(—3—) 不过还是有很多不太会的地方,版面看起来并不是特别好看,我会努力哒~• 打算坚持把自己做过的acm题加上自己的感想发到这个博客中,希望自己更加进步,也希望能够帮助更多弄acm的好朋友~
• 除了acm之外,这个博客也会记载我从开始接触IT行业的很多好东西~也是督促着自己的学习和进步~干巴爹呦~
相关文章推荐
- 【iOS】原生微信登录和分享
- windows server 2008解决盘符C盘变D盘的安装方法
- 文章标题
- 使用 satis 搭建一个私有的 Composer 包仓库
- 随机数问题
- Jedis源码解析(Transation事务)
- 醉最基础的汉诺塔
- Java基础算法集50题
- 硬盘安装ubuntu注意事项
- elecworks 报表----按线类型的电线清单
- java中的坐标转换及测距
- Android_动态权限管理的解决方案
- C#中委托和Lambda表达式
- POJ 3080 Blue Jeans
- JavaScript 运行机制详解:再谈Event Loop
- 如何控制开放HTTPS服务的weblogic服务器
- 个人理解---扩展欧几里德定理
- 输出星号图
- BZOJ_P3757 苹果树(树上莫队+LCA)
- 第四周项目五-用递归方法求解(输出Fibnacci序列的第20个数)