Codeforces Round 955 (Div. 2, with prizes from NEAR!)

Codeforces Round 955 (Div. 2, with prizes from NEAR!)

A. Soccer

解题思路:

a区间为[x1,x2],b区间为[y1,y2]。二者不相交则不可能出现同分。

代码:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i128 = __int128_t;
typedef pair<int, int> pii;
#define fi first
#define se second
const int N = 1e5 + 10;

void solve()
{
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a < b)
    {
        if (c >= d)
        {
            puts("NO");
        }
        else
        {
            puts("YES");
        }
    }
    else if (a > b)
    {
        if (c <= d)
        {
            puts("NO");
        }
        else
        {
            puts("YES");
        }
    }
    else
    {
        puts("YES");
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

B. Collatz Conjecture

解题思路:

每次xy后,x至少减少一半,所以在logx次内x一定会到达1

到达1后很好讨论,达到1前按题意模拟即可。

代码:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i128 = __int128_t;
typedef pair<int, int> pii;
#define fi first
#define se second
const int N = 1e5 + 10;

void solve()
{
    ll x, y, k;
    cin >> x >> y >> k;
    ll a = y - (x % y);
    while (k > 0)
    {
        if (k >= a)
        {
            k -= a;
        }
        else
        {
            x += k;
            k = 0;
            break;
        }
        x += a;
        while (x % y == 0)
        {
            x /= y;
            if (x == 1)
            {
                x += (k % (y - 1));
                while (x % y == 0)
                {
                    x /= y;
                }
                k = 0;
            }
        }
        a = y - (x % y);
    }
    cout << x << "\n";
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

C. Boring Day

解题思路:

  • ai>r:i处一定会将序列切断。

  • lair:单独算一次贡献一定最优。

  • ai<l:考虑往后接着凑s=ai+ai+1+...+aj

    • s>r:i指针右移。

    • lsr:如果凑出来的段在范围内,那么直接算一次贡献。

    • s<l:继续往后凑。

代码:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i128 = __int128_t;
typedef pair<int, int> pii;
#define fi first
#define se second
const int N = 1e5 + 10;

void solve()
{
    ll n, l, r;
    cin >> n >> l >> r;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    ll s = 0;
    int ans = 0;
    for (int i = 1, j = 1; i <= n; i++)
    {
        while (s < l && j <= n)
        {
            if (a[j] > r)
            {
                s = 0;
                i = j;
                j = i + 1;
                break;
            }
            if (a[j] >= l)
            {
                ans++;
                i = j;
                j = i + 1;
                s = 0;
                break;
            }
            if (a[j] < l)
            {
                if (s + a[j] > r)
                {
                    s -= a[i];
                    break;
                }
                if (s + a[j] >= l && s + a[j] <= r)
                {
                    s = 0;
                    ans++;
                    i = j;
                    j = i + 1;
                    break;
                }
                else if (s + a[j] < l)
                {
                    s += a[j];
                    j++;
                }
            }
        }
    }
    cout << ans << "\n";
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

D. Beauty of the mountains

解题思路:

计算出不同山峰的总和差s

如果子矩阵能凑出总和差的因子,那么就有解。

我们考虑每次子矩阵增加最小单位,求所有子矩阵可操作最小单位的公因数g

s%g==0,那么有解,反之无解。

代码:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i128 = __int128_t;
typedef pair<int, int> pii;
#define fi first
#define se second
const int N = 1e5 + 10;

ll gcd(ll a, ll b)
{
    return b ? gcd(b, a % b) : a;
}

void solve()
{
    int n, m, k;
    cin >> n >> m >> k;
    vector<vector<ll>> a(n + 1, vector<ll>(m + 1)), s(n + 1, vector<ll>(m + 1));
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> s[i][j];
        }
    }
    vector<string> g(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> g[i];
        g[i] = ' ' + g[i];
    }
    ll dis = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (g[i][j] == '1')
            {
                dis += s[i][j];
            }
            else
            {
                dis -= s[i][j];
            }
        }
    }
    dis = abs(dis);
    if (dis == 0)
    {
        cout << "YES\n";
        return;
    }
    map<ll, bool> vis;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            s[i][j] = (g[i][j] == '1' ? 1 : -1);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
        }
    }
    auto get = [&](int x1, int y1, int x2, int y2) -> ll
    {
        return s[x2][y2] + s[x1 - 1][y1 - 1] - s[x1 - 1][y2] - s[x2][y1 - 1];
    };
    set<ll> b;
    for (int i = k; i <= n; i++)
    {
        for (int j = k; j <= m; j++)
        {
            ll res = get(i - k + 1, j - k + 1, i, j);
            b.insert(res);
        }
    }
    ll res = 0;
    for (auto x : b)
    {
        res = gcd(res, x);
    }
    if (res == 0)
    {
        cout << "NO\n";
        return;
    }
    if (dis % res == 0)
    {
        cout << "YES\n";
        return;
    }
    cout << "NO\n";
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
posted @   value0  阅读(499)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示