Official

B - Trick Taking Editorial by leaf1415


実際に全カードを走査することで、 色が \(T\) のカード(および、プレイヤー \(1\) が出したカードと同じ色のカード)が場に出ているか、また、それらの中で値が最大のカードはどれか・誰によって出されたかを調べ、問題文中の決定方法にしたがって勝者を決定すれば良いです。

全カードを走査するには、プログラミング言語の標準的な機能である繰り返しの機能( for 文など)を用いることができます。

以下に、C++ 言語による本問題の正解例を記載します。

#include <iostream>
#include <utility>
using namespace std;

int n, t;
int c[200001], r[200001];

int main(void)
{
  cin >> n >> t;
  for(int i = 1; i <= n; i++) cin >> c[i];
  for(int i = 1; i <= n; i++) cin >> r[i];
  
  pair<int, int> tmax = {-1, -1}, lmax = {-1, -1};
  for(int i = 1; i <= n; i++){
    if(c[i] == t) tmax = max(tmax, {r[i], i});
    if(c[i] == c[1]) lmax = max(lmax, {r[i], i});
  }
  if(tmax.first != -1) cout << tmax.second << endl;
  else cout << lmax.second << endl;
  
  return 0;
}  

posted:
last update: