I Dichotomous basic questions
Integer bisection -- ordered sequence query number
Given an integer array with a length of nn , arranged in ascending order, and qq , queries.
For each query, the start and end positions of an element {kk} are returned (the positions are counted from {00}).
If the element does not exist in the array, - 1 - 1 is returned.
Input format
The first line contains the integers {nn} and} qq, indicating the length of the array and the number of queries.
The second line contains ∼ nn integers (all in the range of ∼ 100001 ∼ 10000), representing the complete array.
Next, there are # qq # lines. Each line contains an integer # kk, representing a query element.
Output format
There are {qq} lines in total, and each line contains two integers, indicating the starting position and ending position of the element.
If the element does not exist in the array, - 1 - 1 is returned.
Data range
1≤n≤1000001≤n≤100000
1≤q≤100001≤q≤10000
1≤k≤100001≤k≤10000
Input example:
6 3 1 2 2 3 3 4 3 4 5
Output example:
3 4 5 5 -1 -1
code:
#include<iostream> using namespace std; const int N = 100020; int q[N]; int main() { int n, pp; cin >> n >> pp; for (int i = 0; i < n; i++) cin >> q[i]; while (pp--) { int k; cin >> k; int l = 0, r = n - 1; while (l < r) { int mid = l + r >> 1; //Put it in the while loop if (q[mid] >= k) r = mid; else l = mid + 1; } if (q[l] != k)cout << "-1 -1" << endl; else { cout << l << " "; l = 0, r = n - 1; while (l < r) { int mid = l + r + 1 >> 1; //Put it in the while loop if (q[mid] <= k) l = mid; else r = mid - 1; } cout << l << endl; } } return 0; }
Real number dichotomy -- root of equation
Given a floating point number {nn, find its cubic root.
Input format
A total of one line, including a floating point number {nn.
Output format
There is one line, including a floating point number, which represents the solution of the problem.
Note that the result retains 66 ¢ decimal places.
Data range
−10000≤n≤10000−10000≤n≤10000
Input example:
1000.00
Output example:
10.000000
code:
#include<iostream> using namespace std; int main() { double n; cin >> n; double l = - 100 , r = 100 ; while ((r - l) > 1e-8) { double ans = (l + r)/2; if (ans * ans * ans > n) r = ans; else l = ans; } printf("%.6lf", l ); }
II Binary combination greed
Topic 1
Title Description
The annual "stone jumping" competition is starting again!
The competition will be held in a straight river with some huge rocks. The organizing committee has selected two rocks as the starting point and ending point of the competition. Between the start point and the end point, there are {N rocks (excluding the rocks from the start point and the end point). During the competition, the contestants will start from the starting point and jump to the adjacent rocks with each step until they reach the end.
In order to improve the difficulty of the competition, the organizing committee plans to remove some rocks to make the shortest jumping distance as long as possible. Due to budget constraints, the organizing committee can remove at most M rocks from between the starting point and the end point (the rocks from the starting point and the end point cannot be removed).
Enter description
The first line of the input file contains three integers L, N, ML, N and M, which respectively represent the distance from the start point to the end point, the number of rocks between the start point and the end point, and the number of rocks removed by the organizing committee.
Next, line # NN, one integer per line, and the integer # D in line # ii_ I (0 < D_i < L) di (0 < Di < L) represents the distance between the , ii , rock and the starting point. These rocks are given in the order of the distance from the starting point from small to large, and there will be no two rocks in the same position.
Where, 0 \leq M \leq N \leq 5 \times 10^4, 1 \leq L \leq 10^90 ≤ M ≤ N ≤ 5 × 104,1≤L≤109.
Output description
The output contains only one integer, the maximum value of the shortest jump distance.
sample input
25 5 2 2 11 14 17 21
sample output
4
code:
//2022-01-15 //Dichotomy - greed #include<iostream> using namespace std; const int N = 5E4 + 20; int ll, m, n; int q[N]; bool check(int dis) //Judge whether it is true when mid is the minimum jump distance and m is 2 { /*********The following is the correct answer. I couldn't think of it at that time!!! It's a greedy idea****************/ int stand = 0; //Where I stand now int sum = 0; //Number of stones removed //Simulate the process of walking a stone for (int i = 0; i < n; i++) { if ((q[i] - stand) < dis) sum++; else stand = q[i]; } if (sum <= m)return true; else return false; } int main() { cin >> ll >> n >> m; for (int i = 0; i < n; i++)cin >> q[i]; int l = 1, r = ll; while (l < r) { int mid = l + r + 1 >> 1; if (check(mid)) l = mid; else r = mid - 1; // printf("mid=%d check=%d\n", mid, check(mid)); } cout << l << endl; return 0; }
Topic 2
The office area of Xiaoming company has a long corridor, which is composed of {NN square areas, as shown in the figure below.
KK , floor sweeping robots are deployed in the corridor, of which , ii , is in the , AI square area.
It is known that the sweeping robot can move to the left and right adjacent squares every minute and clean the area.
Please write a program to calculate the cleaning route of each robot so that
- They all eventually return to the starting grid,
- Each square area is cleaned at least once,
- It takes the least time from the start of the robot to the last robot's homing.
Note that multiple robots can clean the same block area at the same time, and they will not affect each other.
Output takes the least time.
In the example shown in the figure above, the minimum time spent is 66.
The first route: 2 − 1 − 2 − 3 − 4 − 3 − 22 − 1 − 2 − 3 − 4 − 3 − 2, cleared areas 1, 2, 3 and 4.
The second route is − 5 − 6 − 7 − 6 − 55 − 6 − 7 − 6 − 5, cleaning − 5, 6, 75, 6 and 7.
The third route is − 10 − 9 − 8 − 9 − 1010 − 9 − 8 − 9 − 10, cleaning − 8, 98, 9 and 1010.
Input format
The first line contains two integers , NN , and , KK.
Next, KK lines, each line has an integer AiAi.
Output format
Output an integer representing the answer.
Data range
1≤K<N≤105 ,1≤K<N≤105,
1≤Ai≤N1≤Ai≤N
Input example:
10 3 5 2 10
code:
#include <iostream> #include<algorithm> using namespace std; const int N = 1e5 + 10; int n, k; //There are n grids and k robots in total int q[N]; bool check(int x) //The difficulty of giving a maximum time (assuming 6) is: how to judge { int now = 0; //At present, the number of grids that have been ok is finally compared with the size of n for (int i = 0; i < k; i++) { if (now + x / 2 >= q[i]) //You can at least walk to the left border { if (now < q[i] - 1) //You need to sweep to the left again { now = q[i] + (x - (q[i] - now - 1) * 2) / 2; } else //You don't need to sweep to the left { now = q[i] + x / 2; } // printf("33 %d %d\n", x, now); } else //It takes the most time to get to the left border return 0; } if (now >= n)return 1; else return 0; } int main() { cin >> n >> k; for (int i = 0; i < k; i++)cin >> q[i]; sort(q, q + k); int l = 0, r = n; while (l < r) { int mid = l + r >> 1; if (check(mid))r = mid; else l = mid + 1; } cout << l << endl; return 0; }
Summary:
1. One target sequence is two questions (i.e. can I use two points)
According to the meaning of the question, only x is an even number, but it is enough to determine now > N in the function check(). In this way, the target sequence is divided into two, that is, both the right side of the target value are positive solutions.
2. The greedy idea is used in the check() function, that is, as long as the simulated stone walking problem is completed at the moment (the main writing method is cp, and pay attention to this idea in the future).