AtCoder abc301 参加メモ

パナソニックグループプログラミングコンテスト2023(AtCoder Beginner Contest 301) - AtCoder

https://atcoder.jp/contests/abc300/tasks/abc301_b

#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
#define endl '\n'

int main() {
  int n; cin >> n;
  vector<int> a(n);
  REP(i,n) cin >> a[i];
  REP(i,n-1) {
    cout << a[i] << " ";
    for(int j = a[i]+1; j < a[i+1]; j++) cout << j << " ";
    for(int j = a[i]-1; j > a[i+1]; j--) cout << j << " ";
  }
  cout << a.back() << endl;
  return 0;
}

C - AtCoder Cards

出現数文字種別をカウントして一致できるかどうか調べる

#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
#define endl '\n'

int main() {
  const string atc = "atcoder";
  string s,t; cin >> s >> t;
  map<char,int> ms,mt;
  REP(i,(int)s.size()) { ms[s[i]]++; mt[t[i]]++; }

  auto f = [&](map<char,int> ma, map<char,int> mb) {
    for(auto [k,v]: ma) {
      if (k == '@') continue;
      int d = v - mb[k];
      if (d <= 0) continue;
      if (atc.find(k) == string::npos) return false;
      if (mb['@'] < d) return false;
      mb['@'] -= d;
    }
    return true;
  };

  bool ok = f(ms,mt) && f(mt,ms);
  cout << (ok ? "Yes" : "No") << endl;
  return 0;
}

D - Bitmask

ビットが ? のところを貪欲に上から n 以下になるように立てていく

#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
#define endl '\n'
using ll = long long;

int main() {
  string s; cin >> s;
  ll n; cin >> n;

  int sz = s.size();
  ll ans = 0;
  REP(i,sz) if (s[i] == '1') ans += (1LL << (sz-i-1));
  REP(i,sz) {
    if (s[i] != '?') continue;
    ll now = 1LL << (sz-i-1);
    if (ans + now <= n) ans += now;
  }

  cout << (ans > n ? -1 : ans) << endl;
  return 0;
}