Groundhog took a math class. In this class, his math teacher said:
Any positive integer can be represented by the power of 2. For example:137=2^7^+2^3^+2^0^
And powers are expressed in parentheses.That is ,a(b){a(b)}a(b) stands for ab{a^b}ab.Therefore,137 can be expressed as 137=2(7)+2(3)+2(0).
Further more,for 7=2^2^+2+2^0^(2^1^is expressed with 2),3=2+2^0^,137 can be finally expressed as 137=2(2(2)+2+2(0))+2(2+2(0))+2(0).
Another example:1315=2^10^+2^8^+2^5^+2+1=2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0).
Groundhog feels amazing and wants you to write a program to simulate the above content.You need to read in an expression that is a power of 2 and calculate its value.
输入描述:
1
Given astring, indicating the power representation.
Today, ZLZX has a mysterious case: Orange lost his down jacket hanging in his dorm room. Under the expectations of everyone, detective Groundhog took his small spoon of the artifact and started the journey to solve the case.
After an in-depth investigation of the northernmost mysterious room on each floor, Groundhog discovered n{n}n mysterious numbers. As long as the clues conveyed by these numbers are deciphered, he can reveal the truth of the matter. The deciphering method is: using these numbers to generate two positive integers without leading zeros, and minimizing the product of these two positive integers is the final clue.
Then Groundhog wants to know: What is the smallest product?
As he continued to investigate in the room west of the new building, he gave you the question.
Concise meaning:Given n numbers between 0 and 9, use them to make two positive integers without leading zeros to minimize the product.
输入描述:
1 2 3
The first lineofinputis a single integer T,the number of test cases. Foreachsetof data: Each test case begins with a single integer n , the count of numbers.The next line are n{n}n numbers.
输出描述:
1
For eachsetof Case, aninteger is output, representing the smallest product.
usingnamespace std; constint N = 1e5 + 10; int a[N]; vector<int> A;
vector<int> mul(vector<int> A, int B){ vector<int> C; int t = 0; for (int i = 0 ; i < A.size() || t ; i++) { if (i < A.size()) t += A[i] * B; C.push_back(t % 10); t /= 10; }
voidsolve(){ A.clear(); int n, res = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] == 0) res++; }
if (res == n - 1 || res == n) { cout << "0\n"; return; } sort(a + 1, a + n + 1);
int b = a[res + 1]; swap(a[res + 1], a[1]); swap(a[res + 2], a[2]); for (int i = n; i >= 2; i--) A.push_back(a[i]); auto D = mul(A, b); for (int i = D.size() - 1; i >= 0; i--) cout << D[i]; cout << '\n'; } intmain() { int T; cin >> T; while (T--) { solve(); } }
Groundhog finds that Apple seems to be less intimate than before.
He is very distressed for this.After pondering for a long time, Groundhog finds that he looks too dowdy.So, Groundhog decided to improve a little.
Because Groundhog is lazy, he only lists the clothes that can be worn for the next n{n}n days. On the ith{i^{th}}ith day, the jth{j^{th}}jth clothes have a dowdiness ai,ja_{i,j}ai,j.On each day,he will choose one of the clothes and wear it.
And Groundhog should choose m{m}m days from n{n}n days to go out with Apple.
Groundhog wants to know the minimum difference between the maximum dowdiness and the minimum dowdiness in m{m}m days when Groundhog’s choice is optimal.
Simplified: You should choose n clothes for each day and then choose m clothes from those n clothes, and the problem is to calculate the minimum difference between the maximum dowdiness and the minimum dowdiness.
输入描述:
1 2 3
The first line contains two integers n{n}n and m{m}m.
Then n lines follow,each line contains a integer kik_iki,represents thenumberofthe clothes that can be worn on ith{i^{th}}ith day.Then kik_iki integers ai,j follow.
Apple will pay attention to Groundhog's clothes on day 1, 3, and 4 ,Groundhog will wear clothes with dowdiness of 3, 2, and 1on day 1, 3, and 4,and the difference is 2.
备注:
1
1⩽ai,j⩽10^91⩽ai,j⩽109,1≤n⩽106,1≤m⩽n,the sum of clothes ⩽2⋅106.ki≥1
Groundhog was especially careful after the outbreak,so he put on his mask in the 1st bedroom early,and then walked on the way to the nth dormitory to play with Orange. There are n dormitories in ZLZX, which are connected by n−1 corridors. Each dormitory can be reached to each other.The length of each corridor is 1.The walking speed of Groundhog is 1 m/s.
At that moment the bad news came: After Groundhog set out for t{t}t seconds,Orange took his temperature, and it turned out to be 41℃ !!! In addition to grief and indignation, Orange decided to run to Groundhog, to tell him the news at the speed of 2 m/s.
Groundhog had to run, of course, but he was running too slow at 1 m/s . As he ran, he had an idea: if he ran with the best strategy, how soon would Orange catch up with him? Define that every second Groundhog moves and then Orange moves again. Groundhog can choose to stay put. Groundhog would have solved that, of course, but he is running away now, so he give it to you, the smartest one.
输入描述:
1 2
The firstlinecontainstwo integers n,t。 The next n−1lines,eachlinecontainstwo integers x,y, indicating there is a corridor between the xth dormitory andthe yth dormitory.
输出描述:
1
An integer, indicating the latest timefor Orange tocatch Groundhog.
After t seconds, Groundhog is in the 5th dormitory and Orange is in the 7th dormitory.At this point, the best way for Groundhog istogoto the 2nd dormitory or the 6th6^{th}6th dormitory.But wherever he goes, he will be immediately caught by Orange.
备注:
1
1≤n≤105,1≤t≤n−1,1≤x,y≤n
思路
首先分析一下題目:有一棵树,A在1,B在2,A的移动速度是1边/s,B的是2边/s(可以选择走1边),前 t s A 在不断走向B,B不动。前 t s A在不断走向B,B是不动的,之后B就要开始移动逃离 A 了,求A最晚被追到的时间。
一棵树的两个结点之间的路径的基本是唯一的。所以可以把 B 为根,用LCA的ST倍增板子求A最后到达的位置。随后我们就要开始逃离,考虑A,B到达每一个点的情况,考虑A到达每个点所需要的时间,对于这个点,取两个时间中的较小值,取 A 和 B第 1 次到这个点的时间。然后从 A 出发寻找最大的二者到达时间相等的点。
usingnamespace std; typedef pair<int, int> PII; constint N = 1e5 + 10; int n, t; vector<int> P[N]; int fa[N][21]; bool st[N]; int ans = 0; int dist1[N], dist2[N];
voiddfs(int root, int place){ fa[root][0] = place; for (int i = 1; i <= 20; i++) fa[root][i] = fa[fa[root][i - 1]][i - 1]; for (auto &v : P[root]) { if (v != place) dfs(v, root); } }
intLCA(int u, int k){ int bit = 0; while (k) { if (k & 1) u = fa[u][bit]; k >>= 1; bit++; } return u; }
while (q.size()) { auto tt = q.front(); q.pop(); int k = tt.first, distance = tt.second; st[k] = true; for (int kkk: P[k]) { if (st[kkk]) continue; dist[kkk] = distance + 1; q.push({kkk, distance + 1}); } } }
voidbfs2(int kk){ memset(st, false, sizeof st); queue<int> q; q.push(kk); while (q.size()) { int k = q.front(); q.pop(); st[k] = true; ans = max(ans, max((dist2[k] + 1) / 2, dist1[k])); if ((dist2[k] + 1) / 2 <= dist1[k]) continue; for (int kkk : P[k]) { if (st[kkk]) continue; q.push(kkk); } } }
intmain() { ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); cin >> n >> t; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; P[a].push_back(b); P[b].push_back(a); } // 1、下面利用LCA的DFS写法寻找到我们最终到达的结点位置 dfs(n, 0);