Soj--4393:laob ' s PROBLEM1

Source: Internet
Author: User

Describe

A sequence contains n (1<n<=100000) elements, which may have the same number of M (1<=m<=100000) queries on this sequence, each of which contains an ordinal x, and answer the sum of the elements in the series that are less than the X element.

Input
The first line contains a number t representing the total number of test samples each set of samples contains 4 rows The first row contains an integer n that represents the length of the sequence the second line contains n less than 1000 positive integers separated by a space the third line contains an integer m for the number of queries the fourth row contains m of 1 to N of the integer representing the query number
Output
Each set of outputs contains a m-separated number of integers to buy the results of M-Times inquiry
Example Input
24255 139 58 41251 1 2 3 35427 563 289 203 32643 1 4 4
Example Output
197 197 58 0 0203 818 0 0
Analysis: This problem is not difficult, mainly there is easy to consider where, I am through the structure to hold the number and number, and then the value of the size of the order, and then calculate the value of each number of the results stored in the RES array, error-prone is not considering the number of possible repetition of the casesuch as this set of data5 the51 2 3 4Output0 0 0If you consider this set of data it should be a big problem.
put your own AC code.
#include <stdio.h> #include <algorithm>using std::sort;//define global Variables const int M=100000+5;STRUCT data{int N;int Num;} A[m];int B[m];int res[m];//is prioritized according to the size of the numeric value, if the number is equal then the ascending bool CMP (data X,data y) {if (X.N&LT;Y.N) {return true;} else if (X.N&GT;Y.N) {return false;} Else{return X.num<y.num;}} int main () {int t;scanf ("%d", &t), while (t--) {int n,cnt=1;scanf ("%d", &n), for (int i=0; i<n; ++i) {scanf ("%d", &AMP;A[I].N); a[i].num=i+1;} int m;scanf ("%d", &m);//Enter the number of the query for (int i=0; i<m; ++i) {scanf ("%d", &b[i]);} Sort the data in array a sort (a,a+n,cmp);//statistics are less than the sum of the corresponding values for each ordinal res[a[0].num]=0;for (int i=1; i<n; ++i) {if (A[I].N!=A[I-1].N) {res[ A[i].num]=res[a[i-1].num]+a[i-1].n;int temp=i-1;//If the front is all the same element while (TEMP&GT;0&AMP;&AMP;A[I-1].N==A[TEMP-1].N) {res [A[i].num]+=a[temp-1].n;--temp;}} else{//the same as the previous element Res[a[i].num]=res[a[i-1].num];}} The output is for (int i=0; i<m; ++i) {printf ("%d", Res[b[i]]),//Output space, and the last number outputs a newline if (i!=m-1) {printf ("");} else{printf ("\ n");}}}


Soj--4393:laob ' s problem1

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.