[fastest] get the most bonus.
Knowledge points: double pointer
(Title address) [https://www.nowcoder.com/practice/247f7bd088764aefa7474cff274489095? TPID = 98 & TQD = 32839 & RP = 1 & Ru = / Ta / 2019test & qru = / Ta / 2019test / question ranking]
Solution idea: the relative position of the red packet array is required to remain unchanged, and it is divided into three parts. The first part and the third part are added and equal, and the sum is the largest. You can add one to the previous sequence cumulative sum, and move two pointers. If the previous sequence cumulative sum is less than the subsequent cumulative sum, the previous sequence pointer will add one. Otherwise, the subsequent cumulative sum will decrease one. If it is equal, both pointers will move forward one step.
#include <iostream> #include <algorithm> using namespace std; int main() { long n; cin >> n; long long nums[n]; for (int i = 0; i < n; i++) cin >> nums[i]; //Double pointer p1, p2 int p1 = 0; int p2 = n - 1; long long forward[n]; long long backward[n]; forward[0] = nums[0]; backward[n-1] = nums[n-1]; for (long i = 1; i < n - 1; i++) { //Sum of previous and subsequent accumulations forward[i] = forward[i-1] + nums[i]; backward[n - i - 1] = backward[n - i] + nums[n - i - 1]; } long long res = -1; while (p1 < p2) { //Double pointer if (forward[p1] < backward[p2]) p1++; else if (backward[p2] < forward[p1]) p2--; else { res = forward[p1]; p1++; p2--; } } if (res > 0) cout << res << endl; else cout << 0 << endl; }
[fast] convert two full tree into summation tree.
Knowledge points: recursion, tree traversal, given middle order traversal and first order traversal to reconstruct the tree
Title address
Solutions:
1. Direct tree reconstruction
2. Recursive summation
3. Middle order traversal return result
However, the topic is full of binary trees, which can be reconstructed by traversing the middle order, and can be summed in situ on the middle order array, saving space
#include <bits/stdc++.h> using namespace std; int dfs(vector<int>& inorder, int start, int end) { //Full binary tree only needs middle order traversal if (start == end) { int tmp = inorder[start]; inorder[start] = 0; return tmp; } int mid = (end - start) / 2 + start; int root = inorder[mid]; int left = dfs(inorder, start, mid - 1); int right = dfs(inorder, mid + 1, end); inorder[mid] = left + right; return root + inorder[mid]; } int main() { vector<int> nums; vector<int> preorder; vector<int> inorder; vector<int> res; int num; while (cin >> num) { nums.push_back(num); } for (int i = 0; i < nums.size() / 2; i++) { preorder.push_back(nums[i]); inorder.push_back(nums[i+nums.size() / 2]); } dfs(inorder, 0, inorder.size()-1); copy(inorder.begin(), inorder.end(), ostream_iterator<int>(cout, " ")); }
[quick hands] building blocks
I haven't come up with a way to solve the problem yet. I'm waiting for you to understand and fill in the hole...
The abyss of magic
Knowledge points: dynamic planning
Title address
Solution idea: a typical dynamic planning problem with ladder climbing
#include <iostream> #include <algorithm> using namespace std; int main() { int M; cin >> M; int N = -1; int steps[M]; for (int i = 0; i < M; i++) { cin >> steps[i]; N = max(N, steps[i]); } long long dp[N+1]; long long mod = 1000000003; for (int i = 0; i <= N; i++) dp[i] = 0; dp[0] = 1; dp[1] = 1; for (long long i = 2; i <= (long long)N; i++) { long long fromDiff = 1; // dp[i] is equal to the sum of all DPS [J] that can reach I while (i - fromDiff >= 0) { dp[i] += dp[i-fromDiff]; dp[i] %= mod; fromDiff *= 2; } } for (int step: steps) cout << dp[step] << endl; }
[fast] fickle companion
Knowledge points: dynamic planning
Waiting to fill in the hole, the biggest and the same problem with m strings, not understood yet...
[fast] string normalization
Title address
Knowledge points: map
Solution: whatever you do
#include <iostream> #include <vector> using namespace std; int main() { vector<char> chars; char c; while(cin >> c) { if (c != '\n') chars.push_back(c); } int freq[26] = {0}; for (char ch: chars) { freq[ch - 'a']++; } for (int i = 0; i < 26; i++) { if (freq[i] > 0) cout << (char)(i + 'a') << freq[i]; } }
[fast] string sorting
Title address
Knowledge points: strings
Solution idea: initialize string array, read in array with cin, read the last six digits with string class built-in substr method, call strNum or stdio to convert to int type
#include <iostream> #include <algorithm> #include <string> #include <bits/stdc++.h> using namespace std; int strToNum(string str) { int num = 0; for (int i = 0; i < str.size(); i++) { num = num * 10 + (str[i] - '0'); } return num; } int main() { int M; cin >> M; string s[M]; int nums[M]; for (int i = 0; i < M; i++) { nums[i] = 0; cin>>s[i]; string str = s[i].substr(s[i].size() - 6, s[i].size() - 1); nums[i]=strToNum(str); } sort(nums, nums + M); for (int i = 0; i < M; i++) cout << nums[i] << endl; }