Skip to content

Commit

Permalink
new: France-IOI Algorithmes Gloutons
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-besbes committed Dec 2, 2024
1 parent fed02cf commit 9e4e47b
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <bits/stdc++.h>
using namespace std;

template <class U>
void merge(set<U> &a, set<U> &b) {
for(auto &i: b) a.insert(i);
b.clear();
}

typedef tuple<int, int, int> edge;

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);

int n, a;
cin >> n >> a;
vector<set<tuple<int, int, int>>> adj(n, set<tuple<int, int, int>>());
for (int i = 0; i < a; i++) {
int a, b, w;
cin >> a >> b >> w;
a--, b--;
adj[a].insert(make_tuple(w, a, b));
adj[b].insert(make_tuple(w, b, a));
}

vector<bool> selected(n, false);
set<edge> mst;

int ans = 0, nb = 1;
selected[0] = true;
set<edge> q;
merge(q, adj[0]);

while (nb < n) {
int w, a, b;
auto e = *q.begin();
tie(w, a, b) = e;
q.erase(make_tuple(w, b, a));
q.erase(make_tuple(w, a, b));

if (selected[a] && selected[b]) continue;
if (!selected[a]) selected[a] = true, merge<edge>(q, adj[a]);
if (!selected[b]) selected[b] = true, merge<edge>(q, adj[b]);

mst.insert(e);
ans += w;
nb++;
}

cout << ans << "\n";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define all(x) x.begin(), x.end()

vector<int> solve(vector<int>& machines, vector<int>& jobs) {
sort(all(jobs));
int m = machines.size(), j = jobs.size();
vector<int> cum(m, 0), ans(j, INT_MAX);
for (int job = 0; job < j; job++) {
pii mini = make_pair(INT_MAX, -1);
for (int i = 0; i < m; i++)
mini = min(mini, make_pair(cum[i] + machines[i], i));
ans[job] = mini.first;
cum[mini.second] = mini.first;
}
sort(all(ans));
for (int job = 0; job < j; job++) ans[job] += jobs[j - job - 1];
return ans;
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);

int n;
cin >> n;
int A;
cin >> A;
vector<int> a(A);
for (auto& i : a) cin >> i;
int B;
cin >> B;
vector<int> b(B);
for (auto& i : b) cin >> i;

vector<int> input(n, 0);
auto output1 = solve(a, input);
auto ans1 = *max_element(all(output1));
cout << ans1 << "\n";

auto output2 = solve(b, output1);
auto ans2 = *max_element(all(output2));
cout << ans2 << "\n";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "bits/stdc++.h"
using namespace std;

int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);

int n;
cin >> n;
vector<pair<int, int>> s(n);
for (auto &item : s) cin >> item.first >> item.second;

sort(s.begin(), s.end(),
[](pair<int, int> x, pair<int, int> y) { return x.second < y.second; });

auto ans = 0, cur = 0;
for (int i = 0; i < n; i++) {
if (s[i].first >= cur) cur = s[i].second + 1, ans++;
}

cout << ans << "\n";
}

0 comments on commit 9e4e47b

Please sign in to comment.