1069 - Repetitions

Click to show code.


using namespace std;
int main(void)
{
    int ans, cur;
    char last;
    string s;
    cin >> s;
    ans = cur = 0;
    last = s[0];
    for (auto c : s)
    {
        if (c == last)
            ++cur;
        else
        {
            ans = max(ans, cur);
            cur = 1;
        }
        last = c;
    }
    ans = max(ans, cur);
    cout << ans << endl;
    return 0;
}


10684 - Jackpot

Click to show code.


using namespace std;
int N;
int dp[10010];
int v[10010];
int solve(void) {
  dp[0] = max(v[0], 0);
  for (int i = 1; i < N; ++i)
    dp[i] = max(v[i] + dp[i - 1], v[i]);
  return *max_element(dp, dp + N);
}
int main(void) {
  while (cin >> N and N != 0) {
    for (int i = 0; i < N; ++i)
      cin >> v[i];
    int ans = solve();
    if (ans > 0)
      cout << "The maximum winning streak is " << ans << ".\n";
    else
      cout << "Losing streak.\n";
  }
  return 0;
}


1068 - Weird Algorithm

Click to show code.


using namespace std;
int main(void)
{
    long long n;
    cin >> n;
    while (n != 1)
    {
        cout << n << " ";
        n = (n % 2 == 0 ? n / 2 : 3 * n + 1);
    }
    cout << n;
    return 0;
}


10664 - Luggage

Click to show code.


using namespace std;
const int NMAX = 20 + 11;
const int SMAX = 200 + 11;
int w[NMAX], tsum, n;
int mem[NMAX][SMAX];
void read_integers(void)
{
    int x, i = 0;
    string s;
    tsum = 0;
    getline(cin, s);
    stringstream ss(s);
    while (ss >> x)
    {
        tsum += x;
        w[i++] = x;
    }
    n = i;
}
int dp(int pos, int capacity)
{
    int &ans = mem[pos][capacity];
    if (pos == n)
        return 0;
    if (ans != -1)
        return ans;
    if (capacity - w[pos] >= 0)
        ans = dp(pos + 1, capacity - w[pos]) + w[pos];
    return max(ans, dp(pos + 1, capacity));
}
bool solve(void)
{
    int ans;
    if (tsum % 2 == 1)
        return false;
    memset(mem, -1, sizeof(mem));
    ans = dp(0, tsum / 2);
    return ans == tsum / 2;
}
int main(void)
{
    int m;
    cin >> m;
    cin.ignore();
    while (m--)
    {
        read_integers();
        cout << (solve() ? "YES" : "NO") << endl;
    }
    return 0;
}


10405 - Longest Common Subsequence

Click to show code.


using namespace std;
using vi = vector<int>;
int solve(string s, string t)
{
    int n = (int)s.size(), m = (int)t.size();
    vector<vi> dp(n + 1, vi(m + 1, 0));
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
        {
            auto &ans = dp[i][j];
            ans = max(ans, dp[i - 1][j]);
            ans = max(ans, dp[i][j - 1]);
            if (s[i - 1] == t[j - 1])
                ans = max(ans, dp[i - 1][j - 1] + 1);
        }
    }
    return dp[n][m];
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    string s, t;
    while (getline(cin, s) and getline(cin, t))
    {
        cout << solve(s, t) << endl;
    }
    return 0;
}


1037D - Valid Bfs

Click to show code.


using namespace std;
int const NMAX = 2e5 + 11;
int n, a[NMAX];
set<int> g[NMAX];
bool bfs_check(void)
{
    int i = 0;
    queue<int> q;
    g[0].insert(1), g[1].insert(0);
    q.push(0);
    while (not q.empty())
    {
        auto p = q.front();
        q.pop();
        while (i < n and g[p].find(a[i]) != g[p].end())
            q.push(a[i++]);
    }
    return i == n;
}
int main(void)
{
    cin >> n;
    for (int i = 0; i < n - 1; ++i)
    {
        int u, v;
        cin >> u >> v;
        g[u].insert(v);
        g[v].insert(u);
    }
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    cout << (bfs_check() ? "Yes" : "No") << endl;
    return 0;
}


10341 - Solve It

Click to show code.


