算法竞赛入门经典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 ...
随机推荐
- angular中transclude的理解
今天被这个transclude搞糊涂了,弄了半天,才知道原来使用起来很简单.很烦恼为社么书中的对于这个的介绍这么晦涩难懂.直到看到了这篇文章,才让我弄清楚了. 一.transclude介绍 trans ...
- bzoj 1834: [ZJOI2010]network 网络扩容【最大流+最小费用最大流】
第一问直接跑最大流即可.建图的时候按照费用流建,费用为0. 对于第二问,在第一问dinic剩下的残量网络上建图,对原图的每条边(i,j),建(i,j,inf,cij),表示可以用c的花费增广这条路.然 ...
- 洛谷P2082 区间覆盖(加强版)(珂朵莉树)
传送门 虽然是黄题而且还是一波离散就能解决的东西 然而珂朵莉树还是很好用 相当于一开始区间全为0,然后每一次区间赋值,问最后总权值 珂朵莉树搞一搞就好了 //minamoto #include< ...
- Access operations
Access operations Accessing elements inside tensors Suppose we have the following tensors: > t = ...
- Java Genericity
四. Java Genericity 1. Genericity 泛型 泛型 <T> 1. 泛型就是参数化类型 2. 作用:安全,方便 3. 适用于对多种数据类型执行相同功能的代码,主 ...
- mysql远程备份
相关链接:https://blog.csdn.net/LiuHuan_study/article/details/81512831https://www.cnblogs.com/ryanzheng/p ...
- 《windows核心编程系列 》六谈谈线程调度、优先级和关联性
线程调度.优先级和关联性 每个线程都有一个CONTEXT结构,保存在线程内核对象中.大约每隔20ms windows就会查看所有当前存在的线程内核对象.并在可调度的线程内核对象中选择一个,将其保存在C ...
- CF916E
Codeforces 916E 简要题解Description Description 有一棵n个点的树,每个节点上有一个权值wi,最开始根为1号点.现在有3种类型的操作: 1 root, 表示将根设 ...
- 2017 JUST Programming Contest 3.0 D. Dice Game
D. Dice Game time limit per test 1.0 s memory limit per test 256 MB input standard input output stan ...
- 自定义View(11)**在onDraw中使用矩阵Matrix
1.代码示例 1.1 效果 原图 : 其尺寸为162 x 251,示例中的红点是变形的锚点. 变形之后: 1.2 代码 package com.e.weixin.session.view; impor ...