AcWing 1542. 老鼠和大米---迭代
原题链接
简单
作者:
巨鹿噜噜噜路
,
2020-06-01 01:36:05
,
所有人可见
,
阅读 710
C++ 代码
#include <iostream>
#include <vector>
using namespace std;
const int MAX_N = 1010;
int w[MAX_N], Rank[MAX_N];
int nP, nG;
vector<int> record; //淘汰者
int main() {
cin >> nP >> nG;
vector<int> curr(nP);
for (int i = 0; i < nP; i++) cin >> w[i];
for (int i = 0; i < nP; i++) cin >> curr[i];
int idxRank = 0;
while (record.size() < nP - 1) {
vector<int> next; //晋级者
int restCnt = curr.size(); //剩余选手数量
for (int i = 0; i < restCnt; i += nG) {
int maxId, maxW = -1;
for (int j = 0; j < nG && i + j < restCnt; j++) { //小组内进行比赛
int idx = i + j;
if (w[curr[idx]] > maxW) {
if (maxW != -1) record.push_back(maxId); //记录淘汰者
maxW = w[curr[idx]];
maxId = curr[idx];
}
else record.push_back(curr[idx]); //记录淘汰者
}
next.push_back(maxId); //记录晋级者
}
while(idxRank < record.size()) {//更新淘汰者排名,晋级人数+1
Rank[record[idxRank++]] = next.size() + 1;
}
curr = next; //更新下一轮比赛人员
}
Rank[curr[0]] = 1; //仅剩冠军还在curr数组
for (int i = 0; i < nP; i++) {
if (i) printf(" ");
printf("%d", Rank[i]);
}
return 0;
}