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地 ...
随机推荐
- apple-touch-icon,shortcut icon和icon的区别
apple-touch-icon 可以了解到这是一个类似网站favicon的图标文件,用来在iphone和ipod上创建快捷键时使用. 这个文件应当是png格式,57x57像素大小,放在网站根目录之下 ...
- 嵌入式 Linux下curl库API简单介绍
1:CURLcode curl_global_init(long flags); 这个函数全局需要调用一次(多次调用也可以,不过没有必要), 所以这也是把Curlplus设计成单体类的原因,curl_ ...
- erase() 返回的是删除此元素之后的下一个元素的迭代器 .xml
pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...
- 基本的SQL Server 语句,包含 增、删、改、查 程序员必会
这是我以前学习时, 整理的一套基础SQL Server增.删.改.查 等语句 ,初学者可以从上往下学完. 也方便自己忘记时翻看! create database SQLschool go --批 go ...
- hadoop是什么?
在如今这个信息高速发展的今天,hadoop也越来越火了,那么到底是什么原因让hadoop如此的火,接下来新霸哥将详细的为了介绍,并让你快速的任何hadoop是什么? hadoop思想起源:Google ...
- Python 学习笔记(五)杂项
1. Assert assert len(unique_characters) <= 10, 'Too many letters' #…等价于: if len(unique_characters ...
- [转] Web前端优化之 Javascript篇
原文链接: http://lunax.info/archives/3099.html Web 前端优化最佳实践之 JavaScript 篇,这部分有 6 条规则,和 CSS 篇 重复的有几条.前端优化 ...
- Typecho集成ueditor笔记
前言:萝卜青菜各有所爱,因为个人需求所以需要在博客中集成ueditor,并非是我不喜欢md语法 其实本篇的笔记的书写最早也是在本地的md编辑器上完成的 1. 首先下载ueditor编辑器,然后重命名文 ...
- WS之cxf与spring整合2
在action中加入webservice
- CDH4.1基于Quorum-based Journaling的NameNode HA
几个星期前, Cloudera发布了CDH 4.1最新的更新版本,这是第一个真正意义上的独立高可用性HDFS NameNode的hadoop版本,不依赖于特殊的硬件或外部软件.这篇文章从开发者的角度来 ...