两种解法:

1.计数排序

//计数排序
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=10001;
int v[maxn], s[maxn]; int main() {
int N, Q;
int Kase=0;
while(cin>>N>>Q && N && Q) {
memset(v, 0, sizeof v);
memset(s, 0, sizeof s);
int m=0;
while(N--) {
int x;
cin>>x;
v[x]++;
m=max(m, x);
}
int rank=v[0];
for(int i=1;i<=m;i++)
{
s[i]=rank;
rank+=v[i];
}
printf("CASE# %d:\n", ++Kase);
while(Q--) {
int q;
cin>>q;
if(v[q])
printf("%d found at %d\n", q, s[q]+1);
else
printf("%d not found\n", q);
}
}
return 0;
}

2.排序,取lower_bound

#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std; int main() {
int N, Q;
int Kase=0;
while(cin>>N>>Q && N && Q) {
vector<int> v;
while(N--) {
int x;
cin>>x;
v.push_back(x);
}
sort(v.begin(), v.end());
printf("CASE# %d:\n", ++Kase);
while(Q--) {
int q;
cin>>q;
vector<int>::iterator it=lower_bound(v.begin(), v.end(), q);
if(it!=v.end() && !(q<*it))
printf("%d found at %d\n", q, it-v.begin()+1);
else
printf("%d not found\n", q);
}
}
return 0;
}

注意找到情况下判断条件

it!=v.end() && !(q<*it))

Uva10474 - Where is the Marble?的更多相关文章

  1. UVa10474 Where is the Marble?(排序sort)

    今天开始学STL,这是书上的一道例题,主要是用了sort函数和lower_bound函数,挺容易理解的. lower_bound的作用是查找“大于或等于x的第一个位置”. 需要注意的是,不要忘记alg ...

  2. UVA10474 Where is the Marble?【排序】

    参考:https://blog.csdn.net/q547550831/article/details/51326321 #include <iostream> #include < ...

  3. 10474 - Where is the Marble?(模拟)

    传送门: UVa10474 - Where is the Marble? Raju and Meena love to play with Marbles. They have got a lot o ...

  4. 排序与检索【UVa10474】Where is the Marble?

    Where is the Marble?  DescriptionRaju and Meena love to play with Marbles. They have got a lot of ma ...

  5. 【UVA - 10474 】Where is the Marble?(排序)

    Where is the Marble? Descriptions: Raju and Meena love to play with Marbles. They have got a lot of ...

  6. zjuoj 3605 Find the Marble

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3605 Find the Marble Time Limit: 2 Seco ...

  7. [RxJS] Introduction to RxJS Marble Testing

    Marble testing is an expressive way to test observables by utilizing marble diagrams. This lesson wi ...

  8. [RxJS] Marble diagrams in ASCII form

    There are many operators available, and in order to understand them we need to have a simple way of ...

  9. Merkaartor,Marble,QGIS等等

    Merkaartor介绍 Merkaartor是Qt开发开源的OpenStreetMap(下简称osm)数据的编辑器,这里简单列出相关资源.方面基于osm数据的开发. Merkaartor支持osm地 ...

随机推荐

  1. notepad++采用正则表达式删除空行

    正则表达式匹配空行:  \s*$

  2. C#实现CAD数据转shape或mdb

    jojojojo2002 原文C#实现CAD数据转shape或mdb 本文所指的CAD数据为不带空间参考和扩展数据的数据.如果CAD带了空间参考或是扩展属性数据的话,就要采用图形和属性分离的方法转CA ...

  3. 8、NFC技术:让Android自动打开网页

    创建封装Uri的NdefRecord  public  NdefRecord  createUri(String  uriString);  public  NdefRecord  cre ...

  4. 有趣的库:pipe(类似linux | 管道)库

    pipe并不是Python内置的库,如果你安装了easy_install,直接可以安装它,否则你需要自己下载它:http://pypi.python.org/pypi/pipe 之所以要介绍这个库,是 ...

  5. Flex的基础用法【转】

    //获得屏幕的分辨率 var x:Number=Capabilities.screenResolutionX; var y:Number=Capabilities.screenResolutionY; ...

  6. js 多物体运动

    <!doctype html> <html> <head> <meta charset = "utf-8"> <title&g ...

  7. Apache Spark 架构

    1.Driver:运行 Application 的 main() 函数并且创建 SparkContext. 2.Client:用户提交作业的客户端. 3.Worker:集群中任何可以运行 Applic ...

  8. Jtemplates 基本语法

    jTemplates是一个基于JQuery的模板引擎插件,功能强大,有了他你就再不用为使用JS绑定数据集时发愁了. 首先送上jTtemplates的官网地址:http://jtemplates.tpy ...

  9. 解决getOutputStream() has already been called for this response

    http://qify.iteye.com/blog/747842 —————————————————————————————————————————————————— getOutputStream ...

  10. Dagger2 scope

    1. 一个没有scope的component是不能依赖于另外一个有scope的component 2.@Singleton不是真正意义的单例,比如下面 @Singleton @Component cl ...