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. UVa 11538 Chess Queen (排列组合计数)

    题意:给定一个n*m的棋盘,那么问你放两个皇后相互攻击的方式有多少种. 析:皇后攻击,肯定是行,列和对角线,那么我们可以分别来求,行和列其实都差不多,n*A(m, 2) + m*A(n, 2), 这是 ...

  2. 洛谷 P3358 最长k可重区间集问题 【最大费用最大流】

    同 poj 3680 https:www.cnblogs.com/lokiii/p/8413139.html #include<iostream> #include<cstdio&g ...

  3. 牛客网NOIP赛前集训营-提高组(第八场)

    染色 链接:https://ac.nowcoder.com/acm/contest/176/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言10 ...

  4. springboot(十)SpringBoot消息中间件RabbitMQ

    github地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service 1. ...

  5. Jedis线上的一个小坑:Redis有并发访问的数据错乱的问题

    问题现象: 业务数据有错乱,A的一些数据有好几个都是B的数据 这些业务数据在保存在Redis缓存中,怀疑是并发情况下Jedis错乱的问题 原因分析: JedisUtil里面在使用完Jedis 后释放资 ...

  6. DP Codeforces Round #260 (Div. 1) A. Boredom

    题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...

  7. 字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles

    题目传送门 /* 题意:给出一系列名字变化,问最后初始的名字变成了什么 字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置 在每一次更新时都把初始pos加上 ...

  8. 题解报告:CODE[VS] 1082 线段树练习3(区间修改+区间查询)

    题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[a,b]的数的和. 输入描述 Input Description 第一行一个正整数n,接下 ...

  9. Java中的流(5)大数据流的分段读取

    来自文件 或 网络的InputStream数据量可能很大,如果用流的大小申请byte[],可能内存不足报错. 解决方案:分段读取 InputStream的方法int available()返回本次可读 ...

  10. git ---理论知识

    理论基础: 不要高估自己的智商,不要低估Git的能耐. 1.Git记录的 是什么? 记录每一次版本变动的内容 将每个版本独立保存 方便分支管理. 2.git的三棵树---工作区.暂存区域和Git仓库 ...