Submission
Status:
(PPPPPPPPPPPPP)(PPPPPPPPP)(PPPPPPPPPP)(PPPPPPPPPP)(PPPPPPPPPPPPPP)(PPPPPPPPPPPPPPPPPPP)(PPPPPPPPPPPPPPPPPPPPPP)
Subtask/Task Score:
{4/4}{4/4}{5/5}{7/7}{25/25}{34/34}{21/21}
Score: 100
User: meme_boi2
Problemset: ร้านปลอดภาษี (Duty Free)
Language: cpp
Time: 0.318 second
Submitted On: 2025-11-02 10:43:18
#include <bits/stdc++.h>
using namespace std;
int pa[2000002];
vector<int> vec;
int find(int i){
// cout << "FIND i : " << i << '\n';
if(pa[i] == i) return i;
else return pa[i] = find(pa[i]);
}
void reset(){
for(auto x:vec){
pa[x] = x;
}
vec.clear();
}
int cnt = 0;
int minimum_bag_rearrangement_time (std::vector<int> max_allowed_positions){
int n = max_allowed_positions.size();
// max_allowed_positions.insert(max_allowed_positions.begin(),0);
for(int i = 0; i <= n; i++) pa[i] = i;
for(int i = 0; i < n; i++){
int temp = find(max_allowed_positions[i]);
// cout << max_allowed_positions[i] << '\n';
if(pa[max_allowed_positions[i]] == 0){
// cout << temp << ' ' << i << '\n';
cnt++;
reset();
}
vec.push_back(pa[max_allowed_positions[i]]);
pa[pa[max_allowed_positions[i]]] = find(pa[max_allowed_positions[i]]-1);
}
return cnt;
}
int32_t __main(){
cin.tie(nullptr)->sync_with_stdio(0);
int n; cin >> n;
vector<int> mat(n);
for(auto &x: mat) cin >> x;
cout << minimum_bag_rearrangement_time(mat);
}