Topic link
answer
Title
By adding an existing number in the array at the end of the array or adding 1
an operation to a number in the array to make the array value equal n
, find the smallest operand.
Ideas
Write a few examples and then you can find that you can get the smallest number of operation steps when you get to the middle.
(Actually it is the same as finding the minimum side length of the known rectangle area in elementary school.
For example
42
1 42 : 41
2 21 : 40
........
AC code
First edition
In the last few minutes, I thought of the method, and then simulated it, but it was 1s late. . . .
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ll)(1e9+7)
typedef pair<int, int> P;
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
int T;cin >> T;
while (T--) {
int n;cin >> n;
int ans = INF;
for (int t = floor(sqrt(n) + 1); t >= 1; t--) {
if (t * ceil((double(n) / t)) >= n) {
ans = min(ans, (int)(t + ceil(double(n) / t) - 2));
}
}
cout << ans << endl;
}
return 0;
}
second edition
After thinking about this, I don’t need simulation, just take the middle value.
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ll)(1e9+7)
typedef pair<int, int> P;
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
int T;
cin >> T;
while (T--) {
int n;cin >> n;
int t = int(floor(sqrt(n)));
int ans = t + int(ceil(double(n) / t));
cout << ans - 2 << endl;
}
return 0;
}
Boss code
Well, the boss is very short.
(Note what I wrote
#include<bits/stdc++.h>
int N;
int main() {
for (std::cin >> N; std::cin >> N; std::cout << ceil(2 * sqrt(N)) - 2 << ' ');
// 如果先ceil后*2会导致所得的结果较小。
// 不好理解的话可以画一个正方形,然后求他的最小周长。
}