Gym - 101778B Ran and the Lock Code
The meaning of problems
Lets you construct the number n, their average and if a, demand structure of the maximum number of columns have different numbers.
Thinking
It took me a long time to overcome a mindset:
First, it is easy to think, \ (. 1 \ leftrightarrow. 2A-. 1 \) digital symmetric distribution is can, therefore, \ (n-\ leq2a-. 1 \) the answer is \ (n-\) , is greater than is the \ (. 2A -1 \) .
Then look at the following example, no problem, so then wrote a wa.
why? It can be considered, \ (n-\) when large: \ [\ {. 1, \ cdots,. 1-A, A, A +. 1, \ cdots,. 1. 2A-, \ cdots ??? \} \]
A key point is that right there \ (n- (2a-1) \) position, here's what to fill?
Full fill \ (A \) ?, But if filled \ (\ {1, a- 1,2a \} \) can be found, will not change the average, but more a new number, \ (\ {. 2A \} \)
That is how these arrangements back position is a very complicated very abstract issue, then we think about how to verify that a situation may not be feasible.
Assumed that the current I guess may be constructed with a different number of \ (m \) th order number as much, it must include \ (\ {1 \ cdots m \} \)
Obviously \ (m = 2a-1 \ ) , it is the previously imagined ideal situation.
Because then the average sum is equal to \ (a \ times m \) so we can detect:
\ [(m + 1) \ times m \ div2-a \ times m \] to say that \ (m \) the sum of the ratio of the number of full \ (A \) how much per cent more,
These extra, we left \ (nm \) number can go and join in, see if you can cobble together on it.
So we have the equations can be verified is monotonous, half can do.
Code
#include <bits/stdc++.h>
#define _debug(x) cerr<<#x<<" = "<<(x)<<endl;fflush(stdout)
#define cvint const vint &
#define cint const int &
using namespace std;
typedef long long ll;
typedef vector<int> vint;
const int maxn = 505;
const int inf = 0x3F3F3F3F;
const int mod = 1e9 + 7;
int Kase;
int n, a, ans;
inline bool chek(const ll &m) {
// return (m + 1) * m / 2 - m * a <= (n - m) * (a - 1);
return 2ll * n - m <= 2ll * a * n - m * m;
}
int main() {
scanf("%d", &Kase);
while (Kase--) {
scanf("%d %d", &n, &a);
if (n <= 2 * a - 1) {
printf("%d\n", n);
continue;
}
int l = 1, r = n, m;
while (l <= r) {
m = l + r >> 1;
if (chek(m)) {
ans = m;
l = m + 1;
} else {
r = m - 1;
}
}
printf("%d\n", ans);
}
return 0;
}
/*
9
1 2 8 3 7 4 1 2 5
*/