当前位置:网站首页>[heuristic divide and conquer] the inverse idea of heuristic merging
[heuristic divide and conquer] the inverse idea of heuristic merging
2022-07-23 15:09:00 【ZhgDgE】
Blog :
( violence / Heuristic splitting ) Code source daily question Div1 Good sequence
「 essays 」 A divide and conquer strategy based on heuristic thought —— Heuristic splitting
First think about the heuristic method of merging sequence attributes : Take the number of attributes as an example that satisfy the property of the sequence . Now there are two sets , It is required to calculate the number of merged pairs . We enumerate the elements in a smaller set , Query number to number in another set . Put larger elements after query . Time complexity O ( n log n ) O(n\log n) O(nlogn)
Heuristic divide and conquer is actually the inverse process of heuristic merging . We choose a subscript in an interval as the dividing point ( Divide and conquer in reverse order 、 The dividing points selected in the sorting algorithm are all intermediate points ), Count the number of pairs of left and right subintervals . Then we need to calculate the number of pairs at both ends of the dividing point , Here we need the core of heuristic divide and conquer : enumeration There are fewer interval elements The elements of , Then count the number of pairs in another interval . This is exactly the inverse process of heuristic merging . This ensures that the time complexity is also O ( n log n ) O(n\log n) O(nlogn)
#613. Good sequence
The question : There's a long one for n n n Sequence A 1 , A 2 , ⋯ , A n A_1,A_2,\cdots,A_n A1,A2,⋯,An . Define a sequence { A } \{A\} { A} Good. , If and only if each of his subintervals [ l , r ] [l,r] [l,r] Satisfy , At least one element exists x x x Only once .
Answer key :- ( violence / Heuristic splitting ) Code source daily question Div1 Good sequence
Ideas : First preprocess the subscript of each number that is the same as him and the nearest number . For an interval [ l , r ] [l, r] [l,r] , If there is a number that only appears once , Then it must be good to span the subinterval of this number , Don't have to consider , Just consider the two sub intervals separated . The core here is to enumerate from both ends to the middle , This can ensure that the number of enumerations must be less than half of the total number of elements .
AC Code :http://oj.daimayuan.top/submission/299119
HDU 2019 Multi school Make Rounddog Happy
The question : The length of the question is n ( n ≤ 3 × 1 0 5 ) n(n\leq 3\times 10^5) n(n≤3×105) How many subintervals satisfy max ( a l , a l + 1 , ⋯ , a r ) − ( r − l + 1 ) ≤ k \max(a_l,a_{l+1},\cdots,a_r)-(r-l+1) \leq k max(al,al+1,⋯,ar)−(r−l+1)≤k And the elements in the interval are different from each other
Answer key : Heuristic divide and conquer
Ideas : Heuristic divide and conquer takes the maximum subscript as the dividing point . Calculate the heresy with two ends at the dividing point , Then enumerate the elements in the interval with smaller length , Calculation is enough .
AC Code :
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
#define endl '\n'
#define fi first
#define se second
#define ppb pop_back
#define pb push_back
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mset(x, a) memset(x, a, sizeof (x))
#define rep(i, l, r) for(LL i = l; i <= (r); ++ i)
#define per(i, r, l) for(LL i = r; i >= (l); -- i)
#define reps(i, l, r, d) for(LL i = l; i <= (r); i += d)
#define pers(i, r, l, d) for(LL i = r; i >= (l); i -= d)
template<class T> bool ckmax(T& a, T b) {
return a < b ? (a = b, 1) : 0; }
template<class T> bool ckmin(T& a, T b) {
return a > b ? (a = b, 1) : 0; }
const int N = 3e5 + 10, M = 1e5 + 10, LG = 20;
const LL p = 1e9 + 7;
struct st_pair{
int w, idx;
bool operator < (const st_pair & x) const{
return w < x.w;
}
};
st_pair f[N][LG];
int n, a[N], k, cnt[N], L[N], R[N];
void init(){
rep(j, 0, LG - 1){
for(int i = 1; i + (1 << j) - 1 <= n; i ++ ){
if(!j) f[i][j] = {
a[i], i};
else f[i][j] = max(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
}
}
}
st_pair query(int l, int r){
int k = __lg(r - l + 1);
return max(f[l][k], f[r - (1 << k) + 1][k]);
}
LL split(int l, int r){
if(l > r) return 0;
if(l == r) return a[l] - 1 <= k;
st_pair pr = query(l, r);
LL mid = pr.idx, mx = pr.w;
LL res = split(l, mid - 1) + split(mid + 1, r);
if(mid - l < r - mid){
rep(i, l, mid) if(R[i] >= mid) res += max(0LL, min(R[i], r) - max(mid, mx - k + i - 1) + 1);
}else{
rep(i, mid, r) if(L[i] <= mid) res += max(0LL, min(mid, i + 1 - mx + k) - max(L[i], l) + 1);
}
return res;
}
void solve()
{
cin >> n >> k;
rep(i, 1, n) cin >> a[i];
init();
rep(i, 1, n) cnt[i] = 0;
for(int i = 1, j = 0; i <= n; i ++ ){
while(j < n && !cnt[a[j + 1]]) j ++ , cnt[a[j]] ++ ;
R[i] = j;
cnt[a[i]] -- ;
}
rep(i, 1, n) cnt[i] = 0;
for(int i = n, j = n + 1; i >= 1; i -- ){
while(j > 1 && !cnt[a[j - 1]]) j -- , cnt[a[j]] ++ ;
L[i] = j;
cnt[a[i]] -- ;
}
cout << split(1, n) << endl;
}
int main()
{
ios;
int T; cin >> T;
while(T -- )
solve();
return 0;
}
边栏推荐
- 深入理解CAS (自旋锁)
- Blazor quickly realizes Minesweeper
- Linux: analysis of the basic use of vim editor
- 【无标题】
- The accuracy of digital addition
- AVX指令集加速矩阵乘法
- leetcode: 17. 电话号码的字母组合
- Prometheus入门使用(三)
- 易基因|靶基因DNA甲基化测序(Target-BS)
- [test platform development] 21. complete sending interface request and display response header information
猜你喜欢
随机推荐
Simulation of voltage source PWM rectifier with double closed loop vector control based on Simulink
it 农民工的现状和历史
Redis bloom filter
Blazor快速实现扫雷(MineSweeper)
基于matlab的BOC调制信号捕获仿真
数字相加的精度问题
supervisord安装使用
Qt| imitation text floating letter
基于simulink的双闭环矢量控制的电压型PWM整流器仿真
day18
The best time to buy and sell stocks
基於matlab的CBOC信號調制解調仿真,輸出其相關性,功率譜以及頻偏跟踪
MySQL 常用命令
如何实现多个传感器与西门子PLC之间485无线通讯?
Live classroom system 03 model class and entity
Subsequence --- edit distance
易基因|靶基因DNA甲基化测序(Target-BS)
[turn] functional area division based on poi ()
Use of RSA encryption
颜值爆表 Redis官方可视化工具来啦,针不戳









