Tao Tao grab Apple's popular group # #

Problem Description

Went to the harvest season, the trees end up with a lot of Tao Tao, wrong, a lot of apples, there are many small Tao Tao came to pick apples. Each Tao Tao wants the greatest apple, so had an argument, in order to solve their conflicts, the question of man - a special rule, according to the size of the weight given order, each round is the first by fat first pick (to take care of fat), each of Tao Tao is very smart, do not miss the largest apple eyes. Now the question is, a total of apples n, m a Tao Tao, you want to order the original Tao Tao can grab the output of Apple's total size of each.

Input

The first line of the two numbers n, m.

Next, the number of row n, respectively, the size of each apple.

The next line number m, Tao Tao separately for each weight.

Output

Line number m, the size of each grab Tao Tao apple.

Sample Input

5 3

1 2 3 4 5

1 2 3

Sample Output

3 5 7

[Data scale]

n,m<=100000

Thinking

This question, at first glance the title, "ah ah ~ ~ good water." But le ......

Although the water looks, but I wrote the code but more than the "Tao Tao pick apple" to go. Let me talk about how to do it.

1. Enter

Enter Needless to say, but there need to be "fat Tao Tao" take a structure, enter the following code

. 1 Scanf ( " % D% D " , & n-, & m);
 2  for ( int I = . 1 ; I <= n-; I ++ )
 . 3      Scanf ( " % D " , & Apple [I]); // apple 
4  . 5 for ( int I = . 1 ; I <= m; I ++ )
 . 6 {
 . 7      Scanf ( " % D " , & T [I] .W); // input Tao Tao weight . 8      T [I] .L = I; // record number 9 }  

2. Sort

The very title of the presentationclearWe want to Tao Tao by ordering too fat, because 100,000 is not large, sort through it again.

. 1  int CMP (Node A, Node B) // Sort wt key 
2  {
 . 3      return AW> BW; // big to small 
4  }
 5  {
 6 ... // other code 
. 7  }
 . 8 Sort (T + . 1 , T + m + . 1 , CMP); // this is a sorting function 
. 9 Sort (Apple + . 1 , Apple + . 1 + n-); // Apple under way π

3. keep the answer

This step is also veryCattle 13,amount.

First Apple over again from n to 1, and then follow the pretreated home Tao Tao apples before.

. 1  int K = n-; // K enumeration Apple 
2  int I = . 1 ; // I Tao Tao enumeration 
. 3  the while (K =! 0 )
 . 4  {
 . 5      T [I] .c = Apple + [K]; / / record accumulation 
. 6      K--; // apple house 
. 7      I ++; // picked next Tao Tao 
. 8      IF (I> m) // picked to play again 
. 9          I = . 1 ; // then again wheel 
10 }

4. Output

Output is beautiful friends, but he is output by number, complexity a bit worrying now, forget it, control him, make another again.

. 1  int CMP2 (Node A, Node B)
 2  {
 . 3      return Al <BL; // press is numbered 
4  }
 5  {
 6      ... // other 
. 7  }
 . 8 Sort (T + . 1 , T + m + . 1 , CMP2 );
 . 9  for (I = . 1 ; I <= m; I ++ )
 10  {
 . 11      the printf ( " % D " , T [I] .c); // after finishing output 
12 }

Chatter chatter, this problem is solved, the whole paste the code:

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 int n, m;
 5 int apple[100001];
 6 struct node {
 7     int l;
 8     int w;
 9     int c;
10 }t[100001];
11 int cmp(node a, node b)
12 {
13     return a.w > b.w;
14 }
15 int cmp2(node a, node b)
16 {
17     return a.l < b.l;
18 }
19 int main()
20 {
21     scanf("%d%d", &n, &m);
22     for(int i = 1; i <= n; i++)
23         scanf("%d", &apple[i]); 
24     
25     for(int i = 1; i <= m; i++)
26     {
27         scanf("%d", &t[i].w);
28         t[i].l = i;
29     }
30     sort(t + 1, t + m + 1, cmp);
31     sort(apple + 1, apple + 1 + n);
32     int k = n;
33     int i = 1;
34     while(k != 0)
35     {
36         t[i].c += apple[k];
37         k--;
38         i++;
39         if(i > m)
40             i = 1;
41     }
42     sort(t + 1, t + m + 1, cmp2);
43     for(i = 1; i <= m; i++)
44     {
45         printf("%d ", t[i].c);
46     }
47 }

Milk think ~ ~ ~ fly horse ...

 

Guess you like

Origin www.cnblogs.com/wnfs/p/11516100.html