算法竞赛入门经典5.2 STL初步
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初步的更多相关文章
- 随机生成数,摘自算法竞赛入门经典P120-P123测试STL。
//#include<bits/stdc++.h> #include<cstring> #include<iostream> #include<cstdio& ...
- 【C/C++】例题5-4 反片语/算法竞赛入门经典/C++与STL入门/映射:map
本题是映射:map的例题. map:键值对. [题目] 输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一个单词. 在判断是否满足条件时,字母不分大小写,但在输出 ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- [刷题]算法竞赛入门经典 3-12/UVa11809
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa11809:Floating-Point Numbers 代码: //UVa11 ...
- [刷题]算法竞赛入门经典 3-10/UVa1587 3-11/UVa1588
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-10/UVa1587:Box 代码: //UVa1587 - Box #include&l ...
- [刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 都是<算法竞赛入门经典(第二版)>的题目,标题上没写(第二版) 题目:算法竞赛入门经典 3-7/UVa13 ...
- [刷题]算法竞赛入门经典 3-4/UVa455 3-5/UVa227 3-6/UVa232
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa455:Periodic Strings 代码: //UVa455 #inclu ...
- [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO(我也是在网上找到的pdf,但不记得是从哪里搜刮到的了,就重新上传了一遍) PS:第一次写博客分享我的代码,不知道我对c ...
随机推荐
- “Live Desktop” privacy statement
“Live Desktop” pays attention to your privacy protection. Sometimes we need some information to prov ...
- hdu4608 I-number
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4608 题意:给定一个数X,注意X是个大数,X的长度不超过1e5. 让你求出一个Y,满足三个条件,Y&g ...
- 一步一步学Vue(六)
本篇继续介绍vue-router,我们需要要完成这样个demo:<分页显示文章列表>:这里我们以博客园首页列表为例简化处理: 按照上图框选所示,简单分为蓝色部分文章组件(ArticleIt ...
- bzoj 2178 圆的面积并【simpson积分】
直接套simpson,f可以直接把圆排序后扫一遍所有圆,这样维护一个区间就可以避免空段. 然而一定要去掉被其他圆完全覆盖的圆,否则会TLE #include<iostream> #incl ...
- thinkphp5.0常遇到的错误
call a member xxxx on null 1.一般是没有继承controller: 2.对象和数组使用错误.
- JSON WEB TOKEN 从原理到实战
阅读本文大概需要 4.2 分钟. 作者:王廷骏原文:https://juejin.im/post/5ce272c1e51d45109b01b0f8 1. JSON WEB TOKEN 1.1 什么是J ...
- springMVC RedirectAttributes
@Controller public class TestController { @RequestMapping("/redirectDemo") public String r ...
- [POI2007]山峰和山谷Grz
Description FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷.为了能够让他对他的旅程有一个安排,他想知道山峰和山谷的数量.给定一个地图,为FGD想要旅行的区域,地图被分为\(n\ ...
- ACM_查找ACM(加强版)
查找ACM(加强版) Time Limit: 2000/1000ms (Java/Others) Problem Description: 作为一个acmer,应该具备团队合作能力和分析问题能力.给你 ...
- 使用HttpClient携带pfx证书调用HTTPS协议的WebService
调用第三方服务时,厂商提供了一个WSDL文件.调用的地址和一个后缀为pfx的证书文件,通过SOUPUI记载证书是可以正常调用WebService服务,那么如何将该服务转换为代码呢? 咨询了厂商的支持, ...