using namespace std;
const double EPSILON = 1e-8;
int p, q, r, s, t, u;
inline double f(double x)
{
    return p * exp(-x) + q * sin(x) + r * cos(x) + s * tan(x) + t * x * x + u;
}
int main(void)
{
    double lo, hi, y, yp, mid;
    y = 0;
    while (cin >> p >> q >> r >> s >> t >> u)
    {
        lo = 0, hi = 1;
        if (f(1) > 0 or f(0) < 0)
            cout << "No solution" << endl;
        else
        {
            do
            {
                mid = lo + (hi - lo) / 2;
                yp = f(mid);
                if (yp > 0)
                    lo = mid;
                else
                    hi = mid;
            } while (abs(y - yp) > EPSILON);
            cout << fixed << setprecision(4) << mid << endl;
        }
    }
    return 0;
}


10276 - Hanoi Tower Troubles Again

Click to show code.


using namespace std;
const int NMAX = 50;
int pegs[NMAX + 1];
int top[NMAX + 1];
bool vis[NMAX + 1];
bool isps(int x)
{
    if (x < 0)
        return false;
    int root(round(sqrt(x)));
    return x == root * root;
}
void precompute(void)
{
    int x = 1;
    while (true)
    {
        int peg;
        for (peg = 1; peg <= NMAX; ++peg)
        {
            if (top[peg] == 0 or isps(x + top[peg]))
            {
                if (not vis[peg])
                    pegs[peg - 1] = x - 1;
                vis[peg] = true;
                top[peg] = x;
                break;
            }
        }
        if (peg > NMAX)
        {
            pegs[peg - 1] = x - 1;
            return;
        }
        ++x;
    }
}
int main(void)
{
    int t, n;
    precompute();
    cin >> t;
    while (t--)
    {
        cin >> n;
        cout << pegs[n] << endl;
    }
    return 0;
}


1025 - A Spy In The Metro

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void read_n(InputIterator it, int n)
{
    copy_n(istream_iterator<T>(cin), n, it);
}
int const INF = 1e9;
int n, atime, m1, m2;
vi t, dep_left, dep_right, acc_time;
int solve(void)
{
    vector<vi> dp(n + 2, vi(atime + 1, INF));
    dp[1][0] = 0;
    for (int tt = 1; tt <= atime; ++tt)
    {
        for (int i = 1; i <= n; ++i)
        {
            auto &ans = dp[i][tt];
            ans = min(INF, dp[i][tt - 1] + 1);
            if (i - 1 >= 1 and tt - acc_time[i] >= 0 and
                binary_search((dep_left).begin(), (dep_left).end(), tt - acc_time[i]))
            {
                ans = min(ans, dp[i - 1][tt - t[i]]);
            }
            if (i + 1 <= n and tt - (acc_time[n] - acc_time[i]) >= 0 and
                binary_search((dep_right).begin(), (dep_right).end(), tt - (acc_time[n] - acc_time[i])))
            {
                ans = min(ans, dp[i + 1][tt - t[i + 1]]);
            }
        }
    }
    return dp[n][atime] == INF ? -1 : dp[n][atime];
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int tc = 1;
    while (cin >> n and n)
    {
        cin >> atime;
        t.resize(n + 1, 0), read_n(t.begin() + 2, n - 1);
        acc_time.resize(n + 1, 0), partial_sum((t).begin(), (t).end(), begin(acc_time));
        cin >> m1;
        dep_left.resize(m1, 0), read_n(dep_left.begin(), m1);
        cin >> m2;
        dep_right.resize(m2, 0), read_n(dep_right.begin(), m2);
        cout << "Case Number " << tc << ": ";
        if (auto ans = solve(); ans != -1)
            cout << ans << endl;
        else
            cout << "impossible" << endl;
        tc++;
    }
    return 0;
}


102433E - Rainbow Strings

Click to show code.


using namespace std;
using ll = long long;
const int MOD = 11092019;
const int ALPHSZ = 26;
ll freq[ALPHSZ];
inline ll m(ll a, ll b) { return (a % MOD * b % MOD) % MOD; }
inline ll p(ll a, ll b) { return (a % MOD + b % MOD) % MOD; }
int main(void)
{
    int n;
    string s;
    ll ans;
    cin >> s;
    n = (int)s.size();
    for (int i = 0; i < n; i++)
        freq[s[i] - 'a']++;
    ans = 0;
    for (int i = 0; i < ALPHSZ; ++i)
    {
        ans = p(ans, p(m(ans, freq[i]), freq[i]));
    }
    cout << ans + 1 << endl;
    return 0;
}