QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#616938 | #6308. Magic | fryan | WA | 0ms | 3596kb | C++20 | 4.6kb | 2024-10-06 13:08:09 | 2024-10-06 13:08:10 |
Judging History
answer
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
// Constants
const int MXN = 2e4 + 5;
// Interval structure for clarity
struct Interval {
int start, end;
};
// Hopcroft-Karp Algorithm for Maximum Bipartite Matching
struct HopcroftKarp {
int n, m;
vector<vector<int>> adj;
vector<int> pair_u, pair_v, dist;
HopcroftKarp(int size_u, int size_v) : n(size_u), m(size_v), adj(n + 1), pair_u(n + 1, 0), pair_v(m + 1, 0), dist(n + 1, 0) {}
void add_edge(int u, int v) {
adj[u].push_back(v);
}
bool bfs() {
queue<int> q;
for(int u = 1; u <= n; u++) {
if(pair_u[u] == 0) {
dist[u] = 0;
q.push(u);
}
else {
dist[u] = INT32_MAX;
}
}
dist[0] = INT32_MAX;
while(!q.empty()) {
int u = q.front(); q.pop();
if(u != 0) {
for(auto &v : adj[u]) {
if(dist[pair_v[v]] == INT32_MAX) {
dist[pair_v[v]] = dist[u] + 1;
q.push(pair_v[v]);
}
}
}
}
return dist[0] != INT32_MAX;
}
bool dfs(int u) {
if(u != 0) {
for(auto &v : adj[u]) {
if(dist[pair_v[v]] == dist[u] + 1) {
if(dfs(pair_v[v])) {
pair_u[u] = v;
pair_v[v] = u;
return true;
}
}
}
dist[u] = INT32_MAX;
return false;
}
return true;
}
int max_matching() {
int matching = 0;
while(bfs()) {
for(int u = 1; u <= n; u++) {
if(pair_u[u] == 0) {
if(dfs(u)) {
matching++;
}
}
}
}
return matching;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<Interval> intervals(n);
for(auto &it : intervals) {
cin >> it.start >> it.end;
}
// Sort intervals by start time
sort(intervals.begin(), intervals.end(), [&](const Interval &a, const Interval &b) {
if(a.start != b.start)
return a.start < b.start;
return a.end < b.end;
});
// Create bipartition
// Left nodes: interval starts
// Right nodes: interval ends
// Assign unique IDs to starts and ends
// This assumes that starts and ends can be up to 1e4, adjust if necessary
// Alternatively, compress coordinates
// Here, we map each unique start and end to unique IDs
// Coordinate compression
vector<int> coords;
for(auto &it : intervals) {
coords.push_back(it.start);
coords.push_back(it.end);
}
sort(coords.begin(), coords.end());
coords.erase(unique(coords.begin(), coords.end()), coords.end());
auto get_id = [&](int x) -> int {
return lower_bound(coords.begin(), coords.end(), x) - coords.begin() + 1;
};
// Determine sizes for bipartition
int size_left = n;
int size_right = n; // At most n unique ends
HopcroftKarp hk(size_left, size_right);
// Build adjacency list efficiently
// For each interval, connect to overlapping intervals using binary search
// based on the problem's specific condition
// Adjust the condition as per your problem statement
for(int i = 0; i < n; i++) {
// Example condition: interval i can be connected to interval j if they overlap in a specific way
for(int j = 0; j < n; j++) {
if(i == j) continue;
if(intervals[i].start < intervals[j].start && intervals[i].end > intervals[j].start && intervals[j].end > intervals[i].end) {
hk.add_edge(i + 1, j + 1);
}
}
}
int maxMatch = hk.max_matching();
// Assuming you need to output the number of unmatched nodes
// Depending on the problem, adjust accordingly
// Here, let's assume it's n - maxMatch
cout << (n - maxMatch);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3596kb
input:
5 2 3 6 7 1 9 5 10 4 8
output:
4
result:
wrong answer 1st numbers differ - expected: '9', found: '4'