1645 - Nearest Smaller Values

Click to show code.


using namespace std;
using ii = pair<int, int>;
int main(void)
{
    int n, ai;
    stack<ii> S;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> ai;
        while (!S.empty() and S.top().first >= ai)
            S.pop();
        if (S.empty())
            cout << "0 ";
        else
            cout << S.top().second + 1 << " ";
        S.push({ai, i});
    }
    return 0;
}


1644 - Maximum Subarray Sum Ii

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);
}
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void write(InputIterator first, InputIterator last, const char *delim = "\n")
{
    copy(first, last, ostream_iterator<T>(cout, delim));
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int n, minsz, maxsz;
    cin >> n >> minsz >> maxsz;
    vector<ll> a(n + 1, 0), psum(n + 1, 0);
    read_n(next(a.begin()), n);
    for (int i = 1; i <= n; ++i)
        psum[i] = psum[i - 1] + a[i];
    multiset<ll> window;
    ll ans = -1e18;
    window.insert(psum[0]);
    for (int i = minsz; i <= n; ++i)
    {
        ans = max(ans, psum[i] - *window.begin());
        if (i - minsz + 1 > 0)
            window.insert(psum[i - minsz + 1]);
        if (i - maxsz >= 0)
            window.erase(window.find(psum[i - maxsz]));
    }
    cout << ans << endl;
    return 0;
}


1643 - Maximum Subarray Sum

Click to show code.


using namespace std;
using ll = long long;
using vll = vector<ll>;
int main(void)
{
    ll n, ans = INT_MIN;
    cin >> n;
    vll x(n), dp(n + 1, 0);
    for (auto &xi : x)
        cin >> xi;
    for (int i = 1; i <= n; ++i)
        dp[i] = max(dp[i - 1] + x[i - 1], x[i - 1]);
    for (int i = 1; i <= n; ++i)
        ans = max(dp[i], ans);
    cout << ans << endl;
    return 0;
}


1642 - Sum Of Four Values

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int const NMAX = 1e3 + 11;
int n, x, a[NMAX];
map<ll, vector<ii>> sums;
int main(void)
{
    cin >> n >> x;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    for (int i = 0; i < n - 1; ++i)
        for (int j = i + 1; j < n; ++j)
            sums[a[i] + a[j]].emplace_back(i + 1, j + 1);
    for (auto [sum, positions] : sums)
    {
        if (auto it = sums.find(x - sum); it != sums.end())
        {
            for (auto [i, j] : positions)
            {
                for (auto [k, l] : it->second)
                {
                    if (i != k and i != l and j != k and j != l)
                    {
                        cout << i << " " << j << " " << k << " " << l << endl;
                        return 0;
                    }
                }
            }
        }
    }
    cout << "IMPOSSIBLE" << endl;
    return 0;
}


1641 - Sum Of Three Values

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
using vii = vector<ii>;
int main(void)
{
    int n;
    ll x;
    cin >> n >> x;
    vii a(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i].first;
        a[i].second = i;
    }
    sort(a.begin(), a.end());
    for (int i = 0; i < n; ++i)
    {
        int target = x - a[i].first;
        for (int j = i + 1, k = n - 1; j < k; ++j)
        {
            while (j < k and a[j].first + a[k].first > target)
                --k;
            if (j < k and a[j].first + a[k].first == target)
            {
                cout << a[i].second + 1 << " " << a[j].second + 1 << " "
                     << a[k].second + 1 << endl;
                return 0;
            }
        }
    }
    cout << "IMPOSSIBLE" << endl;
    return 0;
}


1640 - Sum Of Two Values

Click to show code.


using namespace std;
using ii = pair<int, int>;
using vii = vector<ii>;
int main(void)
{
    int n, x;
    cin >> n >> x;
    vii a(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i].first;
        a[i].second = i;
    }
    sort(a.begin(), a.end());
    for (int i = 0; i < n; ++i)
    {
        auto it = lower_bound(
            a.begin() + i + 1, a.end(), make_pair(x - a[i].first, 0));
        if (it != a.end() and it->first == x - a[i].first)
        {
            cout << a[i].second + 1 << " " << it->second + 1 << endl;
            return 0;
        }
    }
    cout << "IMPOSSIBLE" << endl;
    return 0;
}


1639 - Edit Distance

Click to show code.


using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 1e9 + 11;
int main(void)
{
    int n, m;
    string s, t;
    cin >> s >> t;
    n = s.size(), m = t.size();
    vvi dp(n + 1, vi(m + 1, INF));
    dp[0][0] = 0;
    for (int i = 1; i <= n; ++i)
        dp[i][0] = i;
    for (int j = 1; j <= m; ++j)
        dp[0][j] = j;
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
        {
            int &ans = dp[i][j];
            ans = min(
                {dp[i][j - 1] + 1,
                 dp[i - 1][j] + 1,
                 dp[i - 1][j - 1] + (s[i - 1] != t[j - 1])});
        }
    }
    cout << dp[n][m] << endl;
    return 0;
}


1638 - Grid Paths

Click to show code.


using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
const int MOD = 1e9 + 7;
int main(void)
{
    int n;
    char x;
    cin >> n;
    vvi free_cell(n, vi(n, 0));
    vvi dp(n + 1, vi(n + 1, 0));
    for (int r = 0; r < n; ++r)
    {
        for (int c = 0; c < n; ++c)
        {
            cin >> x;
            free_cell[r][c] = (x != '*');
        }
    }
    dp[1][1] = (free_cell[0][0]);
    for (int r = 1; r <= n; ++r)
    {
        for (int c = 1; c <= n; ++c)
        {
            if ((r == 1 and c == 1) or not free_cell[r - 1][c - 1])
                continue;
            dp[r][c] = (dp[r - 1][c] + dp[r][c - 1]) % MOD;
        }
    }
    cout << dp[n][n] << endl;
    return 0;
}


1637 - Removing Digits

Click to show code.


using namespace std;
int main(void)
{
    int n, num, x, ans;
    cin >> n;
    ans = 0;
    while (n != 0)
    {
        x = n;
        num = 0;
        while (x != 0)
        {
            num = max(num, x % 10);
            x /= 10;
        }
        n -= num;
        ans += 1;
    }
    cout << ans << endl;
    return 0;
}


1636 - Coin Combinations Ii

Click to show code.


using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
const int MOD = 1e9 + 7;
int solve(int n, int x, const vi &c)
{
    vvi dp(n + 1, vi(x + 1, 0));
    for (int i = 1; i <= n; ++i)
        dp[i][0] = 1;
    for (int i = 1; i <= n; ++i)
    {
        for (int s = 1; s <= x; ++s)
        {
            int &ans = dp[i][s];
            ans = dp[i - 1][s];
            if (s - c[i - 1] >= 0)
                ans = (ans + dp[i][s - c[i - 1]]) % MOD;
        }
    }
    return dp[n][x];
}
int main(void)
{
    int n, x;
    cin >> n >> x;
    vi c(n);
    for (auto &ci : c)
        cin >> ci;
    sort(c.begin(), c.end());
    cout << solve(n, x, c) << endl;
    return 0;
}