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. 【191】◀▶ Powershell 命令集 Cmdlets

     Powershell 命令集 cmdlets cmdlets是Powershell的内部命令,cmdlet的类型名为System.Management.Automation.CmdletInfo,包 ...

  2. asp.net Identity2 角色(Role)的使用(三)用户管理,用户控制器和视图

    修改用户控制器AccountController,增加角色管理器. public class AccountController : Controller { public AccountContro ...

  3. bzoj 1661: [Usaco2006 Nov]Big Square 巨大正方形【枚举】

    每句两个顶点确定正方形,取max即可 #include<iostream> #include<cstdio> using namespace std; int n,x,y,s, ...

  4. Tensor Operation

    Main operation categories that encompass the operations of tensors. Reshaping operations Element-wis ...

  5. SpringMVC异步调用,Callable和DeferredResult的使用

    Callable和DeferredResult都是springMVC里面的异步调用,Callable主要用来处理一些简单的逻辑,DeferredResult主要用于处理一些复杂逻辑 1.Callabl ...

  6. 【TIDB】2、TIDB进阶

    0.TIDB优势 1.和MySql相比,具备OLAP能力.省去了很多数据仓库搭建成本和学习成本.这在业务层是非常受欢迎的.可以在其他分库分表业务中,通过 syncer 同步,进行合并,然后进行统计分析 ...

  7. input type="file"文件上传到后台读取

    html页面(表单采用bootStrap) js部分: //更换头像时把上传的图片post方式到控制器 <script type="text/javascript"> ...

  8. 程序3-3 Palindromes

    刘大婶说这个比较难,哈哈,我感觉自己写的代码还是比较简单的. #include<stdio.h> #include<string.h> #include<math.h&g ...

  9. jenkins一次构建两次触发job问题

    具体内容详见: https://issues.jenkins-ci.org/browse/JENKINS-21464?focusedCommentId=250183&page=com.atla ...

  10. Sequence POJ - 2442

    Sequence POJ - 2442 口胡一个结论:就是前i行产生的最小的n个和,一定可以在"前i-1行产生的最小n个和,每一个加上这一行的任意一个数,产生的n2个数"中找到.( ...