沙发~
2026-04-26 10:56:44
发布于:北京
1阅读
0回复
0点赞
代码:(解释见注释)
include iostream
include vector
include string
include algorithm
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n + 1);
// pos[i] 存储数字 i 在数组 a 中的下标 (1-based)
vector<int> pos(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
pos[a[i]] = i;
}
string ans(n, '0');
int min_pos = pos[1];
int max_pos = pos[1];
// 检查 m=1
if (max_pos - min_pos + 1 == 1) {
ans[0] = '1';
}
for (int m = 2; m <= n; ++m) {
// 加入数字 m
min_pos = min(min_pos, pos[m]);
max_pos = max(max_pos, pos[m]);
// 检查区间长度是否等于 m
if (max_pos - min_pos + 1 == m) {
ans[m - 1] = '1';
}
}
cout << ans << endl;
}
int main() {
// 优化 IO 操作
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
end
这里空空如也







有帮助,赞一个