除了位置0,其他位置一旦對應就從存儲容器中刪除,直到存儲容器中只剩下0, 要考慮rr[0]==0的特殊情況
#include<iostream>#include<map>#PRagma warning(disable:4996)using namespace std;int main(){ int N; int cnt = 0; int tt = 0; cin >> N; map<int, int> rr;//數據,位置(存儲容器) for (int t = 0;t < N;t++) { int temp; scanf("%d", &temp); if (temp == 0 || temp != t) rr[temp] = t; } while (rr.size() != 1) { if (rr[0] == 0) { auto it = rr.begin(); it++; rr[0] = (*it).second; it->second = 0; cnt++; } int tem = rr[0]; rr[0] = rr[tem]; rr.erase(tem); cnt++; } cout << cnt << endl;}新聞熱點
疑難解答