Skip to Content

Convention II

Análisis oficial (C++) 

Debido a las cotas bajas de NN, podemos simular los eventos de forma naive. Ordenaremos por tiempo de llegada para procesar las vacas en orden y guardaremos las vacas que esperan en una cola de prioridad.

Implementación

Complejidad temporal: O(NlogN)\mathcal{O}(N \log N)

#include <algorithm> #include <array> #include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; int main() { freopen("convention2.in", "r", stdin); freopen("convention2.out", "w", stdout); using Cow = array<int, 3>; int cow_num; vector<Cow> cows; cin >> cow_num; for (int c = 0; c < cow_num; c++) { int start, duration; cin >> start >> duration; cows.push_back({c, start, duration}); } // ordenamos por tiempo de llegada sort(cows.begin(), cows.end(), [](const Cow &a, const Cow &b) { return a[1] < b[1]; }); int time = 0; int curr = 0; int longest_wait = 0; // ordenado por prioridad para que la de mayor antigüedad empiece a comer primero priority_queue<Cow, vector<Cow>, greater<Cow>> waiting; // mientras no hayamos procesado todas las vacas o todavía haya vacas esperando while (curr < cow_num || !waiting.empty()) { // esta vaca se puede procesar. if (curr < cow_num && cows[curr][1] <= time) { waiting.push(cows[curr]); curr++; // no hay vaca esperando, saltamos a la siguiente. } else if (waiting.empty()) { // fijamos el tiempo al de finalización de la siguiente vaca. time = cows[curr][1] + cows[curr][2]; curr++; } else { // procesamos la siguiente vaca Cow next = waiting.top(); longest_wait = max(longest_wait, time - next[1]); // fijamos el tiempo a cuando esta vaca termina time += next[2]; waiting.pop(); } } cout << longest_wait << endl; }
import java.io.*; import java.util.*; public class Convention2 { public static void main(String[] args) throws IOException { BufferedReader read = new BufferedReader(new FileReader("convention2.in")); int cowNum = Integer.parseInt(read.readLine()); Cow[] cows = new Cow[cowNum]; for (int c = 0; c < cowNum; c++) { StringTokenizer cow = new StringTokenizer(read.readLine()); int start = Integer.parseInt(cow.nextToken()); int duration = Integer.parseInt(cow.nextToken()); cows[c] = new Cow(c, start, duration); } // ordenamos por tiempo de llegada Arrays.sort(cows, Comparator.comparingInt(c -> c.start)); int time = 0; int curr = 0; int longestWait = 0; // ordenado por prioridad para que la de mayor antigüedad empiece a comer primero PriorityQueue<Cow> waiting = new PriorityQueue<>(); // mientras no hayamos procesado todas las vacas o todavía haya vacas esperando while (curr < cowNum || !waiting.isEmpty()) { // esta vaca se puede procesar if (curr < cowNum && cows[curr].start <= time) { waiting.add(cows[curr]); curr++; // no hay vaca esperando, saltamos a la siguiente. } else if (waiting.size() == 0) { // fijamos el tiempo al de finalización de la siguiente vaca. time = cows[curr].start + cows[curr].duration; curr++; } else { // procesamos la siguiente vaca Cow next = waiting.peek(); longestWait = Math.max(longestWait, time - next.start); // fijamos el tiempo a cuando esta vaca termina time += next.duration; waiting.poll(); } } PrintWriter written = new PrintWriter("convention2.out"); written.println(longestWait); written.close(); } static class Cow implements Comparable<Cow> { int seniority; int start; int duration; public Cow(int seniority, int start, int duration) { this.seniority = seniority; this.start = start; this.duration = duration; } @Override public int compareTo(Cow other) { return seniority - other.seniority; } }; }
import heapq cows = [] with open("convention2.in") as read: for c in range(int(read.readline())): start, duration = [int(i) for i in read.readline().split()] cows.append((c, start, duration)) # ordenamos por tiempo de llegada cows.sort(key=lambda c: c[1]) time = 0 curr = 0 longest_wait = 0 # ordenado por prioridad para que la de mayor antigüedad empiece a comer primero waiting = [] # mientras no hayamos procesado todas las vacas o todavía haya vacas esperando while curr < len(cows) or waiting: # esta vaca se puede procesar if curr < len(cows) and cows[curr][1] <= time: heapq.heappush(waiting, cows[curr]) curr += 1 # no hay vaca esperando, saltamos a la siguiente elif not waiting: # fijamos el tiempo al de finalización de la siguiente vaca time = cows[curr][1] + cows[curr][2] curr += 1 else: # procesamos la siguiente vaca next_cow = heapq.heappop(waiting) longest_wait = max(longest_wait, time - next_cow[1]) # fijamos el tiempo a cuando esta vaca termina time += next_cow[2] print(longest_wait, file=open("convention2.out", "w"))