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地 ...
随机推荐
- bootstrap-datepicker 插件修改为默认中文
bootstrap-datepicker 是一个非常优秀的时间选择插件,默认是英文显示日期的,通过下面几个小修改让其支持默认中文 1.首先将 bootstrap-datepicker.js 另存为 u ...
- ArcEngine9.3报错Create output feature class failed
ArcEngine9.3执行IFeatureDataConverter.ConvertFeatureClass Method出错如下错误信息: Create output feature class ...
- mvc中@RenderSection()研究
一.@RenderSection定义 HelperResult RenderSection(string name) 但是当如果使用了_Layout.cshtml做母版页的页没有实现Section的话 ...
- 40个最好的Tumblr主题
如果安装了一款较好的Tumblr主题,你的Tumblr空间将焕然一新.然而找到一款合适的主题并不是一件容易的事,这正是本文中我整理那么多优质的Tumblr模板作为灵感的原因.其中有一些免费的Tumbl ...
- Best Practices for Speeding Up Your Web Site
The Exceptional Performance team has identified a number of best practices for making web pages fast ...
- 分析fork后多进程对文件的共享
fork函数是创建一个新的进程作为原进程的子进程,创建的子进程和父进程存在很多的相似性,首先父子进程的虚拟存储空间的用户空间是相同的,是将父进程的拷贝给子进程.同时父子进程对文件的操作是共享方式.因为 ...
- HDU5873:Football Games
题目链接: Football Games 分析: 先将分数排序,然后 设当前队编号为p,设个指针为p+1,然后p>1,每次p-=2,指针右移一位p==1,指针指向的队-=1p==0,从指针开始到 ...
- mysql cluster 名词概念解读
Node Group [number_of_node_groups] = number_of_data_nodes / NoOfReplicas Partition When using ndbd, ...
- Hadoop HDFS概念学习系列之分布式文件管理系统(二十五)
数据量越来越多,在一个操作系统管辖的范围存在不了,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,因此迫切需要一种系统来 管理多台机器上的文件,这就是分布式文件管理系统. 是一种允许文件 ...
- 如何判断ios设备是否是高清屏幕
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2f) { CGRect winRect = [[UIScreen m ...