Construction repair

Time limit: C/C++ 1 second, other languages ​​2 seconds
Space limit: C/C++ 262144K, other languages ​​524288K
64bit IO Format: %lld
Title description
Xiaogang is playing a computer game called "Building Repair" provided by JSOI : After a fierce battle, the T tribe eliminated all the invaders of the z tribe. However, there are already N buildings in the base of the T tribe that have been severely damaged. If they are not repaired as soon as possible, these buildings will be completely destroyed.
The current situation is: there is only one repairman in the T tribe base. Although he can reach any building in an instant, it takes a certain amount of time to repair each building. At the same time, repairers can repair the next building only after repairing one building, and cannot repair multiple buildings at the same time.
If a building is not completely repaired within a period of time, the building will be scrapped. Your task is to help Xiaogang formulate a reasonable repair sequence to repair as many buildings as possible.
Input description: The
first line is an integer N. The next N lines are two integers T1 and T2 describing a building: repairing this building requires T1 seconds. If the repair is not completed within T2 seconds, the building will be scrapped.
Output description:
Output an integer S, indicating that up to S buildings can be repaired.
N <150,000; T1 <T2 <maxlongint

Input :

4
100 200
200 1300
1000 1250
2000 3200

Output:

3

Idea : Core: Greedy
Sort first, either by time-consuming or by deadline. If you think about it again, you must meet the deadline first.
When the election deadline cannot be met, you have to pause and decide whether to choose or not. If you choose, you will definitely kick out a building that you have previously selected. Therefore, the number of elections and non-elections is actually the same. Change one, it will definitely take a long time to change.
To sum up the analysis, first sort the deadlines, and when the deadlines are not met, kick off the most time-consuming of the selected buildings. You can use a large root pile to maintain the selected buildings, and just pop the top of the pile every time.


#include <bits/stdc++.h>

#define maxn 0x3f3f3f3f
using namespace std;

typedef long long ll;

priority_queue<int, vector<int >, less<int > > q; //建大根堆

struct jian
{
    
    
    ll tt,d;//定义耗时和截止时间
}t[200000];

bool cmp(jian a,jian b) // 假先修截止时间早的
{
    
    
    if(a.d!=b.d)    return a.d<b.d;
    else return a.tt<b.tt;
}
int main()
{
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    int n;
    ll sum=0; // 定义总耗时
    cin>>n;

    for(int i=1;i<=n;i++)
        cin>>t[i].tt>>t[i].d;

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

    for(int i=1;i<=n;i++)
    {
    
    
        sum+=t[i].tt;
        q.push(t[i].tt);
        if( sum >t[i].d) //(特别注意取等,正好可以修,可以
        {
    
    
            sum-=q.top();
            q.pop();
        }
    }
    cout<<q.size()<<endl;
}

Guess you like

Origin blog.csdn.net/m0_53688600/article/details/113618391