[20190718 test] [revocable greedy] A problem

1 A

 

1.1 Description

   LZGZ dorm room eight people can live, comes with a separate toilet and a separate bathroom. However, despite the teacher and then back to the dorm to emphasize how we must seize the time to wash, there are still a bunch of students addicted to all kinds of things, until the lights before going to take a bath. The problem is that high school Liuzhou uninterrupted supply of hot water is not, for example, not noon supply of hot water, the water temperature will gradually decrease after lights out at night, until it became cold.

  So, in the cold of winter, dormitory lights out after a period of time in the evening, the bathroom can get pretty cold water, bathing the students will feel extremely painful. Of course, different people for the endurance of cold water is not the same.

  We might remember the students began to take a bath time is 0, a student takes a bath is T1, and time once more than T2, then he (she) will feel very painful. Note that if the students in the process of taking a bath outside for more than T2, he (she) will still feel very

pain.

  In general, the more quarters of the people, the fewer the number of students can comfortably take a bath, but not absolute.

  If you are always president, how would you arrange the students of bathing order so that the largest number of students it can comfortably take a bath?

  Of course, if you only consider an individual of only eight quarters, the question was too simple, you need to handle the case when the number is n.

1.2 Input

  The first line of the input integer n, the number of quarters.

  The next n lines, each line an integer of two T1, T2, describes a bathing student takes time and patience.

 

1.3 Output

  Outputting a maximum integer number of students comfortable bathing.

 

1.4 Example

input:

4
100 200
200 130
1000 1250
2000 3200

 

output:

3

 

1.5 Hint

  For 50% of the data,. 1 ≤ n-≤ 10 . 3 .
  To 100% of the data,. 1 ≤ n-≤ 10 . 3 , 0 <Tl <T2 <2 31 is -. 1.


The following is a solution to a problem of time (you wrote it?)

See this topic, many people must think of the greedy, but note that this is not an ordinary greedy, but  revocable greedy

You can take a look at  P3045 [USACO12FEB] cattle ticket Cow Coupons 

 

What is a revocable greedy it?

  Popular that is already selected items to be greedy better alternative embodiment, the article has been replaced is taken off.

  This question is put on, it is that we let a person to take a bath, if necessary, and then he was  naked  pull out, for a person to go

 

First on the code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
typedef long long LL;
using namespace std;
const LL N=1e6+3;
struct node{LL a,b;}t[N];
bool cmp_for_MAXB(node x,node y){
    return x.b<y.b;
}
int main(){
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
    priority_queue<int>Q;
    LL n,used=0,tot=0;
    scanf("%I64d",&n);
    for(LL i=1;i<=n;++i)
        scanf("%I64d%I64d",&t[i].a,&t[i].b);
    sort(t+1,t+1+n,cmp_for_MAXB);
    for(LL i=1;i<=n;++i){
        if(used+t[i].a<=t[i].b){
            Q.push(t[i].a);
            ++tot;
            used+=t[i].a;
        }else{
            if(used-Q.top()+t[i].a<=t[i].b&&t[i].a<Q.top()){
                used=used-Q.top()+t[i].a;
                Q.pop();
                Q.push(t[i].a);
            }
        }
    }printf("%I64d",tot);
    return 0;
}

 

 

 

In fact, a few part

 

sort(t+1,t+1+n,cmp_for_MAXB);

Ordered by time T2 showered (if T2 is small even in front of a choice are gone ...), it should be well understood, according to T1 sort of little use, because after washing can also rely on fast wash ~ qwq

 

// used is the elapsed time of 
for (I = LL . 1 ; I <= n-; ++ I) { IF (Used + T [I] II.A <= T [I] .B) {// if the time enough to make the person washing, wash it Q.push (T [I] II.A); ++ TOT; Used + = T [I] II.A; } the else {// if the waste of time most people pull out , which allows people washing, and this person than the person torn out time-consuming, the substitutions IF (used-Q.top () + T [I] II.A <[I] .B && T = T [I] II.A < Q.top ()) { Used = used-Q.top () + T [I] II.A; Q.pop (); Q.push (T [I] II.A); } } }

General revocable greedy will use a priority queue to find out which is better replaced.

Apparently no matter how greedy, have a principle :  we must not lose!

 

to sum up

  Greedy subject either to do it or can not make dawned and saw the solution to a problem is to think of himself.

  So to experience a variety of multi-model, in order to better practice thinking.

 

Guess you like

Origin www.cnblogs.com/lsy263/p/11294093.html