NOIP Universal group 2017 librarian librarian__ the world is very big, cherish the water

Source: Internet
Author: User
The book keeper of the boring

Boring Librarian Topic description Analysis-Water title-water problem-really is the water title code-not necessarily is to solve some doubtful place end

Topic Description

Each book in the library has a book code that can be used to quickly retrieve books, which encode a positive integer. Each reader of the library has a demand code, which is also a positive integer. If a book's books are coded to end with the reader's demand code, the book is what the reader needs. Little D just became the librarian at the library, she knows all the books in the library code, she asked you to help her write a program, for each reader, to find the book he needs the smallest books, if there is no book he needs, please output-1.

input and output formats

Input Format:
The first line of the input file contains two positive integers n and q, separated by a single space, representing the number of books in the library and the number of readers.
The next n rows, each containing a positive integer, represents the book encoding for a particular textbook in the library.
The next Q line, each row contains two positive integers, separated by a single space, the first positive integer represents the length of the reader's demand code in the library, and the second positive integer represents the reader's demand code.
output Format:
The output file has a Q line, each containing an integer, and if there is a book that the first reader needs, the book encoding of the book with the least number of books in the books that the reader needs is encoded in line I, otherwise output-1.

input and Output sample

Input Sample:
5 5
2123
1123
23
24
24
2 23
3 123
3 124
2 12
2 12
Output Sample:
23
1123
-1
-1
-1

"Data size and Convention"
For 20% of the data, 1≤n≤2.
Another 20% of the data, q = 1.
Another 20% of the data, all readers of the demand code length is 1.
With another 20% of the data, all the book codes are given in order from small to large.
For 100% of the data, 1≤n≤1,000,1≤q≤1,000, all the book code and demand code are not more than 10,000,000. Analysis-water problem-water problem-it's a water problem.

Water problem
According to the requirement code , a For statement is the code for each book, which is more legitimate
And then in all the legitimate encodings, find the minimum value
The more than the legal method considers the remainder to be the last few, making it the same length as the requirement code
Check the minimum value, you can first sort, save time
So it's really still a water problem code-not necessarily a positive solution .

#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 1000;
int a[maxn+5];
int pow_10 (int x)
{
    int res = 1;
    for (int i=1;i<=x;i++)
        res = res *;
    return res;
}
int main ()
{
    int n,q;
    scanf ("%d%d", &n,&q);
    for (int i=1;i<=n;i++)
        scanf ("%d", &a[i]);
    Sort (a+1,a+n+1);
    for (int i=1;i<=q;i++)
    {
        int x,y;
        scanf ("%d%d\n", &x,&y);
        int k = pow_10 (x);
        BOOL flag = FALSE;
        for (int j=1;j<=n;j++)
        {
            if (a[j]%k = = y)
            {
                flag = true;
                printf ("%d\n", A[j]);
                break;
            }
        }
        if (flag = = False) printf (" -1\n");
    }
    return 0;
}
some place of doubt.

Why is this not necessarily a positive solution?
According to the school's internal bosses ' discussion, the problem may have a leading 0 .
In other words, it is possible to appear:
Book: 123,10123 123,10123
Question: 0123 0123
It's embarrassing.
But if that's true, it could be:
Book: 123,0123 123,0123
Question: 123 123
What to output at this time.
Book: 123,0123 123,0123
Q: 00000000000. Omit 109 0 ... 123 00000000000 ... omit 10^9 0 ... 123
What's the output now?
So if it's really wrong ... Find out the theory of people's problems. End

Just like this, the new day, also please a lot of care oh (ノω<. ) ノ) ☆. ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.