UVA
来源:互联网 发布:telnet端口连接失败 编辑:程序博客网 时间:2024/06/06 06:36
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 is terminated by a test case where N = 0 and Q = 0.
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.
4 1235155 213331230 0
CASE# 1:5 found at 4CASE# 2:2 not found3 found at 3
现有N个大理石,每个大理石上写了一个非负整数。首先把各数从小到大排序,然后回答Q个问题。每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上
写着x。排序后的大理石从左到右编号为1~N。
先排序,再查找。使用algorithm头文件中的sort和lower_bound
#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <string>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <map>#include <set>using namespace std;const int maxn = 10001;int main() { int cnt = 0; int a[maxn]; int n; int q; while(cin >> n >> q) { if(n == 0) { break; } for(int i = 0;i < n;i++) { cin >> a[i]; } sort(a,a+n); cnt++; cout << "CASE# " << cnt << ":" << endl; while(q--) { int x; cin >> x; int t = lower_bound(a, a+n, x) - a; if(a[t] == x) { printf("%d found at %d\n", x, t+1); } else { printf("%d not found\n", x); } } } return 0;}
- uva
- UVA
- UVA
- UVA
- uva
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- C++ 智能指针
- shiro 简介
- ActiveMQ解决分布式事务方案以及代码实现(二)
- 多线程(1)
- 用SaltStack实现自动部署OpenStack
- UVA
- 2017 ACM-ICPC亚洲区域赛 西安站总结
- C语言头文件深入理解
- php 入门
- FCM(Firebase Cloud Messaging) 接入问题说明
- 2017-11-2 跟进昨日白银,继续分析
- $_POST 和php://input的区别
- POJ3624 Charm Bracelet
- tomcat免安装版本的常用配置