1.  排序和检索,学会使用sort排序,以及low_bound函数

Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginning, Raju would place the marbles one after another in ascending order of the numbers written on them. Then Meena would ask Raju to find the first marble with a certain number. She would count 1...2...3. Raju gets one point for correct answer, and Meena gets the point if Raju fails. After some fixed number of trials the game ends and the player with maximum points wins. Today it’s your chance to play as Raju. Being the smart kid, you’d be taking the favor of a computer. But don’t underestimate Meena, she had written a program to keep track how much time you’re taking to give all the answers. So now you have to write a program, which will help you in your role as Raju.

Input

There can be multiple test cases. Total no of test cases is less than 65. Each test case consists begins with 2 integers: N the number of marbles and Q the number of queries Mina would make. The next N lines would contain the numbers written on the N marbles. These marble numbers will not come in any particular order. Following Q lines will have Q queries. Be assured, none of the input numbers are greater than 10000 and none of them are negative. Input is terminated by a test case where N = 0 and Q = 0.

Output

For each test case output the serial number of the case. For each of the queries, print one line of output. The format of this line will depend upon whether or not the query number is written upon any of the marbles. The two different formats are described below: • ‘x found at y’, if the first marble with number x was found at position y. Positions are numbered 1, 2, . . . , N. • ‘x not found’, if the marble with number x is not present. Look at the output for sample input for details.

Sample Input

4 1 2 3 5 1 5 5 2 1 3 3 3 1 2 3 0 0

Sample Output

CASE# 1: 5 found at 4

CASE# 2: 2 not found 3 found at 3

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
using namespace std; const int maxn = ;
int N,Q;
int temp;
int casee=;
int a[maxn]; int main()
{
while(cin>>N>>Q)
{
if(N==&&Q==)
break;
else
{
cout<<"CASE# "<<casee++<<":"<<endl;
for(int i=;i<N;i++)
cin>>a[i];
sort(a,a+N);//排序,可以自己手写cmp函数作为参数,一般是用在结构体里面排序。
while(Q--)
{
cin>>temp;
int flag=lower_bound(a,a+N,temp)-a;//在已经排序的数组里面寻找x
if(a[flag]==temp)
cout<<temp<<" found at "<<flag+<<endl;
else
cout<<temp<<" not found"<<endl;
}
}
}
return ;
}

2.  vector的用法。

常用函数,push_back() 尾部添加元素     pop_back() 删除最后一个元素  size() 返回长度  resize(b) 改变大小,保留下标0—b之间的元素

reverse(vec.begin(),vec.end());将元素翻转,即逆序排列!

vector元素遍历利用迭代器  for(vector<int>::iterator it = v.begin();it!=v.end();it++)

               { cout<<*it<<" "; }

UVA-101

题目链接https://vjudge.net/problem/UVA-101

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
using namespace std; const int maxn=;
vector<int>v[maxn];
int n;
string s1,s2;
int a,b; void findd(int a,int &p,int &h)
{
for(p=;p<n;p++)
{
for(h=;h<v[p].size();h++)
{
if(v[p][h]==a)
return;
}
}
} void fun1(int p,int h)//归位
{
for(int i=h+;i<v[p].size();i++)
{
int j=v[p][i];
v[j].push_back(j);
}
v[p].resize(h+);
} void fun2(int p1,int h,int p2)
{
for(int i=h;i<v[p1].size();i++)
v[p2].push_back(v[p1][i]);
v[p1].resize(h);
} int main()
{
cin>>n;
for(int i=;i<n;i++)
v[i].push_back(i);
while(cin>>s1)
{
if(s1=="quit")
break;
cin>>a>>s2>>b;
int pa,ha,pb,hb;
findd(a,pa,ha);
findd(b,pb,hb);
if(pa==pb)
continue;
//cout<<pa<<" "<<ha<<" "<<pb<<" "<<hb<<endl;
if(s2=="onto")
fun1(pb,hb);
if(s1=="move")
fun1(pa,ha);
fun2(pa,ha,pb);
}
for(int i=;i<n;i++)
{
cout<<i<<":";
for(int j=;j<v[i].size();j++)
cout<<" "<<v[i][j];
cout<<endl;
}
return ;
}

