# Ali programming question 2021.8.27 + CMB technology test development 2021.9.6

1. Min max
Niuniu has a positive integer array of length n, a={a1,a2,..., an}. Now he can perform the following operations:
Select an I (1 < = I < = n) and make ai=ai+1
He must perform the above operations k times. Niuniu wants to make the maximum value in array a as small as possible after performing all operations. Please help Niuniu calculate the maximum value as small as possible.

Example 1:
Input:
3 3
1 2 3
Output:
3
explain:
The first operation: select A1, make a1=a1+1=2, and the array is {2,2,3}
The second operation: select A1, make a1=a1+1=3, and the array is {3,2,3}
The third operation: select a2, make a2=a2+1=3, and the array is {3,3,3}

Example 2:
Input:
3 6
1 2 3
Output:
3
explain:
The first three operations are the same as example 1, and the array becomes {3,3,3}
The next three operations select a1, a2 and a3 in turn, and the final array becomes {4,4,4}, so the answer is 4.

Original:

```#include <iostream>
#include <cmath> 	//  ceil() is the header file of rounding up
#include <vector>
#include <algorithm>
using namespace std;

int main() {
int n,k;
cin>>n>>k;
vector<int> inputs(n);
for(int i=0;i<n;i++){
cin>>inputs[i];
}
sort(inputs.begin(),inputs.end());
float sum=0;
for(int i=0;i<n;i++){
sum+=inputs[inputs.size()-1]-inputs[i];
}
if(k<=sum){
cout<<inputs[inputs.size()-1]<<endl;
}
else if(k>sum){
float j=k-sum;
float m=inputs.size();
int res = ceil(j/m);
cout<<inputs[inputs.size()-1] + res<<endl;
}
return 0;
}
// Only 52%
```

Note: consider data overflow - use long type!!!!!
Rounding up x/y is equivalent to rounding down (x+y-1)/y
Modified version:

```#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
int n;
long k;
cin>>n>>k;
vector<long> inputs(n);
for(int i=0;i<n;i++){
cin>>inputs[i];
}
sort(inputs.begin(),inputs.end());
long max = inputs[n-1];
long sum=0;
for(int i=0;i<n;i++){
sum += max-inputs[i];
}

if(k>sum){
k=k-sum;
max+=(k+n-1)/n;	//Round up
}
cout<<max<<endl;
return 0;
}
```

2. Assemble
Niuniu's k troops are stationed on a rectangular map of n * m. each force is stationed on a unique coordinate (i, j) (1 < = i < = n, 1 < = j < = m) (i, j are integers).
For each force, assuming that the current coordinates of the force are (i,j), the force can spend 1 hour moving from coordinates (i,j) to one of the four coordinates (i+1,j), (i,j+1), (i-1,j), (i,j-1) (that is, it can move up, down, left and right each time), and the force cannot leave the map.
Some integer coordinates on the map are swamps, and the troops can't reach the swamp area.
Now Niuniu wants to assemble these troops to the same coordinate point. The total time for assembly is the time for the slowest troops to reach the assembly point. Niuniu wants to know how many hours it takes to assemble?
Enter Description:
In the first line, enter two positive integers n and m to represent the length and width of the matrix map.
In the next n lines, enter a string of length m containing only '0' and '1'. If the character in column j of line i is' 1 ', it means that the coordinate (i,j) is swamp; if the character in column j of line i is' 0', it means that the coordinate (i,j) is land (troops can only be on land, children can't be on swamp).
Enter a positive integer k in the next line to indicate the number of troops.
Finally, there are two positive integers x and Y in each line of k lines, indicating that the coordinates of the troops stationed are (x,y).
(1 < = n, m < = 500, 1 < = k < = min (10, n * M-S), 1 < = x < = n, 1 < = y < = m, where s is the number of swamp areas, and the data ensure that the coordinates of the initial force will not coincide with those of the swamp.)
Output Description:
The output of a positive integer indicates that the total time of assembly takes a minimum of several hours. If all troops cannot be assembled together, the output of - 1.

Example 1:
Input:
3 3
000
101
000
4
1 1
1 3
3 1
3 3
Output:
2
Note: the time taken by troops from (1,1), (1,3), (3,1), (3,3) to (2,2) is 2, and the final assembly time is 2, so the answer is 2.

Example 2:
Input:
3 3
000
111
000
2
1 2
3 2
Output:
-1
Note: the two teams cannot be assembled together.

Idea: each army does breadth first traversal at the same time to see if it can gather together?
First, find the shortest time for each army to reach any place on the map, and then traverse all places to calculate the maximum value of all armies to reach here. The minimum of these maximum values is the required value.

## CMB technology test development

```#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> get_in(vector<int>&L){
vector<vector<int>> result;
int j=1;
int n=L.size();
for(int i=0;i<n;){
if(n-j<0) break; //!! I made a mistake in the written examination
int k=0;
vector<int> tmp;
while(k<j){
tmp.push_back(L[i+k]);
k++;
}
result.push_back(tmp);
i += j;
j++;
}
return result;
}
int main() {
vector<int> inputs={1};
vector<vector<int>> res = get_in(inputs);
for(int i=0;i< res.size();i++){
cout << "[";
for(int j=0;j<res[i].size();j++)
cout<<res[i][j]<<",";
cout<<"]";
}
return 0;
}
```

Tags: C C++ Algorithm

Posted on Sat, 20 Nov 2021 10:52:27 -0500 by chiaki*misu