Doing Homework Again (range selected)

Source: Internet
Author: User
Tags min sort

Description Ignatius have just come back school from the 30th ACM/ICPC. Now he had a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher would reduce his score of the final Test. And now we assume this doing everyone homework always takes one day. So Ignatius wants-to-help him to arrange the order of doing homework to minimize the reduced score.

Input the input contains several test cases. The first line of the input was a single integer T so is the number of test cases. T test Cases follow.
Each test case start with a positive integer N (1<=n<=1000) which indicate the number of homework. Then 2 lines follow. The first line contains n integers this indicate the deadlines of the subjects, and the next line contains N integers that Indicate the reduced scores.

Output for each test case, you should output of the smallest total reduced score, one line per test case.

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

Problem Description: Ignatius after the game back, each teacher gave Ignatius a deadline to hand in the homework, if not pay off points. Each job must be completed in one day, the minimum number of points to deduct. First enter a T to indicate that there is a T group of test data, then each group of data first entered an n, representing n jobs, and then enter two lines, the first line represents the date of each job to be delivered, the second line indicates the corresponding if not to pay the job to buckle points. Outputs the minimum score to buckle.

Greedy question:

Limit the number of days from small to large, if the limit of the number of days at the same time, the value of the deduction points from large to small sort.

Defines an array of one.

Pseudo-code (discussion of the minimum value):

if (limit number of days is not less than the current number of days)//No deduction at this time

{

To mark with an array;

The current number of days increased;

}

else//At this time to face the deduction of the situation

{

Find the minimum value from 0 to the current number of days, accumulate.

}

AC

#include <stdio.h> #include <string.h> #include <math.h> #include <iostream> #include < Algorithm> #include <string> using namespace std; struct P    //x is a limit of days {    int  x,y;} a[1001]; BOOL Comp (P x,p Y) {    if (x.x==y.x)         return x.y>y.y;     return x.x&lt ; y.x; } int main () {    int s[1001];//tag using     int n,i,j,m,k,     cin>>n;     WH Ile (n--)     {        memset (s,0,sizeof (s)),         cin>>k; &nbs P       for (i=0; i<k; i++)             scanf ("%d", &a[i].x);         for (i=0; i<k; i++)             cin>>a[i].y;         sort (a,a+k,comp);         int sum=0,d=1,min,t;         for (i=0; i<k; i++)     &NBSP;   {            if (a[i].x>=d)    //no deduction (with a special value, such as a third group, the first 4 is passed later d= 5, so the remaining 4 will not pass, to 6 can only)             {                s [I]=1;                 d++;            }             else        //Search The smallest case, the accumulation             {                MIN=A[I].Y;                 t=i;                 for (j=i-1; j>=0; j--)                     if (s[j]==1&&a[j].y<min)    //s[j]=1 proves that the number belongs to no deduction category       &NB Sp             {                      & nbsp MIN=A[J].Y; &nbsP                       T=J;                    }               & nbsp SUM+=A[T].Y;                 A[T].Y=A[I].Y;      //Exchange (may be minimized in the next discussion)            }        }         cout<<sum<<endl;    }}

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.