A website through 1430: Homework

Original title  Portal

Description [title]

In the first day of school the teacher took all the jobs are arranged, each job if handed in within the stipulated time, then have the credits. Each deadlines and credit operations may be different. For example, if a job is 10 credits required to pay in six days, then in order to get the 10 credits, you must pay before the end of the sixth day.

Time to complete each job is only one day. For example, assume there are seven jobs and completion time credit as follows:

Job number 1 2 3 4 5 6 7
the term 1 1 3 3 2 2 6
credit 6 7 2 1 4 5 1

 

Can get up to 15 credits, including a complete job order is 2,6,3,1,7,5,4, d may be noted there are other ways.

Your task is to find a sequence of homework to get the most credits.

[Enter]

A first line integer N, the number of jobs.

Next N rows, each row comprising two integers, the first integer represents job completion deadline, the second number indicates the unit job.

[Output]

Output An integer representing the maximum credit that can be obtained. To ensure that the answer does not exceed longint range.

[Sample input]

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

[Sample Output]

15

 

At first glance this question, and Intellect surfing a bit like, even simpler than some of the questions, do not judge how to do endless.

Greedy strategy:

In order to make credit as much as possible, we want to have to do as much work credits, credits descending order according to our first, and then enumerate each job, try to make this work against deadlines;

We can look forward from a period of a job, if the job is not arranged point in time will find it on the schedule.

code show as below:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct homework
{
    int k,date;
}a[1000001];
int read()
{
    char ch=getchar();
    int a=0,x=1;
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') x=-x;
        ch=getchar (); 
    } 
    the while (CH> = ' 0 ' && CH <= ' . 9 ' ) 
    { 
        A = (A << . 3 ) + (A << . 1 ) + (CH-, ' 0 ' ); 
        CH = getchar () ; 
    } 
    return A * X; 
} 
int CMP (Homework X, Y Homework) 
{ 
    return XK> YK;               // accordance with descending order credits 
}
 int n-, SUM, the hash [ 1000001 ];
 int main () 
{ 
    n-= Read ();
     for ( int I = . 1 ; I <= n-; I ++ ) 
    { 
        A [I] .date = Read (); 
        A [I] .K = Read (); 
    } 
    Sort (A + . 1 , A + . 1 + n-, CMP);
     for ( int I = . 1 ; I <= n-; I ++ ) 
    { 
        int BJ = . 1 ;
         for ( int J = A [I] .date; J> = . 1 ; J,) // as far as possible close to the deadline to do the job, to reduce the impact on other jobs 
        {
             IF(hash[j]==0)
            {
                hash[j]=1;
                bj=0;
                sum+=a[i].k;
                break;
            }
        }
    }
    cout<<sum<<endl;
    return 0;
}

But after this code is to pay up two points TLE We found it! We want to further optimize!

If we arrange for a job i, found that from a [i] .date 1 to enumerate their hash is 1, that is to say: from 1 ~ a [i] .date are already booked up, arrangements can not other homework;

Q we can use this to record a [i] .date, for the latter operation, if it is the period a [i + 1] .date <= q, this operation must not be described arrangement, we directly out like, without then enumerated;

AC code is as follows:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct homework
{
    int k,date;
}a[1000001];
int read()
{
    char ch=getchar();
    int a=0,x=1;
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') x=-x;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        a=(a<<3)+(a<<1)+(ch-'0');
        ch=getchar();
    }
    return a*x;
}
int cmp(homework x,homework y)     //按照学分从大到小排序 
{
    return x.k>y.k;
}
int n,sum,q,hash[1000001];
int pan(int x)
{
     
    {for ( int I = A [X] .date; I> = . 1 ; i-- ) 
    { 
        IF (the hash [I] == 0 ) 
        { 
            the hash [I] = . 1 ;
             return  . 1 ; 
        } 
    } 
    Q = A [X ] .date;                    // If here described 1 ~ a [x] .date booked up, recorded by q 
    return  0 ; 
} 
int main () 
{ 
    n- = Read ();
     for ( int I = . 1 ; I <= n-; I ++ ) 
        A [I] .date =read();
        a[i].k=read();
    }
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=n;i++)
    {
        if(a[i].date<q) continue; //直接跳出,节省时间 
           if(pan(i)) sum+=a[i].k;    
    }
    printf("%d\n",sum);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/xcg123/p/10994763.html