FJUT16 first week winter vacation homework problem

Source: Internet
Author: User

Pose problem 3timelimit:1000ms memorylimit:128000kb64-bit integer IO format:%lldProblem description up pose problem is so-called optimization problem, in the team game, teams found a problem of water, then who should be handed over to deal with? As the code to deal with water problems, what qualities should be possessed?
1, to fast, the water problem is speed!
2, can not card water problem! Water problems are cards, absolutely not a code hand demeanor!
3, can not be wrong, the wrong time penalty is 20 minutes, for the water problem is fatal!
4, to be able to see a problem is water! Without this, the above three is meaningless!

If you want your team to be a qualified code hand, then this is your best choice, fast AC bar!

This series is in order to improve the quality of water problem code hand prepared! Water problems often need to use simple optimization, in the problem solving process also often need a variety of optimization, optimization is the first choice to deal with timeouts, the purpose is to reduce the complexity of time.

The pose is titled 3, the surface is exactly the same, only the data range is different, please choose the appropriate algorithm according to different data range.

Title Description:
Given series A[1] a[2] ... a[n]
Multiple inquiries
Every time you ask for a number Qi
Ask how many groups (L,r) meet F (l,r) =a[l]+a[l+1]+...+a[r]=qiinput The first line is a t that represents the number of groups of test data.

The first row of each set of data is two integers n, q, respectively, indicating the length of the array and the number of queries
The second line is n integers a[1],a[2],..., a[n].
Next is the Q line, where each line of an integer qi denotes the first I ask.

Data range:
Question 1th: t<=130,1<=n<=1000,-10^9<=ai<=10^9,-10^12<=qi<=10^12, Big data not more than half, each group of big data Q is 1 or 2.
Question 2nd: t<=130,1<=n<=10000,q<=50,1<=ai<=10^9,1<=qi<=10^12, the big data is not more than half.
question 3rd: t<=30,1<=n<=1000,q<=1000000,-10^6<=ai<=10^6,-10^6<=qi<=10^6, Big Data not more than 5 groups.

Be careful to compare the data range of each number of each problem, then choose the appropriate algorithm AC bar. Be careful not to mention the staggered topics. Output for each query, outputs an integer that represents the answer Sampleinput
15 64 5 6 5 4411120610
Sampleoutput
220210
Thinking: The data of this problem has a characteristic, the number of queries is very much, and the number of data groups are not many, so many times asked to calculate a lot of time, so we need to ask before he even out all the answers, so that he can directly output the answer when asked, the specific implementation method is as follows
1, first of all, this problem needs to calculate the interval and, so we use the prefix and the method to save data.
1         sum[0]=0; 2          for (i=1; i<=n;i++) 3         {4             scanf ("%d",&temp); 5             sum[i]=sum[i-1]+temp; 6         }
Prefixes and
2, this time the data is negative, I intend to use two arrays to store a positive number of answers and negative answers, a array of positive numbers and 0 of the answer, b array stored negative answer.
3, to make a table, traverse all the interval, the answer may be asked to save into the array, if the conditions are met, let the corresponding array element +1.
1          for(i=0; i<n;i++)2              for(j=i+1; j<=n;j++)3                 if(ABS (Sum[j]-sum[i]) <=1000005)///sum is the first I item and, A has a positive answer, B has a negative number4                     if(sum[j]-sum[i]>=0)5a[sum[j]-sum[i]]++;6                     Else7b[sum[i]-sum[j]]++;
Play Table
4, the combination of the above methods, write the corresponding program, pay attention to the initialization of each time, you can AC.

FJUT16 first week winter vacation homework problem

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.