From 366e5454b54f6c40ea91f8fcf9a45d450b204f0a Mon Sep 17 00:00:00 2001 From: Amr Keleg Date: Sat, 20 Oct 2018 14:53:42 +0200 Subject: [PATCH 1/2] Oct 2018 ECPC2017 D --- Codeforces/CF101840-GYM-D.cpp | 128 ++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 Codeforces/CF101840-GYM-D.cpp diff --git a/Codeforces/CF101840-GYM-D.cpp b/Codeforces/CF101840-GYM-D.cpp new file mode 100644 index 0000000..d2522c0 --- /dev/null +++ b/Codeforces/CF101840-GYM-D.cpp @@ -0,0 +1,128 @@ +// AC +#include +using namespace std; + +int a[100000]; +int vis[100000] = {}; +int t; +vector > divisors(100001); + +vector parent; + +int find(int node) +{ + if (node == parent[node]) + return node; + return parent[node] = find(parent[node]); +} + +void merge(int a, int b) +{ + int pa = find(a); + int pb = find(b); + if (pa==pb)return; + // merge + parent[pa] = pb; +} + +int main() +{ + freopen("dream.in", "r", stdin); + int T; + cin>>T; + for (t=1;t<=T;t++){ + int n; + cin>>n; + parent = vector (n); + for (int i=0;i>a[i]; + parent[i] = i; + for (long long int div=1; div*div<=a[i]; div++) + { + if (a[i] % div == 0) + { + if (div*div == a[i]) + { + divisors[div].push(i); + } + else + { + divisors[div].push(i); + divisors[a[i]/div].push(i); + } + } + } + } + long long int ans = 0; + if (n!=1) + { + for (int div=100000; div>=1; div--){ + queue & q = divisors[div]; + if (q.empty()) + { + continue; + } + if (q.size()==1) + { + q.pop(); + continue; + } + else + { + int n_new = 0; + int gcd_n = q.size(); + // add edges for finding components + int root = -1; + queue new_nodes; + queue old_nodes; + while(!q.empty()) + { + int node = q.front(); + q.pop(); + if (vis[node] != t) + { + vis[node] = t; + new_nodes.push(node); + n_new++; + } + else + { + old_nodes.push(node); + root = node; + } + } + if (gcd_n == n_new){ + root = new_nodes.front(); + new_nodes.pop(); + } + while(!new_nodes.empty()){ + int node = new_nodes.front(); + new_nodes.pop(); + ans += div; + merge(node, root); + } + + if (! old_nodes.empty()) + { + int node_first = old_nodes.front(); + old_nodes.pop(); + while(!old_nodes.empty()) + { + int node_second = old_nodes.front(); + old_nodes.pop(); + int pf = find(node_first); + int ps = find(node_second); + if (pf != ps) + { + //merge + merge(node_second, node_first); + ans+= div; + } + } + } + } + } + } + printf("Case %d: %lld\n", t, ans); + } +} From 92be362179e3fa3188c0fd74bbfb9cc53facbd6b Mon Sep 17 00:00:00 2001 From: Amr Keleg Date: Tue, 29 Jan 2019 19:19:29 +0200 Subject: [PATCH 2/2] Jan 2019 ECPC2017 K, L --- Codeforces/CF101840-GYM-K.py | 14 ++++++++++++++ Codeforces/CF101840-GYM-L.py | 10 ++++++++++ 2 files changed, 24 insertions(+) create mode 100644 Codeforces/CF101840-GYM-K.py create mode 100644 Codeforces/CF101840-GYM-L.py diff --git a/Codeforces/CF101840-GYM-K.py b/Codeforces/CF101840-GYM-K.py new file mode 100644 index 0000000..826e057 --- /dev/null +++ b/Codeforces/CF101840-GYM-K.py @@ -0,0 +1,14 @@ +''' +AC + +Notes: +- Max no of doals is at most = no of bodies +- The second choice isn't good (First choice is always better than it) +- It's better to use just one eye and one mouth than using two eyes +''' + +with open('katryoshka.in', 'r') as f: + T = int(f.readline()) + for t in range(1, T+1): + n, m, k = [int(no) for no in f.readline().split()] + print('Case {}: {}'.format(t, min(k, n if m>=n else m + (n-m)//2))) diff --git a/Codeforces/CF101840-GYM-L.py b/Codeforces/CF101840-GYM-L.py new file mode 100644 index 0000000..bf37170 --- /dev/null +++ b/Codeforces/CF101840-GYM-L.py @@ -0,0 +1,10 @@ +''' +AC + +No of matches = No of teams - 1 +''' +with open('lazy.in', 'r') as f: + T = int(f.readline()) + for i in range(T): + n_teams = int(f.readline()) + print('Case {}: {}'.format(i+1, n_teams-1))