-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: France-IOI Algorithmes Gloutons
- Loading branch information
1 parent
fed02cf
commit 9e4e47b
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
solutions/France-IOI/Niveau 5/Algorithmes Gloutons/Caméras de surveillance.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
46 changes: 46 additions & 0 deletions
46
solutions/France-IOI/Niveau 5/Algorithmes Gloutons/Chaîne de production.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
22 changes: 22 additions & 0 deletions
22
solutions/France-IOI/Niveau 5/Algorithmes Gloutons/Fête du cinéma.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |