UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了那句话:基础不牢,地动山摇. 记录一下自己BS的常见错误: 1.需要传入的参数是,搜索的区间[l,r]和搜索的目标值t; 2.一般被搜索的对象以全局变量的身份出现,故不需要传参进去; 3.退出循环的条件是l < r 注意这里可没有等号; 4.若t在mid左边或等于mid,要把右坐标r移动到m的位置,…
题目给出一系列数字,然后问哪个数字是从小到大排在第几的,重复出现算第一个. 数据范围为10000,不大,完全可以暴力,sort不会超时. 但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力被hack了.之后就学会了计数排序了. 这题也用计数排序做,挺快的,代码也不长. 代码: #include <cstdio> #include <cstring> const int maxn = 10001; int num[maxn], s[maxn]; int main() {…
题意:给出一列数,先排序,再查找学习了sort函数,lower_bound函数sort:可以给任意对象排序(包括自己定义的)(前提是定义好了‘<’运算符)lower_bound:查找大于或者等于x的第一个位置. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; int a[maxn]; int main() {…
我非常奇怪为什么要把它归类到回溯上,明明就是简单排序,查找就OK了.wa了两次,我还非常不解的怀疑了为什么会 wa,原来是我居然把要找的数字也排序了,当时仅仅是想着能快一点查找.所以就给他排序了,没考虑到要按给的顺序输 出答案,这次真是二了,.,看别人题解实用打表做的,那个应该是正确解法.我的耗时980ms,预计数据再大一些就 要TLE了 贴代码: #include<stdio.h> #include<string.h> #include<stdlib.h> int c…
https://vjudge.net/problem/UVA-10474 https://blog.csdn.net/xiyaozhe/article/details/81081344 简单用法 sort(start,end) 默认是升序 实现降序: #include <functional> int a[10]={5,6,7,8,9,0,1,2,3,4}; vector <int> v(a, a+10); sort(v.begin(), v.end(),less<int&g…
今天开始学STL,这是书上的一道例题,主要是用了sort函数和lower_bound函数,挺容易理解的. lower_bound的作用是查找“大于或等于x的第一个位置”. 需要注意的是,不要忘记algorithm头文件. 使用STL真的方便了不少啊! Where is the Marble?,UVa 10474 #include<cstdio> #include<algorithm> using namespace std; ; int main() { ; &&n…
Where is the Marble? Descriptions: Raju 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 th…
 Raju 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 f…
参考:ACM紫书 第五章 P108 [排序与检索] 下面的代码中有些 提示性输出,想Ac 需删除提示性输出语句,读者自行修改. #include <cstdio> #include <algorithm> using namespace std; const int maxn = 10000; int main(void) { int n,q,numarr[maxn],i,x,p,Case=0; while(scanf("%d%d",&n,&q)…
现有N个大理石,每个大理石上写了一个非负整数.首先把各数从小到大排序,然后回 答Q个问题.每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上 写着x.排序后的大理石从左到右编号为1-N.(在样例中,为了节约篇幅,所有大理石上 的数合并到一行,所有问题也合并到一行.) 样例输入: 4 1 2 3 5 1 5  5 2 1 3 3 3 1 2 3 样例输出: CASE #1: 5 found at 4 CASE #2: 2 not found 3 found at 3 [分析]…