就这么被Grok-3破解了?
2025-05-11 09:38:55
发布于:浙江
26阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i]; // Read fruit types: 1 for apple, 0 for orange
}
// Step 1: Initialize blocks
vector<deque<int>> blocks;
for (int i = 0; i < n; ) {
int j = i;
// Find the end of the current block (same type as a[i])
while (j < n && a[j] == a[i]) ++j;
deque<int> block;
// Add indices (1-based) to the block
for (int k = i; k < j; ++k) {
block.push_back(k + 1);
}
blocks.push_back(move(block));
i = j; // Move to the next block
}
// Step 2: Simulate picking process
while (!blocks.empty()) {
vector<int> picked;
// Pick the leftmost fruit from each block
for (auto& block : blocks) {
if (!block.empty()) {
picked.push_back(block.front());
block.pop_front(); // Remove the picked fruit
}
}
// Output the picked fruits
// Since blocks are ordered, picked indices are already sorted
for (size_t j = 0; j < picked.size(); ++j) {
cout << picked[j];
if (j < picked.size() - 1) cout << " ";
}
cout << endl;
// Step 3: Update blocks with merging
vector<deque<int>> new_blocks;
for (auto& block : blocks) {
if (!block.empty()) {
// Merge with the previous block if types match
if (!new_blocks.empty() &&
a[new_blocks.back().back() - 1] == a[block.front() - 1]) {
new_blocks.back().insert(new_blocks.back().end(),
block.begin(), block.end());
} else {
new_blocks.push_back(move(block));
}
}
}
blocks = move(new_blocks); // Update blocks for the next iteration
}
return 0;
}
全部评论 1
顶
2025-05-11 来自 浙江
0
有帮助,赞一个