← Quay lại bài
Lời giải
Ta đi tìm dãy số đẹp dài nhất. Tức tìm dãy số thoả mãn , với lớn nhất có thể.
Nhận xét
Với điều kiện , thì ta thấy khoảng cách giữa với sẽ tăng lên ít nhất gấp đôi theo thứ tự tăng dần. Mà giới hạn của các phần tử là , nên dãy sẽ có độ dài tối đa là , tức là khoảng .
Vì vậy ta nghĩ đến việc: với mỗi vị trí trên dãy , ta tìm dãy con đẹp dài nhất bắt đầu từ . Ta tiến hành chặt nhị phân tìm nhỏ nhất thoả mãn vị trí tiếp theo (có thể dùng lower_bound).
Độ phức tạp
Code tham khảo
// Created by BJMinhNhut
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MASK(i) (1ll<<(i))
#define BIT(t, i) (((t)>>(i))&1)
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef pair<int, int> ii;
/***Common Functions***/
template <class T>
bool minimize(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
bool maximize(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
/***End of Template***/
int n;
const int N = 2e5+5;
int a[N];
void Input() {
cin >> n;
FOR(i, 1, n) cin >> a[i];
}
void Solve() {
int res = 0;
FOR(i, 1, n) {
if (i > 1 && a[i] == a[i-1]) continue;
int cnt = 2;
int cur = i+1;
while (1) {
int j = lower_bound(a+1+cur, a+1+n, 2*a[cur]-a[i]) - a;
if (j > n) break;
cur = j;
cnt++;
}
maximize(res, cnt);
}
cout << n-res << '\n';
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int t; cin >> t;
while (t--)
Input(), Solve();
return 0;
}
Công bố ngày: 26/11/2021
