SMU Summer 2023 Contest Round 2

发布时间 2023-08-03 12:46:51作者: battle_123

Problem - A Treasure Hunt

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N = 1e5 + 50, M = 5e3 + 50, mod = 9901, MAX_N = 6e3 + 50, INF = 0x3f3f3f3f;
const double PI = 3.1415926;
#define IOS ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)

int x, x2, y, y2, a, b;
int main () {
    IOS;
    cin >> x >> y >> x2 >> y2;
    cin >> a >> b;
    if(abs(y - y2) % b != 0 || abs(x - x2) % a != 0)
        cout << "No" << endl;
    else {
        int cnt1 = 0, cnt2 = 0;
        int n = abs(x - x2), m = (y - y2);
        cnt1 = n / a, cnt2 = m / b;
        if(abs(cnt1 - cnt2) % 2 == 0)
            cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}

Problem - B Makes And The Product

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N = 1e5 + 50, M = 3e5 + 50, mod = 9901, MAX_N = 6e3 + 50, INF = 0x3f3f3f3f;
const double PI = 3.1415926;
#define IOS ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
ll a[M], n;
map<ll, int> mp;
ll C[N][10];

int main () {
    IOS;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        mp[a[i]]++;
    }
    sort (a + 1, a + 1 + n);
    ll ans = 0;
    C[1][0] = C[1][1] = 1;
    for (int i = 2; i < N; i++) {
        C[i][0] = 1;
        for (int j = 1; j < 6; j++)
            C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]);
    }
    if (a[1] != a[2] && a[1] != a[3] && a[2] != a[3]) {
        ans = mp[a[1]] * mp[a[2]] * mp[a[3]];
    } else if (a[1] == a[2] && a[2] == a[3]) {
        int num = mp[a[1]];
        ans = C[num][3];
    } else if (a[1] == a[2]) {
        int num = mp[a[1]];
        ans = C[num][2];
        ans *= 1ll * mp[a[3]];
    } else if (a[2] == a[3]) {
        int num = mp[a[2]];
        ans = C[num][2];
        ans *= 1ll * mp[a[1]];
    } else if (a[1] == a[3]) {
        int num = mp[a[1]];
        ans = C[num][2];
        ans *= 1ll * mp[a[2]];
    }
    cout << ans << endl;
    return 0;
}

Problem - C Really Big Numbers

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N = 1e5 + 50, M = 3e5 + 50, mod = 9901, MAX_N = 6e3 + 50, INF = 0x3f3f3f3f;
const double PI = 3.1415926;
#define IOS ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)

ll n, s;

ll cal (ll x) {
    ll res = 0;
    while (x) {
        res += x % 10;
        x /= 10;
    }
    return res;
}

bool check (ll x) {
    if (x - cal (x) >= s) return 1;
    return 0;
}

int main () {
    IOS;
    cin >> n >> s;
    ll l = 1, r = 1e18;
    ll ans = -1;
    while (l <= r) {
        ll mid = (l + r) >> 1;
        if (check (mid)) {
            ans = mid;
            r = mid - 1;
        } else l = mid + 1;
    }
    if (ans > n || ans == -1)
        cout << 0 << endl;

    else cout << n - ans + 1;
    return 0;
}

Problem - D Imbalanced Array(单调栈)

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N = 1e6 + 50, M = 3e3 + 50, mod = 9901, MAX_N = 6e3 + 50, INF = 0x3f3f3f3f;
const double PI = 3.1415926;
#define IOS ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
ll num[N], la[N], ra[N], res, n;

int main () {
    IOS;
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> num[i];
    stack<int> s1, s2;
    for (int i = 0; i < n; ++i) {
        while (!s1.empty () && num[i] > num[s1.top ()])
            s1.pop ();
        if (!s1.empty ()) la[i] = s1.top ();
        else la[i] = -1;
        s1.push (i);
    }
    for (int i = n - 1; i >= 0; --i) {
        while (!s2.empty () && num[i] >= num[s2.top ()])
            s2.pop ();
        if (!s2.empty ()) ra[i] = s2.top ();
        else ra[i] = n;
        s2.push (i);
    }
    for (int i = 0; i < n; ++i)
        res = res + num[i] * (ra[i] - i) * (i - la[i]);
    while (!s2.empty ()) s2.pop ();
    while (!s1.empty ()) s1.pop ();
    for (int i = 0; i < n; ++i) {
        while (!s1.empty () && num[i] < num[s1.top ()])
            s1.pop ();
        if (!s1.empty ()) la[i] = s1.top ();
        else la[i] = -1;
        s1.push (i);
    }
    for (int i = n - 1; i >= 0; --i) {
        while (!s2.empty () && num[i] <= num[s2.top ()])
            s2.pop ();
        if (!s2.empty ()) ra[i] = s2.top ();
        else ra[i] = n;
        s2.push (i);
    }
    for (int i = 0; i < n; ++i)
        res = res - num[i] * (ra[i] - i) * (i - la[i]);
    cout << res << endl;
    return 0;
}