結果

問題 No.2148 ひとりUNO
ユーザー tnakao0123tnakao0123
提出日時 2022-12-09 10:39:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 2,212 bytes
コンパイル時間 718 ms
コンパイル使用メモリ 56,540 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 19:02:43
合計ジャッジ時間 2,097 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 14 ms
5,376 KB
testcase_17 AC 15 ms
5,376 KB
testcase_18 AC 14 ms
5,376 KB
testcase_19 AC 14 ms
5,376 KB
testcase_20 AC 15 ms
5,376 KB
testcase_21 AC 14 ms
5,376 KB
testcase_22 AC 13 ms
5,376 KB
testcase_23 AC 14 ms
5,376 KB
testcase_24 AC 15 ms
5,376 KB
testcase_25 AC 14 ms
5,376 KB
testcase_26 AC 7 ms
5,376 KB
testcase_27 AC 7 ms
5,376 KB
testcase_28 AC 7 ms
5,376 KB
testcase_29 AC 7 ms
5,376 KB
testcase_30 AC 6 ms
5,376 KB
testcase_31 AC 6 ms
5,376 KB
testcase_32 AC 6 ms
5,376 KB
testcase_33 AC 5 ms
5,376 KB
testcase_34 AC 6 ms
5,376 KB
testcase_35 AC 6 ms
5,376 KB
testcase_36 AC 7 ms
5,376 KB
testcase_37 AC 7 ms
5,376 KB
testcase_38 AC 6 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2148.cc:  No.2148 ひとりUNO - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 200000;
enum { B, G, R };

/* typedef */

typedef pair<int,int> pii;

/* global variables */

int e0[3][3], e1[3][3], cls[3];
pii ps[MAX_N];

/* subroutines */

inline int c2i(char c) {
  return (c == 'B') ? B : (c == 'G') ? G : (c == 'R') ? R : -1;
}

/* main */

int main() {
  int tn;
  scanf("%d", &tn);

  while (tn--) {
    int n;
    scanf("%d", &n);

    for (int i = 0; i < 3; i++) fill(e0[i], e0[i] + 3, 0);
    for (int i = 0; i < 3; i++) fill(e1[i], e1[i] + 3, 0);
    fill(cls, cls + 3, 0);
    int cn = 0;

    for (int i = 0; i < n; i++) {
      char s[4];
      int di;
      scanf("%s%d", s, &di);
      int ci = c2i(s[0]);
      ps[i] = pii(di, ci);
      if (! cls[ci]) cn++;
      cls[ci]++;
    }
    sort(ps, ps + n);

    for (int i = 0; i < n;) {
      int i0 = i;
      while (i < n && ps[i0].first == ps[i].first) i++;

      if (i - i0 > 1)
	for (int j0 = i0; j0 < i; j0++)
	  for (int j1 = j0 + 1; j1 < i; j1++) {
	    int c0 = ps[j0].second, c1 = ps[j1].second;
	    if (! e0[c0][c1])
	      e0[c0][c1] = e0[c1][c0] = ps[i0].first;
	    else if (! e1[c0][c1])
	      e1[c0][c1] = e1[c1][c0] = ps[i0].first;
	  }
    }

    //for (int i = 0; i < 3; i++) {
    //for (int j = 0; j < 3; j++) printf("%d,%d ", e0[i][j], e1[i][j]);
    //putchar('\n');
    //}

    bool ok = false;
    if (cn == 1)
      ok = true;
    else if (cn == 2) {
      int c0 = -1, c1 = -1;
      for (int i = 0; i < 3; i++)
	if (cls[i]) {
	  if (c0 < 0) c0 = i;
	  else c1 = i;
	}

      ok = (e0[c0][c1] > 0);
    }
    else {
      for (int c0 = 0; ! ok && c0 < 3; c0++) {
	int c1 = (c0 + 1) % 3, c2 = (c0 + 2) % 3;
	int e01 = e0[c0][c1], e11 = e1[c0][c1];
	int e02 = e0[c0][c2], e12 = e1[c0][c2];
	if (cls[c0] == 1 && e01 && e02)
	  ok = true;
	else if (cls[c0] > 1 &&
		 ((e01 && e02 && e01 != e02) ||
		  (e01 && e12 && e01 != e12) ||
		  (e11 && e02 && e11 != e02) ||
		  (e11 && e12 && e11 != e12)))
	  ok = true;
      }
    }

    if (ok) puts("YES");
    else puts("NO");
  }
  return 0;
}
0