Uva10474 - Where is the Marble?
两种解法:
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?的更多相关文章
- UVa10474 Where is the Marble?(排序sort)
		今天开始学STL,这是书上的一道例题,主要是用了sort函数和lower_bound函数,挺容易理解的. lower_bound的作用是查找“大于或等于x的第一个位置”. 需要注意的是,不要忘记alg ... 
- UVA10474 Where is the Marble?【排序】
		参考:https://blog.csdn.net/q547550831/article/details/51326321 #include <iostream> #include < ... 
- 10474 - Where is the Marble?(模拟)
		传送门: UVa10474 - Where is the Marble? Raju and Meena love to play with Marbles. They have got a lot o ... 
- 排序与检索【UVa10474】Where is the Marble?
		Where is the Marble? DescriptionRaju and Meena love to play with Marbles. They have got a lot of ma ... 
- 【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 ... 
- zjuoj 3605 Find the Marble
		http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3605 Find the Marble Time Limit: 2 Seco ... 
- [RxJS] Introduction to RxJS Marble Testing
		Marble testing is an expressive way to test observables by utilizing marble diagrams. This lesson wi ... 
- [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 ... 
- Merkaartor,Marble,QGIS等等
		Merkaartor介绍 Merkaartor是Qt开发开源的OpenStreetMap(下简称osm)数据的编辑器,这里简单列出相关资源.方面基于osm数据的开发. Merkaartor支持osm地 ... 
随机推荐
- android操作文件
			Android中读取/写入文件的方法,与Java中的I/O是一样的,提供了openFileInput()和openFileOutput()方法来读取设备上的文件.但是在默认状态下,文件是不能在不同的程 ... 
- selenium python (六)定位一组对象
			checkbox源码: <html><head><meta http-equiv="content-type" content="text/ ... 
- [转]linux 下使用dump和restore命令
			转自:http://blog.sina.com.cn/s/blog_63eb479a01011sdu.html dump 支持分卷和增量备份(所谓增量备份是指备份最近一次备份以来修改过的文件,也称差异 ... 
- 如何使用Fiddler调试线上JS代码
			大家平时肯定都用过火狐的Firebug或者谷歌的调试工具来调试JS,但遗憾的是我们不能像编辑html,css那样来直接新增或者删除JS代码. 虽然可以通过调试工具的控制台来动态执行JS代码,但有时候却 ... 
- 安装VMware-tools的问题
			今天晚上解决了之前在跟着 Linux学习之CentOS(六)--CentOS下VMware-Tools安装 解压部分,cp 压缩文件时,没有通过命令,直接鼠标操作复制到/home/ranjiewen下 ... 
- AudioManager --- generateAudioSessionId
			AudioManager中的generateAudioSessionId方法介绍: 1.方法声明 pubilc void generateAudioSessionId(); 2.API描述 返回一个不 ... 
- c++ struct 使用
			typedef与结构结合使用 typedef struct tagMyStruct{ int iNum; long lLength;} MyStruct; 这语句实际上完成两个操作: 1) 定义一 ... 
- IE浏览器 json异常
			当使用json数据结构时,如果对象数组最后一个元素后面依然跟一个“,”,在非IE浏览器下运行正常,但是,在IE浏览器上,则会报错. 如果使用for循环遍历对象数组时,由于后面多了一个分割符" ... 
- kali系统安装图文教程
			工具和原料 1.虚拟机:Oracle VM VirtualBox 下载地址:https://www.virtualbox.org/wiki/Downloads 根据你自己的计算机操作系统下载,其中如果 ... 
- Apache Spark 架构
			1.Driver:运行 Application 的 main() 函数并且创建 SparkContext. 2.Client:用户提交作业的客户端. 3.Worker:集群中任何可以运行 Applic ... 
