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地 ...
随机推荐
- PhoneGap与Jquery Mobile结合开发android应用配置
由于工作需要,用到phonegap与jquery moblie搭配,开发android应用程序. 这些技术自己之前也都没接触过,可以说是压根没听说过,真是感慨,在开发领域,技术日新月异,知识真是永远学 ...
- PHP 截取字符串专题
1. 截取GB2312中文字符串 < ?php//截取中文字符串function mysubstr($str, $start, $len) { $tmpstr = "" ...
- 浅谈Android五大布局——LinearLayout、FrameLayout、AbsoulteLayout、RelativeLayout和TableLayout
Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建 筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.Android的五大布局分别是LinearLa ...
- 【转】linux dumpe2fs命令
转自:http://www.shellcn.net/linux_command/linux_dumpe2fs.html 命令名称 dumpe2fs - 显示ext2/ext3/ext4文件系统信息. ...
- “iOS 推送通知”详解:从创建到设置到运行
这是一篇编译的文章,内容均出自Parse.com的iOS开发教程,同时作者还提供了视频讲解.本文将带领开发者一步一步向着iOS推送通知的深处探寻,掌握如何配置iOS推送通知的奥义. 介绍一点点背景资料 ...
- uoj #58. 【WC2013】糖果公园(树上莫队算法+修改操作)
[题目链接] http://uoj.ac/problem/58 [题意] 有一棵树,结点有自己的颜色,若干询问:u,v路径上的获益,并提供修改颜色的操作. 其中获益定义为Vc*W1+Vc*W2+…+V ...
- FlatBuffers要点
FlatBuffers发布出来一周多,周末便抽时间先研究下它的使用方法.Flatbuffers的idl的语法主要参考[http://google.github.io/flatbuffers/md__s ...
- Java多线程学习总结--线程概述及创建线程的方式(1)
在Java开发中,多线程是很常用的,用得好的话,可以提高程序的性能. 首先先来看一下线程和进程的区别: 1,一个应用程序就是一个进程,一个进程中有一个或多个线程.一个进程至少要有一个主线程.线程可以看 ...
- ninject学习笔记一:IOC的实现
这篇文章主要介绍ninject在IOC方面的实现,至于IOC的含义,网络资源很丰富,我这儿就不再赘述了.官方的文档其实挺好的,只是本人英语很烂,看起来比较费劲,下面这些东西是看官方的代码推敲的,我觉得 ...
- 利用 Oracle EM 企业管理器 进行oracle SQL的优化(自动生成索引)
利用 Oracle EM 企业管理器 进行oracle SQL的优化(自动生成索引) ##应用情景 项目中有大量的SQL,尤其是涉及到统计报表时,表关联比较多,当初开发建表时也没搞好索引关联的,上线后 ...