Codeforces 109D题类选择排序解法测试点41超时求优化建议
问题说明
我在解答本题时,在第41个测试点出现Time limit exceeded错误。
我猜测是选用的数据结构不合适,但思考了很久也不知道该采用什么更优的数据结构,恳请各位给出建议。
算法思路
本方法和选择排序有一定相似性,首先找到一个幸运数,将其与第一个数交换位置;接着从第二个位置开始,找到最小的数,将其与幸运数交换,这样从第二个位置起的最小数就被放到了第一个位置,此时幸运数处在新的位置,需要记录它的位置。之后将幸运数与第二个位置的数交换,重复上述流程。需要特别注意的是,我们需要记录选中的幸运数排序后的位置,因为该位置前后的处理流程略有差异,需要进行对比(代码注释中有相关说明)。
提交代码
using namespace std; typedef long long ll; // Judge whether it is a lucky number. if it is a lucky number, return true, otherwise, return false. bool judge(string x); bool static cmp(vector<ll> a, vector<ll> b) { return a[0] < b[0]; } int main() { int n; cin >> n; vector<ll> nums; ll smLuckyNum = INT64_MAX; for(int i = 0; i < n; i++) { ll x; cin >> x; nums.push_back(x); if(judge(to_string(x)) && smLuckyNum > x) smLuckyNum = x; // Find the smallest lucky number } vector<vector<ll> > b; // b[i][0] stores nums[i], b[i][1] stores i index int pos, origIndex; bool sorted = true; // b[i][0] stores nums[i] and b[i][1] stores i index for(int i = 0; i < n; i++) { vector<ll> tmp; tmp.push_back(nums[i]); tmp.push_back(i); b.push_back(tmp); } sort(b.begin(), b.end(), cmp); // sort by b[i][0] // If the original array is ordered, sorted == true, otherwise, sorted == false for(int i = 0; i < n; i++) if(b[i][1] != i) sorted = false; // If the original array is not ordered and there is no lucky number in it, no way to sort, return -1 if(!sorted && smLuckyNum == INT64_MAX) { cout << -1 << endl; return 0; } // If the original array is ordered, no sorting required if(sorted) { cout << 0 << endl; return 0; } // Find the position of the smallest lucky number, origIndex is the index of the smLuckyNum before sorting, pos is the index of the smLuckyNum after sorting for(int i = 0; i < n; i++) { if(smLuckyNum == b[i][0]) { origIndex = b[i][1]; pos = i; break; } } vector<string> ans; // Traverse the nums array from 0 to n - 2, if i is equal to pos(the index of smLuckyNum after sorting) , continue. // We can swap the element at position i and the element at position origIndex, and record the position of LuckyNum after the swap in origIndex, // then find the smallest element from i+1 to n-1 and record the index in MINID, notice if i > pos, and nums[MINID] > nums[pos] (because there may be a large number placed at pos), the element at pos is smallest number // if MINID < n, swap(nums[i](it is a lucky number), nums[MINID]) for(int i = 0; i < n - 1; i++) { if(i == pos) continue; string res = ""; if(i != origIndex) { res += (to_string(i + 1) + " " + to_string(origIndex + 1)); // According to the requirement of the question, the index starts from 1, the result needs to plus one. swap(nums[i], nums[origIndex]); origIndex = i; ans.push_back(res); } ll MINID = i + 1; for(int j = i + 1; j < n; j++) if(nums[MINID] > nums[j]) MINID = j; if(i > pos && nums[MINID] > nums[pos]) MINID = pos; if(MINID < n) { res = ""; res += (to_string(i + 1) + " " + to_string(MINID + 1)); swap(nums[i], nums[MINID]); origIndex = MINID; ans.push_back(res); } } // Finally we need to compare nums[n - 1] with nums[pos] when pos != n - 1 if(pos + 1 != n && nums[pos] > nums[n - 1]) ans.push_back(to_string(pos + 1) + " " + to_string(n)); cout << ans.size() << endl; for(int i = 0; i < ans.size(); i++) cout << ans[i] << endl; return 0; } bool judge(string x) { for(int i = 0; i < x.size(); i++) if(x[i] != '4' && x[i] != '7') return false; return true; }
问题解决
超时原因
你当前代码的时间复杂度为O(n²),核心耗时点是外层循环每处理一个位置i,都要在内层遍历i之后的所有元素找最小值。本题n的上限为1e5量级,O(n²)的复杂度完全无法满足时间要求,这和你选用的数据结构无关,是算法复杂度本身的问题。
优化方案
采用「置换环」思路可以将复杂度降到O(n log n),轻松通过所有测试用例:
- 先对数组排序,得到每个元素最终应该放置的目标位置
- 用访问标记数组找出所有置换环,每个置换环对应一组互相放错位置的元素
- 分情况处理置换环:
- 如果数组中没有幸运数:仅当原数组已经有序时输出0,否则输出-1
- 如果存在最小幸运数:
- 若置换环内已经包含这个最小幸运数,处理该环仅需要
环长-1次交换 - 若置换环内没有最小幸运数,将最小幸运数临时换到环中作为交换工具,处理完后归位,总共需要
环长+1次交换
- 若置换环内已经包含这个最小幸运数,处理该环仅需要
- 处理置换环的过程中可以同步记录每一步的交换操作,满足题目的输出要求。
内容的提问来源于stack exchange,提问作者RecharBao
相关产品推荐
相关产品推荐

