Sceptically-Sam owns a speed packing service that ...
Criado em: 24 de janeiro de 2026
Respondido usando GPT-5.2 Thinking por Chat01
Criado em: 24 de janeiro de 2026
Respondido usando GPT-5.2 Thinking por Chat01
Sceptically-Sam owns a speed packing service that uses elf-bots to process a row of packages
A row is partitioned into non-overlapping subarrays, each processed by one elf-bot.
An elf-bot cannot move packages outside its assigned subarray.
An elf-bot assigned to a segment from index i to j(i <= j) does the following:
stamps all packages in the subarray [i, j]
sorts the packages in [i, j] in ascending order of weight.
D
After all elf-bots finish, the every package must be stamped and sorted in ascending order of weight.
Determine the maximum number of elf-bots that can be assigned to the row of packages while satisfying the above conditions.
Input
The first line contains a single integer t (1 <= t <= 10 ^ 4) , which represents the number of testcases.
The first line of the i ^ (th) testcase contains a single integer n(1 <= n <= 2 * 10 ^ 5) , the number of packages in the row.
The second line of the i ^ (th) testcase contains n integers a1, a2,..., an (1 ≤ a ≤ 10º), representing the weight of each package in the row.
It is guaranteed that the sum of n over all the testcases does not exceed 2 * 10 ^ 5
Output
For each testcase, output one integer, which is the maximum number of elf-bots satisfying the given conditions.
Example
This is my solution optimize it :-
1
OneCompiler
#include<bits/stdc++.h>
2 using namespace std;
3
4 int main(){
5
int t;
6
7-
8
9
cin >> t;
while ( t --)\
int m;
cin >> m;
10
vector<int> L(m);
11
vector<int> M(m);
12
for(int i = 0 ; i <m; i++){
13
cin >> L[i] ;
14
16
}
M[i] = L[i] ;
15
17
sort(M.begin(), M.end());
18
int z = 0
19
int cm = 0
20
for(int i = 0 i < m; i++){
21
cm= max (L[i] cm);
22
if(cm== M[i] ) z++;
23
}
24
cout << z <<endl;
25
}
26 }
You’re solving “max chunks to make sorted”.
Your current idea (prefixMax == sorted[i]) is only correct when the array is a permutation with distinct values (like 0..n−1). With duplicates / arbitrary values it breaks.
You can end a chunk at position i iff the multiset of elements in a[0..i] equals the multiset of elements in sorted(a)[0..i].
So we track a difference counter:
a[i]sorted[i]
When all counters become zero ⇒ prefix multisets match ⇒ we can cut ⇒ ans++.O(n log n) per test because of sortingO(n) (no map), using coordinate compression + diff arraycpp#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { int n; cin >> n; vector<long long> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i]; b = a; sort(b.begin(), b.end()); // Coordinate compression (values -> 0..k-1) vector<long long> uniq = b; uniq.erase(unique(uniq.begin(), uniq.end()), uniq.end()); unordered_map<long long, int> id; id.reserve(uniq.size() * 2); id.max_load_factor(0.7f); for (int i = 0; i < (int)uniq.size(); i++) id[uniq[i]] = i; vector<int> diff(uniq.size(), 0); int nonzero = 0; auto add = [&](int idx, int delta) { int before = diff[idx]; int after = before + delta; diff[idx] = after; if (before == 0 && after != 0) nonzero++; else if (before != 0 && after == 0) nonzero--; }; int ans = 0; for (int i = 0; i < n; i++) { add(id[a[i]], +1); add(id[b[i]], -1); if (nonzero == 0) ans++; } cout << ans << "\n"; } return 0; }
Whenever the prefix multisets match, you can safely “close” a chunk there. Greedily closing as early/often as possible maximizes the number of chunks.
If you want, paste a couple of failing cases for your original approach and I’ll show exactly how/why it breaks.