Official

D - 大乱闘/Melee Editorial by admin


各プレイヤーについての「スコア」と「自分を最後に攻撃したプレイヤー」を保持しながらシミュレーションを行うことで、\(O(N + M)\) の計算量で解くことが出来ます。

解答例(C++)

#include<bits/stdc++.h>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> score(N+1);
  vector<int> lastAtacker(N+1);
  for (int i = 0; i < M; i++) {
    int type;
    cin >> type;
    if (type == 1) {
      int x, y;
      cin >> x >> y;
      lastAtacker[y] = x;
    } else {
      int z;
      cin >> z;
      score[z]--;
      score[lastAtacker[z]]++;
      lastAtacker[z] = 0;
    }
  }

  for (int i = 1; i <= N; i++) {
    cout << score[i];
    if (i == N) cout << endl; else cout << ' ';
  }
  return 0;
}

posted:
last update: