AtCoder Regular Contest 102 D - All Your Paths are Different Lengths
阿新 • • 發佈:2018-09-05
ret include fio pll urn n) sin push fine
D - All Your Paths are Different Lengths
思路:
二進制構造
首先找到最大的t,使得2^t <= l
然後我們就能構造一種方法使得正好存在 0 到 2^t - 1 的路徑
方法是:對於節點 i 到 i + 1,添加兩條邊,一條邊權值是2^(i-1),一條邊權值是0
對於剩下的2^t 到 l-1的路徑,我們考慮倍增地求,每次添加一條節點 v 到 節點 n 的邊,邊的權值是 X ,新增的路徑是X 到 X + 2^(v-1) - 1
第一次的X是 2^t,之後每次倍增X增加 2^v,使得 X + 2^v <= l
代碼:
#pragmaGCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #defineULL unsigned LL #define pll pair<LL, LL> #define pii pair<int, int> #define piii pair<pii, int> #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head vector<piii> ans; int main() { int l, t, n; scanf("%d", &l); for (int i = 25; ; i--) { if((1<<i) <= l) { t = i; n = t+1; break; } } for (int i = 1; i <= t; i++) { ans.pb({{i, i+1}, 1<<i-1}); ans.pb({{i, i+1}, 0}); } int res = l - (1<<t), now = (1<<t); for (int i = n-1; i >= 1; i--) { if((1<<i-1) <= res) { res -= 1<<i-1; ans.pb({{i, n}, now}); now += 1<<i-1; } } printf("%d %d\n", n, (int)ans.size()); for (int i = 0; i < ans.size(); i++) printf("%d %d %d\n", ans[i].fi.fi, ans[i].fi.se, ans[i].se); return 0; }
AtCoder Regular Contest 102 D - All Your Paths are Different Lengths