1313A - Fast Food Restaurant

Click to show code.


using namespace std;
int ps[7][3] = {{0, 0, 1},
                {0, 1, 0},
                {0, 1, 1},
                {1, 0, 0},
                {1, 0, 1},
                {1, 1, 0},
                {1, 1, 1}};
unsigned char set(unsigned char u, int pos)
{
    u |= 1UL << pos;
    return u;
}
unsigned char clear(unsigned char u, int pos)
{
    u &= ~(1UL << pos);
    return u;
}
bool get(unsigned char u, int pos) { return u & (1UL << pos); }
int solve(int a, int b, int c, unsigned char &taken)
{
    int ans = 0;
    unsigned char maxtaken = taken;
    for (int i = 0; i < 7; ++i)
    {
        unsigned char localtaken = maxtaken;
        auto [ap, bp, cp] = ps[i];
        if (get(taken, i))
            continue;
        if (a - ap < 0 or b - bp < 0 or c - cp < 0)
            continue;
        localtaken = set(localtaken, i);
        int cur = 1 + solve(a - ap, b - bp, c - cp, localtaken);
        if (cur > ans)
        {
            ans = cur;
            maxtaken = localtaken;
        }
        localtaken = clear(localtaken, i);
    }
    taken = maxtaken;
    return ans;
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        int a, b, c, ans;
        unsigned char taken = 0;
        cin >> a >> b >> c;
        ans = solve(a, b, c, taken);
        cout << ans << endl;
    }
    return 0;
}


1312B - Bogosort

Click to show code.


using namespace std;
const int NMAX = 100 + 11;
int n, a[NMAX];
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 0; i < n; ++i)
            cin >> a[i];
        sort(a, a + n, greater<int>());
        for (int i = 0; i < n; ++i)
            cout << a[i] << " ";
        cout << endl;
    }
    return 0;
}


1312A - Two Regular Polygons

Click to show code.


using namespace std;
int main(void)
{
    int t, n, m;
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        cout << (n % m == 0 ? "YES" : "NO") << endl;
    }
    return 0;
}


1311A - Add Odd Subtract Even

Click to show code.


using namespace std;
using ll = long long;
ll solve(ll a, ll b)
{
    ll diff = b - a;
    if (diff == 0)
        return 0;
    else if (diff > 0 and diff % 2 == 1)
        return 1;
    else if (diff < 0 and abs(diff) % 2 == 0)
        return 1;
    else
        return 2;
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        ll a, b, ans;
        cin >> a >> b;
        ans = solve(a, b);
        cout << ans << endl;
    }
    return 0;
}


1305C - Kuroni Impossible Calculation

Click to show code.


using namespace std;
using ll = long long;
int n, m;
int a[200010];
ll solve(void)
{
    ll ans = 1;
    sort(a, a + n, greater<int>());
    for (int i = 0; i < n - 1; ++i)
    {
        for (int j = i + 1; j < n; ++j)
            ans = (ans * (a[i] - a[j]) % m) % m;
    }
    return ans;
}
int main(void)
{
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    if (n <= m)
        cout << solve() << endl;
    else
        cout << 0 << endl;
    return 0;
}


1305B - Kuroni Simple Strings

Click to show code.


using namespace std;
using vi = vector<int>;
string s;
void solve(void)
{
    vi lpar_i;
    vi rpar_i;
    for (int i = 0; i < s.size(); ++i)
    {
        if (s[i] == '(')
            lpar_i.push_back(i + 1);
        else
            rpar_i.push_back(i + 1);
    }
    queue<int> l;
    stack<int> r;
    if (rpar_i.size() > 0 and lpar_i.size() > 0)
    {
        int i = 0;
        int j = rpar_i.size() - 1;
        while (i < lpar_i.size() and j >= 0 and lpar_i[i] < rpar_i[j])
        {
            l.push(lpar_i[i]);
            r.push(rpar_i[j]);
            ++i;
            --j;
        }
    }
    int n = l.size() + r.size();
    cout << (n > 0 ? 1 : 0) << endl;
    if (n > 0)
        cout << n << endl;
    while (l.size())
    {
        cout << l.front() << " ";
        l.pop();
    }
    while (r.size())
    {
        cout << r.top() << " ";
        r.pop();
    }
    cout << endl;
}
int main(void)
{
    cin >> s;
    solve();
    return 0;
}


1305A - Kuroni And Gifts

Click to show code.


using namespace std;
int n;
int a[1010];
int b[1010];
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 0; i < n; ++i)
            cin >> a[i];
        for (int i = 0; i < n; ++i)
            cin >> b[i];
        sort(a, a + n);
        sort(b, b + n);
        for (int i = 0; i < n; ++i)
            cout << a[i] << " ";
        cout << endl;
        for (int i = 0; i < n; ++i)
            cout << b[i] << " ";
    }
    return 0;
}


1294B - Collecting Packages

Click to show code.


using namespace std;
using ll = long long;
int n;
int X[1010];
int Y[1010];
void sort_points(int n) {
  pair<int, int> pairt[n];
  for (int i = 0; i < n; i++) {
    pairt[i].first = X[i];
    pairt[i].second = Y[i];
  }
  sort(pairt, pairt + n);
  for (int i = 0; i < n; i++) {
    X[i] = pairt[i].first;
    Y[i] = pairt[i].second;
  }
}
pair<bool, string> solve(int n) {
  sort_points(n);
  string res = "";
  int last_x = 0;
  int last_y = 0;
  for (int i = 0; i < n; ++i) {
    if (Y[i] < last_y)
      return {false, res};
    int dy = Y[i] - last_y;
    int dx = X[i] - last_x;
    last_x = X[i];
    last_y = Y[i];
    for (int j = 0; j < dx; ++j)
      res += "R";
    for (int j = 0; j < dy; ++j)
      res += "U";
  }
  return {true, res};
}
int main(void) {
  int t;
  cin >> t;
  for (int i = 0; i < t; ++i) {
    cin >> n;
    for (int j = 0; j < n; ++j) {
      cin >> X[j] >> Y[j];
    }
    auto [b, res] = solve(n);
    if (b) {
      cout << "YES\n" << res << endl;
    } else
      cout << "NO\n";
  }
  return 0;
}


1294A - Collecting Coins

Click to show code.


using namespace std;
using ll = long long;
bool solve(ll *coins, ll n) {
  sort(coins, coins + 3);
  ll res = n - ((coins[2] - coins[1]) + (coins[2] - coins[0]));
  ll sum = accumulate(coins, coins + 3, 0);
  if ((res < 0) or (res % 3 != 0)) {
    return false;
  }
  if (((sum + n) % 3 != 0) or ((3 * coins[2] + res) != (sum + n))) {
    return false;
  }
  return true;
}
int main(void) {
  int t;
  cin >> t;
  while (t--) {
    ll coins[3], n;
    cin >> coins[0] >> coins[1] >> coins[2] >> n;
    if (solve(coins, n)) {
      cout << "YES\n";
    } else {
      cout << "NO\n";
    }
  }
  return 0;
}


1288B - Yet Another Meme Problem

Click to show code.


using namespace std;
using ll = long long;
ll solve(ll A, ll B) {
  ll count = 0;
  ll maxdig = log10(B) + 1;
  count = maxdig - 1;
  if (B == (ll)pow(10, maxdig) - 1)
    ++count;
  return A * count;
}
int main(void) {
  ll t;
  cin >> t;
  while (t--) {
    ll A, B;
    cin >> A >> B;
    ll n = solve(A, B);
    cout << n << endl;
  }
  return 0;
}