算法竞赛入门经典5.2 STL初步的更多相关文章

  1. 随机生成数,摘自算法竞赛入门经典P120-P123测试STL。

    //#include<bits/stdc++.h> #include<cstring> #include<iostream> #include<cstdio& ...

  2. 【C/C++】例题5-4 反片语/算法竞赛入门经典/C++与STL入门/映射:map

    本题是映射:map的例题. map:键值对. [题目] 输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一个单词. 在判断是否满足条件时,字母不分大小写,但在输出 ...

  3. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  4. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  5. [刷题]算法竞赛入门经典 3-12/UVa11809

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa11809:Floating-Point Numbers 代码: //UVa11 ...

  6. [刷题]算法竞赛入门经典 3-10/UVa1587 3-11/UVa1588

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-10/UVa1587:Box 代码: //UVa1587 - Box #include&l ...

  7. [刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 都是<算法竞赛入门经典(第二版)>的题目,标题上没写(第二版) 题目:算法竞赛入门经典 3-7/UVa13 ...

  8. [刷题]算法竞赛入门经典 3-4/UVa455 3-5/UVa227 3-6/UVa232

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa455:Periodic Strings 代码: //UVa455 #inclu ...

  9. [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO(我也是在网上找到的pdf,但不记得是从哪里搜刮到的了,就重新上传了一遍) PS:第一次写博客分享我的代码,不知道我对c ...

随机推荐

  1. 【188】HTML + CSS + JS 学习网站

    RGB 取色器      HTML 参考手册      CSS 参考手册      HTML 在线测试工具 上面源码(博客园 - HTML): <style><!-- p.bg_gr ...

  2. 关于ArcGis for javascrept查询ArcGis server图层信息的方式

    方式一: queryTask方式: 该方式用于单个图层的条件查询(不能跨图层查询) 1. 创建query对象 query = new esri.tasks.Query(); 2. 给query对象设置 ...

  3. CodeForces 721B Passwords (水题)

    题意:给定 n 个密码,你要按长度不递减的顺序进行尝试,问你最多和最少试多少次可能找出密码,每尝试 k 次错误的,就要等5秒. 析:我们只要把长度全都统计下来,然后从1开始去找目标长度,最少的就是正好 ...

  4. Eclipse打开Android项目报Parsing Data for android-21 failed错误的解决办法(转载)

    转载:http://segmentfault.com/blog/hongliang/1190000000739285 今天手贱,用android命令打开SDK Manager下载了最新的Android ...

  5. Activiti6.0教程 28张表解析 (三)

    使用Activit的朋友都知道Activiti对应的有28张表,今天我们就来说下Activit中28张表对应的含义是什么? 如何创建表? 在Activiti中创建表有三种方式,我们依次来看下: 一.通 ...

  6. Java键盘输入的方法

    转载:http://blog.csdn.net/u012249177/article/details/49586383 java输入的方法: import java.io.BufferedReader ...

  7. _bzoj1015 [JSOI2008]星球大战starwar【并查集】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1015 倒过来做就ok了. #include <cstdio> #include ...

  8. 题解报告:hdu 1039 Easier Done Than Said?

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1039 Problem Description Password security is a trick ...

  9. CentOS 6.5使用:[3]使用xftp传递文件

    先检查CentOS系统是否安装了FTP服务 [root@centos ~]# rpm -qa | grep vsftpd 如果有内容输出,那么恭喜你,你的系统已经安装了ftp服务   如果没有那么按照 ...

  10. AFNetworking2.5使用-转

    来自:http://blog.csdn.net/daiyelang/article/details/38434023 官网下载2.5版本:http://afnetworking.com/ 此文章是基于 ...