ural 1026. Questions and Answers 查询

2024-09-09 06:58

本文主要是介绍ural 1026. Questions and Answers 查询,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1026. Questions and Answers

Time limit: 2.0 second
Memory limit: 64 MB

Background

The database of the Pentagon contains a top-secret information. We don’t know what the information is — you know, it’s top-secret, — but we know the format of its representation. It is extremely simple. We don’t know why, but all the data is coded by integers from 1 up to 5000. The size of the main base (we’ll denote it be  N) is rather big — it may contain up to 100 000 those numbers. The database is to process quickly every query. The most often query is: "Which element is  i-th by its value?"— with  i being an integer in a range from 1 to  N.

Problem

Your program is to play a role of a controller of the database. In the other words, it should be able to process quickly queries like this.

Input

Input of the problem consists of two parts. At first, a database is written, and then there’s a sequence of queries. The format of database is very simple: in the first line there’s a number  N, in the next  N lines there are numbers of the database one in each line in an arbitrary order. A sequence of queries is written simply as well: in the first line of the sequence a number of queries K (1 ≤  K ≤ 100) is written, and in the next  K lines there are queries one in each line. The query "Which element is  i-th by its value?" is coded by the number  i. A database is separated from a sequence of queries by the string of three symbols "#".

Output

The output should consist of  K lines. In each line there should be an answer to the corresponding query. The answer to the query "i" is an element from the database, which is  i-th by its value (in the order from the least up to the greatest element).

Sample

input output
5
7
121
123
7
121
###
4
3
3
2
5
121
121
7
123
Problem Author: Leonid Volkov
Problem Source: Ural State University Internal Contest October'2000 Junior Session

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;public class Main {public static void main(String[] args) {new Task().solve();}
}class Task {InputReader in = new InputReader(System.in) ;PrintWriter out = new PrintWriter(System.out);class Query implements Comparable<Query>{int idx ;int num ;Query(int idx , int num) {this.idx = idx ;this.num = num ; }@Overridepublic int compareTo(Query other) {return Integer.compare(this.num , other.num) ;}}void solve() {int n = in.nextInt() ;int[] cnt = new int[5001] ;Arrays.fill(cnt , 0) ;while(n-- > 0){cnt[in.nextInt()]++ ;}int[] sum = new int[5001] ;sum[0] = 0 ;for(int i = 1 ; i <= 5000 ; i++){sum[i] = sum[i-1] + cnt[i] ;}in.next() ;int k = in.nextInt() ;Query[] query = new Query[k] ;for(int i = 0 ; i < k ; i++){query[i] = new Query(i , in.nextInt()) ;}Arrays.sort(query) ;int idx = 0 ;int[] result = new int[k] ;for(int v = 1 ; v <= 5000 ; v++){if(cnt[v] == 0){continue ; }while(idx < k && sum[v-1] < query[idx].num && query[idx].num <= sum[v]){result[query[idx].idx] = v ;idx++ ;}}Arrays.stream(result).forEach(out::println);out.flush();}}class InputReader {  public BufferedReader reader;  public StringTokenizer tokenizer;  public InputReader(InputStream stream) {  reader = new BufferedReader(new InputStreamReader(stream), 32768);  tokenizer = new StringTokenizer("");  }  private void eat(String s) {  tokenizer = new StringTokenizer(s);  }  public String nextLine() {  try {  return reader.readLine();  } catch (Exception e) {  return null;  }  }  public boolean hasNext() {  while (!tokenizer.hasMoreTokens()) {  String s = nextLine();  if (s == null)  return false;  eat(s);  }  return true;  }  public String next() {  hasNext();  return tokenizer.nextToken();  }  public int nextInt() {  return Integer.parseInt(next());  }  public int[] nextInts(int n){  int[] nums = new int[n] ;  for(int i = 0 ; i < n ; i++){  nums[i] = nextInt() ;  }  return nums ;  }  public long nextLong() {  return Long.parseLong(next());  }  public double nextDouble() {  return Double.parseDouble(next());  }  public BigInteger nextBigInteger() {  return new BigInteger(next());  }  }  


这篇关于ural 1026. Questions and Answers 查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1150473

相关文章

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

ural 1297. Palindrome dp

1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unli

ural 1149. Sinus Dances dfs

1149. Sinus Dances Time limit: 1.0 second Memory limit: 64 MB Let  An = sin(1–sin(2+sin(3–sin(4+…sin( n))…) Let  Sn = (…( A 1+ n) A 2+ n–1) A 3+…+2) An+1 For given  N print  SN Input One

ural 1820. Ural Steaks 贪心

1820. Ural Steaks Time limit: 0.5 second Memory limit: 64 MB After the personal contest, happy but hungry programmers dropped into the restaurant “Ural Steaks” and ordered  n specialty steaks

ural 1017. Staircases DP

1017. Staircases Time limit: 1.0 second Memory limit: 64 MB One curious child has a set of  N little bricks (5 ≤  N ≤ 500). From these bricks he builds different staircases. Staircase consist

ural 1014. Product of Digits贪心

1014. Product of Digits Time limit: 1.0 second Memory limit: 64 MB Your task is to find the minimal positive integer number  Q so that the product of digits of  Q is exactly equal to  N. Inpu

Mybatis中的like查询

<if test="templateName != null and templateName != ''">AND template_name LIKE CONCAT('%',#{templateName,jdbcType=VARCHAR},'%')</if>

京东物流查询|开发者调用API接口实现

快递聚合查询的优势 1、高效整合多种快递信息。2、实时动态更新。3、自动化管理流程。 聚合国内外1500家快递公司的物流信息查询服务,使用API接口查询京东物流的便捷步骤,首先选择专业的数据平台的快递API接口:物流快递查询API接口-单号查询API - 探数数据 以下示例是参考的示例代码: import requestsurl = "http://api.tanshuapi.com/a

DAY16:什么是慢查询,导致的原因,优化方法 | undo log、redo log、binlog的用处 | MySQL有哪些锁

目录 什么是慢查询,导致的原因,优化方法 undo log、redo log、binlog的用处  MySQL有哪些锁   什么是慢查询,导致的原因,优化方法 数据库查询的执行时间超过指定的超时时间时,就被称为慢查询。 导致的原因: 查询语句比较复杂:查询涉及多个表,包含复杂的连接和子查询,可能导致执行时间较长。查询数据量大:当查询的数据量庞大时,即使查询本身并不复杂,也可能导致

oracle11.2g递归查询(树形结构查询)

转自: 一 二 简单语法介绍 一、树型表结构:节点ID 上级ID 节点名称二、公式: select 节点ID,节点名称,levelfrom 表connect by prior 节点ID=上级节点IDstart with 上级节点ID=节点值 oracle官网解说 开发人员:SQL 递归: 在 Oracle Database 11g 第 2 版中查询层次结构数据的快速