Skip to content

Commit 9e4e47b

Browse files
committed
new: France-IOI Algorithmes Gloutons
1 parent fed02cf commit 9e4e47b

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
template <class U>
5+
void merge(set<U> &a, set<U> &b) {
6+
for(auto &i: b) a.insert(i);
7+
b.clear();
8+
}
9+
10+
typedef tuple<int, int, int> edge;
11+
12+
int main() {
13+
ios::sync_with_stdio(false);
14+
cin.tie(0);
15+
cout.tie(0);
16+
17+
int n, a;
18+
cin >> n >> a;
19+
vector<set<tuple<int, int, int>>> adj(n, set<tuple<int, int, int>>());
20+
for (int i = 0; i < a; i++) {
21+
int a, b, w;
22+
cin >> a >> b >> w;
23+
a--, b--;
24+
adj[a].insert(make_tuple(w, a, b));
25+
adj[b].insert(make_tuple(w, b, a));
26+
}
27+
28+
vector<bool> selected(n, false);
29+
set<edge> mst;
30+
31+
int ans = 0, nb = 1;
32+
selected[0] = true;
33+
set<edge> q;
34+
merge(q, adj[0]);
35+
36+
while (nb < n) {
37+
int w, a, b;
38+
auto e = *q.begin();
39+
tie(w, a, b) = e;
40+
q.erase(make_tuple(w, b, a));
41+
q.erase(make_tuple(w, a, b));
42+
43+
if (selected[a] && selected[b]) continue;
44+
if (!selected[a]) selected[a] = true, merge<edge>(q, adj[a]);
45+
if (!selected[b]) selected[b] = true, merge<edge>(q, adj[b]);
46+
47+
mst.insert(e);
48+
ans += w;
49+
nb++;
50+
}
51+
52+
cout << ans << "\n";
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define pii pair<int, int>
4+
#define all(x) x.begin(), x.end()
5+
6+
vector<int> solve(vector<int>& machines, vector<int>& jobs) {
7+
sort(all(jobs));
8+
int m = machines.size(), j = jobs.size();
9+
vector<int> cum(m, 0), ans(j, INT_MAX);
10+
for (int job = 0; job < j; job++) {
11+
pii mini = make_pair(INT_MAX, -1);
12+
for (int i = 0; i < m; i++)
13+
mini = min(mini, make_pair(cum[i] + machines[i], i));
14+
ans[job] = mini.first;
15+
cum[mini.second] = mini.first;
16+
}
17+
sort(all(ans));
18+
for (int job = 0; job < j; job++) ans[job] += jobs[j - job - 1];
19+
return ans;
20+
}
21+
22+
int main() {
23+
ios::sync_with_stdio(false);
24+
cin.tie(0);
25+
cout.tie(0);
26+
27+
int n;
28+
cin >> n;
29+
int A;
30+
cin >> A;
31+
vector<int> a(A);
32+
for (auto& i : a) cin >> i;
33+
int B;
34+
cin >> B;
35+
vector<int> b(B);
36+
for (auto& i : b) cin >> i;
37+
38+
vector<int> input(n, 0);
39+
auto output1 = solve(a, input);
40+
auto ans1 = *max_element(all(output1));
41+
cout << ans1 << "\n";
42+
43+
auto output2 = solve(b, output1);
44+
auto ans2 = *max_element(all(output2));
45+
cout << ans2 << "\n";
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "bits/stdc++.h"
2+
using namespace std;
3+
4+
int main() {
5+
ios_base::sync_with_stdio(false);
6+
cin.tie(nullptr);
7+
8+
int n;
9+
cin >> n;
10+
vector<pair<int, int>> s(n);
11+
for (auto &item : s) cin >> item.first >> item.second;
12+
13+
sort(s.begin(), s.end(),
14+
[](pair<int, int> x, pair<int, int> y) { return x.second < y.second; });
15+
16+
auto ans = 0, cur = 0;
17+
for (int i = 0; i < n; i++) {
18+
if (s[i].first >= cur) cur = s[i].second + 1, ans++;
19+
}
20+
21+
cout << ans << "\n";
22+
}

0 commit comments

Comments
 (0)