Third Zhou Zhousai--Basic data Structure completion field (AK will be insisted on, the title is from Codeforces 633c,633d,631b,651a,651c and poj1577) __ data structure

Source: Internet
Author: User
Tags map class

Question A:

A title link

Topic Description:

Fibonacci-ish timelimit:3000ms memorylimit:512mb 64-bit integer IO format:%i64d
Problem Description

Yash has recently learnt about the Fibonacci sequence and is very about it. He calls a sequence fibonacci-ish if the sequence consists to at least two elements F0 and F1 are arbitrary fn + 2 = fn + 1 + fn for all n≥0.

You are given some sequence of integers a1, A2, ..., an. Your task is rearrange elements of this sequence in such a way it longest possible prefix is fibonacci-ish. Input

The ' The ' input contains a single integer n (2≤n≤1000)-the length of the sequence AI.

The second line contains n integers a1, a2, ..., an (|ai|≤109). Output

Print the length of the longest possible fibonacci-ish prefix the given after sequence. Sampleinput 1

3
1 2-1
Sampleoutput 1
3
Sampleinput 2
5
28 35 7 14 21
Sampleoutput 2
4
     
     
      
      
 Note

     
     

In the, if we rearrange elements of the sequence as-1, 2, 1, the whole sequence AI would is fibonacci-ish.

In the second sample, the optimal way to rearrange elements is,,,, 28.

Meaning

Given a sequence of length n, find the longest Fibonacci sequence and output its length.

Analytical:

This problem was meant to be done directly by two for circular violence, then use the "bucket" thought record results, looked at the scope of the data |ai|<=10^9, it is clear that the idea of bucket is not workable, and then think of whether it can be used to find two points, wrote a long time to find two points can not, each traversal superimposed process, Every time and must find the number of the following is equal to the number, so the time complexity is too large, it must be timed out, and then in the csdn to find someone else's problem-solving report. found that most of the practice is still two for loop brute force solution, but it also did a lot of optimization, the following I talk about the problem of their own understanding.

The first thing to understand is the composition of the Fibonacci series, the first and second numbers being the specified number, starting with the third number, each number is the sum of the first two digits of its own corresponding, then we can use the idea of "bucket", when we sum up two of them and get a number, we directly use the "bucket" Whether it is 0 can determine whether the number exists, but this problem data range reaches the 10^9, then uses the array as "the bucket" obviously is not possible, therefore we may use the map container (the mapping relation), this also is a similar to "the bucket" the container, therefore we can enter the data the time to record first

The number of each number to sort the number of N in ascending order.

The "bucket" problem is solved, so how do we find the longest Fibonacci sequence?

according to the above description of the Fibonacci series, we first want to determine the first two number, then the first two numbers and find a third number, and then according to the second third number and find the fourth number

.... This goes on until the nth number of such numbers does not exist. Then record the length of this Fibonacci sequence.

The process described above is not very much like the process of the Euclidean method. So we can get the following code for the length of the firstborn sequence:

int find_quence (int a,int b)
{
    int ans = 0;
    if (F[a+b])
    {
        --f[a+b];
        Ans = find_quence (b,a+b) + 1;
        ++F[A+B];
    }
    return ans;
}

The explanation of the recursive procedure:

first, the recursive program we need to identify two issues:

1. Initial value at the beginning of recursion

2. Termination conditions of recursion .

Obviously, the starting condition of this recursive program is the formal parameter passed by the main function, and the termination condition is f[a+b] = 0 (The Map class container F is our "bucket"). At the beginning, the sequence length is 0,

We begin the analysis from the first entry into the IF loop, the If loop, which indicates that there is a number equal to a+b in the sequence, so we can incorporate that number into the sequence, so we want to put that number out of the bucket, and then the sequence length

After 1, and then in the same way, look for the second and third numbers in the sequence and whether they exist. So go on, when f[a+b] does not exist, that is, the f[a+b at this time = 0, then if loop is not established,

function returns 0 to the place where the function is called at the previous level, followed by a process that resembles the prefix and overlay process, the return of one layer at a time, the addition of the result of each return one by one to the previous call to it

The length of the sequence of numbers (in fact, this is a backtracking process, meaning that this path I have explored, I want to go back to where I first explored, so I explored the place to be one of the return

Go so that I can use it the next time I explore it, until I return to the place where the function was originally called, bring the result of ANS back to the main function.

Describe the process:

into the stack → continue to recursive conditions are not satisfied (stop into the stack)-> out the stack (explored the path return, will explore the results of the report to their own "upper level")-> stack empty-> total results brought back to the main function

After you know the above process, write the corresponding code submission, but it still timed out.

What is the reason.

The Fibonacci sequence of each item is different, so the given sequence repeats elements to be processed in advance, so that reduce the number of traversal, so as to reduce the complexity of time;

Let's take an example:

Just like we gave the sample 1.

If the sorted sequence is like this:

-1-1-1-1 1 1 1 1 2 2 2 2-2

The final result of such a sequence is still 3, and the repetition element gives us no contribution to the length of the Fibonacci sequence. But the number of traversal is much. such a sequence would have traversed the number of times as long as 3*3.

without going heavy, it becomes 13*13, and the efficiency is very low in the case of a large number of repetitive elements. That's why we timed out. The words here call the unique method in the STL to weigh:

int N = unique (num,num+n)-num;   Go heavy, similar to the thought of pruning, reduce the number of repeated computations

C + + STL algorithm series 4---unique, unique_copy function
Of course you can also write a for loop to go heavy, both of which can be
With several optimizations above, we've come to the following code:

#include <cstdio> #include <iostream> #include <algorithm> #include <map> using namespace std;
const int MAXN = 1010;
int NUM[MAXN];
Map<int,int> F;
    int find_quence (int a,int b) {int ans = 0;
        if (F[a+b]) {--f[a+b];
        Ans = find_quence (b,a+b) + 1;
    ++F[A+B];
return ans;
    int main () {int n;
    scanf ("%d", &n);
        for (int i = 0;i < N;++i) {scanf ("%d", &num[i]);
    ++f[num[i]];         Sort (num,num+n);
    Reduce the number of repeat traversal after sorting int ans = 0;   int N = unique (num,num+n)-num; To weight, similar to the thought of pruning, reduce the number of repetitions for (int i = 0;i < n;++i) for (int j = 0;j < N;++j) {if (i = =
            J && F[num[i]]==1) continue;
            --f[num[i]],--F[num[j]];
            ans = max (ans,find_quence (num[i],num[j]) + 2);
        ++F[NUM[I]],++F[NUM[J]];
    printf ("%d\n", ans);
return 0; }


However, this is not an optimal solution, the optimal solution time is only 40+ms, interested can study:

Codeforces 633d-fibonacci-ish discretization + two-point query

Discretization of data


Question B:

B title Link

Topic Description:

Print Check timelimit:1000ms memorylimit:256mb 64-bit integer IO format:%i64d
Problem Description

Kris works in a large company "Blake Technologies". As a best engineer of the company he is assigned a task to develop a printer that'll be able to print horizontal and ve Rtical strips. The prototype is already built and Kris wants to tests it. He wants your to implement the "checks" the result of the printing.

Printer works with a rectangular sheet of paper of size NXM. Consider the list as a table consisting of n rows and Mcolu Mns. Rows are numbered from-bottom with integers from 1 to n, while columns are numbered the from-right and the integer s from 1 to M. Initially, all cells are painted in color 0.

Your program has to support two operations:paint all cells into row ri in color ai; Paint all cells in the column CI in color AI.

If during some operation I there is a cell that have already been painted, the "color of this cell" also changes to AI.

Your program has to print the resulting table after K operation. Input

The "a" of the input contains three integers n, m and K (1≤n, m≤5000, N m≤100 000, 1≤k≤100)-the Dimensions of the sheet and the number of operations, respectively.

Each of the next K lines contains the description of exactly one query:1 ri ai (1≤ri≤n, 1≤ai≤109), means that row RI is painted in color AI; 2 ci ai (1≤ci≤m, 1≤ai≤109), means that column ci are painted in color AI. Output

Print n lines containing m integers each-the resulting table after all operations are. Sampleinput 1

3 3 3
1 1 3
2 2 1
1 2 2
Sampleoutput 1



Sampleinput 2
5 3 5

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.