Efficient Port Traffic Analysis with Sliding Window Technique

Problem Analysis

Given n sequences (n ≤ 1e5) where each contains:

  • Timestamp t (strict increasing)
  • Count k (k ≤ 3e5)
  • k integers

Compute for each sequence x the count of unique integers across all sequences where (tx - t) < 86400 (24 hours in seconds).

Constraints

Partial Score (70pts):

  • 1 ≤ n ≤ 100
  • Total k ≤ 100
  • Integer values ≤ 100
  • Timestamps ≤ 86400

Full Score (100pts):

  • 1 ≤ n ≤ 1e5
  • Total k ≤ 3e5
  • Integer values ≤ 1e5
  • Timestamps ≤ 1e9

70-Point Solutino

Direct implemantation with array-based deduplication:

#include <iostream>
#include <cstring>
using namespace std;

const int MAX = 1e5 + 10;
int records[MAX][100], counts[MAX], times[MAX];
bool active[MAX], seen[MAX];

int main() {
    int n;
    cin >> n;
    
    for (int i = 1; i <= n; i++) {
        cin >> times[i] >> counts[i];
        for (int j = 1; j <= counts[i]; j++) {
            cin >> records[i][j];
        }
    }
    
    for (int curr = 1; curr <= n; curr++) {
        memset(active, 0, sizeof(active));
        
        // Mark sequences within 24-hour window
        for (int i = curr; i >= 1; i--) {
            if (times[curr] - times[i] < 86400) {
                active[i] = true;
            }
        }
        
        memset(seen, 0, sizeof(seen));
        int unique = 0;
        
        // Count unique values
        for (int i = 1; i <= curr; i++) {
            if (active[i]) {
                for (int j = 1; j <= counts[i]; j++) {
                    int val = records[i][j];
                    if (!seen[val]) {
                        seen[val] = true;
                        unique++;
                    }
                }
            }
        }
        
        cout << unique << endl;
    }
    return 0;
}

100-Point Optimized Solution

Using queue and frequency counting with O(1) updates:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct Event {
    int timestamp;
    vector<int> passengers;
};

int freq[100010] = {0};
int distinct = 0;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    cin >> n;
    queue<Event> window;
    
    for (int i = 0; i < n; i++) {
        int t, k;
        cin >> t >> k;
        Event current{t, {}};
        
        while (k--) {
            int passenger;
            cin >> passenger;
            current.passengers.push_back(passenger);
            if (freq[passenger]++ == 0) {
                distinct++;
            }
        }
        
        window.push(current);
        
        // Remove expired events
        while (!window.empty()) {
            Event oldest = window.front();
            if (t - oldest.timestamp >= 86400) {
                for (int p : oldest.passengers) {
                    if (--freq[p] == 0) {
                        distinct--;
                    }
                }
                window.pop();
            } else {
                break;
            }
        }
        
        cout << distinct << endl;
    }
    return 0;
}

Key optimizations:

  1. Sliding window technique with queue
  2. O(1) frequency updates
  3. Constant-time uniqueness tracking

Tags: algorithm sliding-window Queue Optimization

Posted on Wed, 28 Oct 2026 16:48:53 +0000 by andychamberlainuk