除了位置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;}新闻热点
疑难解答