A. Vladik and flights (water)
Test instructions: From a position to B position, ask the shortest cost figured out is the flood problem
1#include <bits/stdc++.h>2 using namespacestd;3 Const intINF =0x3f3f3f3f;4 Const intMAXN =100000+5;5 CharS[MAXN];6 intMain ()7 {8 intN, a, B;9scanf"%d%d%d", &n, &a, &b);Tenscanf"%s", S +1); One if(S[a] = =S[b]) APuts"0"); - Else -Puts"1"); the return 0; -}
View Code
B. Chloe and the sequence (construction? )
Test instructions: give N and K, according to its rules constitute a series of numbers, ask K is which number
Idea: My approach is to use recursion to solve the inverse of the construction rules.
1#include <bits/stdc++.h>2 using namespacestd;3typedef unsignedLong LongULL;4typedefLong LongLL;5 Const intINF =0x3f3f3f3f;6 Const intMAXN =10000+5;7 intOK;8 voidSolve (ULL N, ULL k)9 {Ten if(N <1)return; OneULL mid = (((ULL)1<< N) +1) /2; A if(k = =mid) - { -cout << N <<Endl; theOK =1; - } - Else if(K <mid) - { +Solve (N-1, k); - } + Else if(k > (n +1) /2) A { atSolve (N-1Kmid); - } - } - - intMain () - { in ULL N, K; -CIN >> N >>K; to solve (n, k); + return 0; -}
View Code
C. Vladik and Fractions (construction)
Test instructions: Known n, asking if there is an integer x, y, Z makes 2/n = 1/x + 1/y + 1/z set up.
Idea: It should be a construction, directly push the formula seconds.
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 Const intINF =0x3f3f3f3f;5 Const intMAXN =10000+5;6 7 intMain ()8 {9 intN;Ten intOK =0; Onescanf"%d", &n); A if(n = =1) - { -Puts"-1"); the } - Else - { -cout << N <<" "<< n +1<<" "<< n +1) <<Endl; + } - + return 0; A}
View Code
D. Chloe and pleasant prizes (tree DP)
Test instructions: Probably a tree above, select two disjoint subtree to make the point right and maximum
Idea: Tree DP entry question. attention!!! Initialize!!! The initialization of Vmax is-inf, because you want to select a subtree instead of selecting some of the points.
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 Const intMAXN =200000+5;5 ConstLL INF =0x3f3f3f3f3f3f3f3f;6 intA[MAXN];7 LL ans;8vector<int>G[MAXN];9 LL SUM[MAXN], VAL_MAX[MAXN];Ten One voidDfsintCurintFA) A { -Sum[cur] =A[cur]; -Val_max[cur] =-INF; theVector<ll>temp; - for(Auto to:g[cur]) - { - if(to = = FA)Continue; + dfs (to, cur); -Val_max[cur] =Max (Val_max[cur], val_max[to]); +Sum[cur] + =Sum[to]; A Temp.push_back (Val_max[to]); at - } -Sort (Temp.begin (), Temp.end (), greater<ll>()); - if(Temp.size () >=2) - { -ans = max (ans, temp[0] + temp[1]); in } -Val_max[cur] =Max (Val_max[cur], sum[cur]); to + } - the intMain () * { $ intN;Panax Notoginsengscanf"%d", &n); - for(inti =1; I <= N; i++) the { +scanf"%d", &a[i]); A } the for(inti =0; I < n-1; i++) + { - intu, v; $scanf"%d%d", &u, &v); $ G[u].push_back (v); - g[v].push_back (u); - } the -Ans = -1ll << the;WuyiDfs1,0); the - if(ans = = -1ll << the) WuPuts"Impossible"); - Else Aboutcout << ans <<Endl; $ return 0; -}
View Code
Codeforces Round #384 (Div. 2)