Codeforces Hello 2019 ABCDF题解

A. Gennady and a Card Game

读不错题就写不错

string s[6];
 
int main() {
#ifdef QvvQ
  // freopen("data.in", "r", stdin), freopen("data.out", "w", stdout);
  Dbg = 1;
#endif
  in, s[0];
  lop1(i, 5) {
    in, s[i];
    if (s[i][0] == s[0][0] || s[i][1] == s[0][1]) return puts("YES"), 0;
  }
  puts("NO");
 
#ifdef QvvQ
  fprintf(stderr, "\ntime:%.5fms", clock() * 1.0 / CLOCKS_PER_SEC * 1000);
#endif
  return 0;
}

B. Petr and a Combination Lock

给出n个数的绝对值(正负可以任意),问是否有可能使得n个数的和是0

\(2^n\) 子集枚举

int main() {
#ifdef QvvQ
  // freopen("data.in", "r", stdin), freopen("data.out", "w", stdout);
  Dbg = 1;
#endif
  in, n;
  lop0(i, n) in, a[i];
  for (int S = 0; S < (1 << n); ++S) {
    int ans1 = 0, ans2 = 0;
    for (int i = 0; i < n; ++i) {
      if ((1 << i) & S) ans1 += a[i];
      else ans2 += a[i];
    }
    if (abs(ans1-ans2) % 360 == 0) return puts("YES"), 0;
  }
  puts("NO");
 
#ifdef QvvQ
  fprintf(stderr, "\ntime:%.5fms", clock() * 1.0 / CLOCKS_PER_SEC * 1000);
#endif
  return 0;
}

C. Yuhao and a Parenthesis

题意:给n个括号序列,如果 \(s_i+s_j\) 合法,那么这一对就可以匹配,问最多能匹配多少对(每个串只能用一次)

假设在某个位置 前缀中)的数量多于(的数量,那么这个串就不能放在前面。同理如果某个位置后缀(多于),那么这个串就不能放在后面。

既能放在前面又能放在后面的肯定自身就是一个合法串,可以任意匹配,剩下的用一个桶存起来两两匹配

char s[MAXN * 5];
int n, b[MAXN * 5], c[MAXN * 5];


int main() {
#ifdef QvvQ
  // freopen("data.in", "r", stdin), freopen("data.out", "w", stdout);
  Dbg = 1;
#endif
  in, n;
  int ans = 0;
  lop1(i, n) {
    in, s + 1;
    int len = strlen(s + 1), tot1 = 0, tot2 = 0, can1 = 1, can2 = 1;
    lop1(j, len) {
      (s[j] == '(' ? tot1 : tot2)++;
      if (tot2 > tot1) can1 = 0;
    }
    tot1 = tot2 = 0;
    dlop1(j, len) {
      (s[j] == '(' ? tot1 : tot2)++;
      if (tot1 > tot2) can2 = 0;
    }
    if (can1 && can2) ++ans;
    else if (can1) ++b[tot1 - tot2];
    else if (can2) ++c[tot2 - tot1];
  }
  ans >>= 1;
  lop0(i,5e5) ans += min(b[i], c[i]);
  out, ans;
#ifdef QvvQ
  fprintf(stderr, "\ntime:%.5fms", clock() * 1.0 / CLOCKS_PER_SEC * 1000);
#endif
  return 0;
}

为了骗访问量,请访问另一个链接

D. Makoto and a Blackboard

F. Alex and a TV Show

posted @ 2019-01-05 16:02  QvvQ  阅读(420)  评论(0编辑  收藏  举报