Program design thinking and practice Week4 operations (1/2 / Smart Class)

Fear of DDL - A

ZJM there are n jobs, each job has its own DDL, if ZJM not done the job before the DDL, then the teacher will deduct this job all the usual points.

So ZJM want to know how to arrange the order of homework to buckle a bit less points as possible.

Please help him!

Input

Input of T test. The first input line is a single integer T, was found in the number of test cases.

Each test case to begin a positive integer N (1 <= N <= 1000), indicates the number of jobs.

Then two lines. The first line contains N integers representing the DDL, the next row containing N integers representing buckle points.

Output

For each test case, you should reduce the output of the minimum total score, each test line.

Sample Input

3
3
3 3 3
10 5 1
3
1 3 1
6 2 3
7
1 4 6 4 2 4 3
3 2 1 7 6 5 4

Sample Output

0
3
5

Ideas:

According to the greedy algorithm, starting from the largest DDL, DDL will be all for today's jobs added to the heap, and then select from the current value of the maximum heap out to ensure that each selection is the greatest value to meet the conditions.

Ergodic process is as follows

for(int i=maxDay; i>0; i--) {
   for(int j=0; j<n; j++) {
      if(ddl[j]==i) {
         heap.push(t[j]);
      }
   }
   int tmp = 0;
   if(heap.size()) {
      tmp = heap.top();
      heap.pop();
   }
   s-=tmp;
}

B - four columns

ZJM has four columns A, B, C, D, each column has the number of digits n. ZJM a number taken from each respective column number, he would like to know how many kinds of programs such that the number of 4 and 0.

When a number of columns has the same plurality of numbers, to treat them as a different number.

Please help him!

Input

The first line: n (number representative of the number of columns) (1≤n≤4000)

The next n-th row, the i-th row of four numbers, each number of columns A, B, C, D in the i-th digit (digit 2 to the power of not more than 28)

Output

The number of different output combinations.

Sample Input

6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45

Sample Output

5

Hint

Sample Explanation: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46), (--32, 30, -75, 77 ), (-32, -54, 56, 30).

Thinking

Enumeration of a and b and get all of them composed of ab array, and then sort them. C and d and then the traversal, the traversal, since ab ordered array, it is possible to find the position opposite the first time and the number of the last occurrence in the ab by half, the distance therebetween is the inverse number of number, ans + = to.

Wherein, find_first () function:

int find_first(int p){
   int right = n*n-1;
   int left = 0;
   int mid;
   int ans = -1;
   while(left<=right){
      mid = (right+left)/2;
      if(ab[mid]<p){
         left = mid+1;
      }
      else if(ab[mid]>p){
         right = mid-1;
      }
      else{
         ans = mid;
         right = mid - 1;
      }
   }
   return ans;
}

find_final () function

int find_final(int p){
   int right = n*n-1;
   int left = 0;
   int mid;
   int ans = -1;
   while(left<=right){
      mid = (right+left)/2;
      if(ab[mid]<p){
         left = mid+1;
      }
      else if(ab[mid]>p){
         right = mid-1;
      }
      else{
         ans = mid;
         left = mid + 1;
      }
   }
   return ans;
}

C - TT mystery gift

TT is a severe cat lovers, cat channel on the daily indulge in B station.

One day, a friend ZJM decided to TT TT a problem, if TT can solve this problem, ZJM will buy a cute cat gave TT.

SUMMARY is given array cat [i] a number N, and generate a new array ans [i] with the array. It is defined as the new array for any i, j and i = j, are ans [] = abs (cat [i] - cat [j])!, 1 <= i <j <= N. This new array. Determine the median, then the median is the sort (len + 1) / 2 corresponding to the position number, '/' is a rounding.

TT desperately wanting bird cute cat, can you help him?

Input

Plural sets of inputs, each input one N, the number N expressed, after the input of a sequence of length N cat, cat [i] <= 1e9, 3 <= n <= 1e5

Output

Output new array ans median

Sample Input

4
1 3 2 4
3
1 10 2

Sample Output

1
8

Ideas:

For the new array, to find the index (len + 1) to a position corresponding to a digital / 2, the answer must lie between min and max, can be performed to find a number between min and max, since the number between the new serial number in the array is incremented, so that the index can be found by the median points of the two methods, and for each index number, if the cat sort the array, can be turned into the ans ans [] = cat [j] -cat [i ], i.e. cat [j] = ans [] + cat [i], i enumeration so satisfying a cat [j] <= ans [] + cat [i] is obtained, and j is the number of the current number of serial number, and the cat [j] is increasing, it can be found by half to meet the conditions <= j last relationship, in order to obtain the serial number of each number, serial number in the median is the answer.

Wherein j is obtained as a function of the last

int getMaxJ(int i,int p) {
   int r = n-1;
   int l = i+1;
   int mid;
   int ans = -1;
   while(l<=r){
      mid = (l+r)>>1;
      if(a[mid]<=p+a[i]){
         ans = mid;
         l = mid+1;
      }
      else{
         r = mid - 1;
      }
   }
   return ans;
}

Obtained as a function of number

int getNum(int p){
   int c = 0;
   for(int i=0;i<n;i++){
      int tmp = getMaxJ(i,p);
      if(tmp!=-1){
         c+=(tmp-i);
      }
   }
   return c;
}

Guess you like

Origin www.cnblogs.com/mopa/p/12528295.html