USACO 2018 DEC #2 (Silver): Convention II

Despite long delays in airport pickups, Farmer John’s convention for cows interested in eating grass has been going well so far. It has attracted cows from all over the world.

The main event of the conference, however, is looking like it might cause Farmer John some further scheduling woes. A very small pasture on his farm features a rare form of grass that is supposed to be the tastiest in the world, according to discerning cows. As a result, all of the N cows at the conference (1≤N≤10^5) want to sample this grass. This will likely cause long lines to form, since the pasture is so small it can only accommodate one cow at a time.

Farmer John knows the time Ai that each cow i plans to arrive at the special pasture, as well as the amount of time Ti she plans to spend sampling the special grass, once it becomes her turn. Once cow i starts eating the grass, she spends her full time of Ti before leaving, during which other arriving cows need to wait. If multiple cows are waiting when the pasture becomes available again, the cow with the highest seniority is the next to be allowed to sample the grass. For this purpose, a cow who arrives right as another cow is finishing is considered “waiting”. Similarly, if a number of cows all arrive at exactly the same time while no cow is currently eating, then the one with highest seniority is the next to eat.

Please help FJ compute the maximum amount of time any cow might possibly have to wait in line (between time Ai and the time the cow begins eating).

INPUT FORMAT (file convention2.in):

The first line of input contains N. Each of the next N lines specify the details of the N cows in order of seniority (the most senior cow being first). Each line contains Ai and Ti for one cow. The Ti‘s are positive integers each at most 10^4, and the Ai‘s are positive integers at most 10^9.

OUTPUT FORMAT (file convention2.out):

Please print the longest potential waiting time over all the cows.

SAMPLE INPUT:

5
25 3
105 30
20 50
10 17
100 10

SAMPLE OUTPUT:

10

In this example, we have 5 cows (numbered 1..5 according to their order in the input). Cow 4 is the first to arrive (at time 10), and before she can finish eating (at time 27) cows 1 and 3 both arrive. Since cow 1 has higher seniority, she gets to eat next, having waited 2 units of time beyond her arrival time. She finishes at time 30, and then cow 3 starts eating, having waited for 10 units of time beyond her starting time. After a gap where no cow eats, cow 5 arrives and then while she is eating cow 2 arrives, eating 5 units of time later. The cow who is delayed the most relative to her arrival time is cow 3.

Problem credits: Brian Dean

Because of how the previous problem was solved, I thought this might be a binary search question, too; I mean, I think you probably could make it a binary search, but it would be pretty unnecessary; this is just simple calculation.

I think the reason it is in silver is because of the time complexity, and you just can’t go through every singe cows comparing this and that, or you’ll reach time limit. You need to have a method to make this more simpler/faster, and I did this by using two priority queues with different rules (there are probably other ways that are a lot more effective, but I did this way). After that, its just simple calculation.

The time complexity of this method would be.. I have no idea. please comment if you know bc I have no idea how much does priority queues take.

Here’s my code:

//
//  main.cpp
//  Convention II
//
//  Created by Shin Lee on 03/01/2020.
//  Copyright © 2020 Shin Lee. All rights reserved.
//

#include <iostream>
#include <bits/stdc++.h>
struct cow {
    int id;
    int time;
    int dur;
};

struct cmp{
public:
    bool operator() (cow a, cow b)
    {
        return a.time != b.time ? a.time > b.time : a.id > b.id;
    }
};
struct cmp2{
public:
    bool operator() (cow a, cow b)
    {
        return a.id>b.id;
    }
};
int n;
using namespace std;
int main() {
    priority_queue<cow,vector<cow>, cmp> q;
    priority_queue<cow,vector<cow>, cmp2> p;
    cin >> n;
    for(int i=1;i<=n;i++){
        int a, b;
        cin >> a >> b;
        q.push({i,a,b});
    }
    
    int curtime = q.top().time + q.top().dur;
    q.pop();
    int wait = 0;
    /*
    while(!p.empty()){
        printf("%d %d %d\n",p.top().id, p.top().time, p.top().dur);
        p.pop();
    }
     */
    while(!q.empty()){
        
        
        while(!q.empty() and q.top().time<=curtime ){
            p.push(q.top());
            //printf("%d %d %d is included\n",f.id, f.time,f.dur);
            q.pop();
        }
        
        while(!p.empty()){
            //printf("initial curtime = %d, looking at %d %d %d\n",curtime,f.id, f.time, f.dur);
            int p_wait = curtime - p.top().time;
            wait=max(wait,p_wait);
            curtime+=p.top().dur;
            //printf("%d %d\n",curtime,wait);
            p.pop();
            while(!q.empty() and q.top().time<=curtime){
                p.push({q.top().id, q.top().time, q.top().dur});
                q.pop();
            }
        }
        
        if(!q.empty() and q.top().time>curtime){
            curtime = q.top().time + q.top().dur;
            q.pop();
        }
        
    }
    
    cout << wait;
    
    return 0;
}

Published by dz2701

Student Coder, interested in learning new Algorithms and new challenges!

Leave a Reply

Create your own website, free on WordPress.com.Create your own website.
Start free