首页 > 其他分享 >AtCoder Beginner Contest 338

AtCoder Beginner Contest 338

时间:2024-01-27 21:58:13浏览次数:31  
标签:AtCoder 338 Beginner int ll long solve using define

AtCoder Beginner Contest 338

A - Capitalized?

代码:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;

void solve()
{
    string s;
    cin >> s;
    int n = s.size();
    for (int i = 0; i < n; i++)
    {
        if (i == 0)
        {
            if (s[i] >= 'a' && s[i] <= 'z')
            {
                puts("No");
                return;
            }
        }
        else
        {
            if (s[i] >= 'A' && s[i] <= 'Z')
            {
                puts("No");
                return;
            }
        }
    }
    puts("Yes");
}

int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}

B - Frequency

代码:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;

void solve()
{
    string s;
    cin >> s;
    vector<int> cnt(40);
    int a = 0;
    for (auto c : s)
    {
        cnt[c - 'a']++;
        a = max(a, cnt[c - 'a']);
    }
    for (int i = 0; i < 30; i++)
    {
        if (a == cnt[i])
        {
            char c = i + 'a';
            cout << c << endl;
            return;
        }
    }
}

int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}

C - Leftover Recipes

解题思路:

观察数据范围,选择枚举制造\(A\)菜的数量。

代码:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;

void solve()
{
    int n;
    cin >> n;
    vector<ll> a(n + 1), b(n + 1), q(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> q[i];
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> b[i];
    }
    ll ans = 0;
    for (int i = 0; i <= 1e6 + 1; i++)
    {
        vector<ll> t = q;
        bool f = true;
        for (int j = 1; j <= n; j++)
        {
            ll cur = i * a[j];
            if (cur <= t[j])
            {
                t[j] -= cur;
            }
            else
            {
                f = false;
                break;
            }
        }
        if (!f)
        {
            // cout << i << endl;
            break;
        }
        ll res = 1e18;
        for (int j = 1; j <= n; j++)
        {
            if (b[j] > 0)
            {
                res = min(res, t[j] / b[j]);
            }
        }
        ans = max(ans, res + i);
    }
    cout << ans << endl;
}

int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}

D - Island Tour

解题思路:

从\(a\to b\),一共就两个方向,也就是两种走法。

我们先在环上,计算出最短走法。对于该走法,使用差分数组,记录每条边被经过了多少次,以及经过该边的路径长度总和。

接下来,我们枚举删去边。

如果我们删去改变,意味着,凡是经过改变的路径全部不可走。此时,只能走另一个反向。

假设经过边\(i\)的路径长度之和为\(b[i]\),路径数量为\(c[i]\),则全部路径反走的长度之和为\(n * c[i] - b[i]\)。

因为正反两个走法的长度之和为\(n\)。

代码:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<ll> a(max(n, m) + 10);
    vector<ll> b(max(n, m) + 10), c(max(n, m) + 10);
    ll sum = 0;
    for (int i = 1; i <= m; i++)
    {
        cin >> a[i];
        if (i > 1)
        {
            if (a[i] > a[i - 1])
            {
                int x = a[i] - a[i - 1];
                int y = a[i - 1] + n - a[i];
                if (x < y)
                {
                    sum += x;
                    b[a[i - 1]] += x;
                    b[a[i]] -= x;
                    c[a[i - 1]] += 1;
                    c[a[i]] -= 1;
                }
                else if (x >= y)
                {
                    sum += y;
                    if (a[i - 1] != 1)
                    {
                        b[1] += y;
                        b[a[i - 1]] -= y;
                        c[1] += 1;
                        c[a[i - 1]] -= 1;
                    }
                    b[a[i]] += y;
                    c[a[i]] += 1;
                }
            }
            else
            {
                int x = a[i - 1] - a[i];
                int y = a[i] + n - a[i - 1];
                if (x < y)
                {
                    sum += x;
                    b[a[i]] += x;
                    b[a[i - 1]] -= x;
                    c[a[i]] += 1;
                    c[a[i - 1]] -= 1;
                }
                else if (x >= y)
                {
                    sum += y;
                    if (a[i] != 1)
                    {
                        b[1] += y;
                        b[a[i]] -= y;
                        c[1] += 1;
                        c[a[i]] -= 1;
                    }
                    b[a[i - 1]] += y;
                    c[a[i - 1]] += 1;
                }
            }
        }
    }
    ll res = 0;
    ll ans = 1e18;
    for (int i = 1; i <= n; i++)
    {
        b[i] += b[i - 1];
        c[i] += c[i - 1];
        res = c[i] * n - b[i];
        ans = min(ans, sum + res - b[i]);
    }
    cout << ans << endl;
}

int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}

E - Chords

解题思路:

我们将每条连线表述为\((l,r)\),其中\(l < r\)。

相交条件就是存在一对连线\((l_1,r_1),(l_2,r_2)\),有\(l_1 < l_2 < r_1,r_2 > r_1\)。

所以,我们先排序。

对于第\(i\)条连线,找到所有满足\(l_i <l <r_i\)的连线,然后取区间中最大的\(r\)与\(r_i\)比较,存在符合则存在相交。

代码:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;

void solve()
{
    int n;
    cin >> n;
    vector<pii> v(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> v[i].fi >> v[i].se;
        if (v[i].fi > v[i].se)
        {
            swap(v[i].fi, v[i].se);
        }
    }
    sort(v.begin() + 1, v.end());
    vector<vector<int>> f(n + 10, vector<int>(22));
    for (int j = 0; j <= 20; j++)
    {
        for (int i = 1; i + (1ll << j) - 1 <= n; i++)
        {
            if (j == 0)
            {
                f[i][j] = v[i].se;
            }
            else
            {
                f[i][j] = max(f[i][j - 1], f[i + (1ll << (j - 1))][j - 1]);
            }
        }
    }

    auto get = [&](int l, int r)
    {
        int len = r - l + 1;
        int k = log(len) / log(2);
        return max(f[l][k], f[r - (1ll << k) + 1][k]);
    };

    for (int i = 1; i <= n; i++)
    {
        auto idx = lower_bound(v.begin() + 1, v.end(), (pii){v[i].se, -1}) - v.begin() - 1;
        int l = i + 1;
        int r = idx;
        if (l > r)
        {
            continue;
        }
        ll val = get(l, r);
        if (val > v[i].se)
        {
            puts("Yes");
            return;
        }
    }
    puts("No");
}

int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}

标签:AtCoder,338,Beginner,int,ll,long,solve,using,define
From: https://www.cnblogs.com/value0/p/17991977

相关文章

  • AtCoder abc336 A-D题代码
    A题:#include<bits/stdc++.h>usingnamespacestd;intmain(){intn;cin>>n;cout<<"L"<<string(n,'o')<<"ng"<<endl;return0;}B题:#include<bits/stdc++......
  • AtCoder Beginner Contest 336
    所有代码都在如下模板中运行#include<bits/stdc++.h>usingnamespacestd;namespacegza{ #defineintlonglong #definepbpush_back #defineMTintTTT=R;while(TTT--) #definepcputchar #defineRread() #definefo(i,a,b)for(inti=a;i<=b;i++) #definerep(......
  • P4338 历史笔记
    神题啊!神题(赞叹)题意形式化题意:给定一棵\(n\)个点的树,第\(i\)个点有点权\(a_i\)。且每个点都有颜色,初始时颜色都为\(1\),第\(i\)个点的颜色是\(c_i\)。你可以对一个点\(x\)进行一次操作:计数有多少\(v\),满足\(v\)在\(x\to1\)的路径(包含\(x\)和\(1\))上,且......
  • P3389 【模板】高斯消元法
    #include<bits/stdc++.h>usingnamespacestd;doublemax(doublea,doubleb){ if(a>=b)returna; if(a<b)returnb;}intn;doublea[1010][1010];doublea1[1010][1010];intmain(){ scanf("%d",&n); for(inti=1;i<=n;i++) { ......
  • AtCoder ABC 266 复盘
    AMiddleLetter水沝淼㵘纯模拟题。根据题意,易得答案。ACCodeBModuloNumber模拟(+数学?)。先\(N\leftarrowN\bmod998244353\),然后\(N\leftarrowN+998244353\(N<0)\),最后输出\(N\)。ACCodeCConvexQuadrilateral数学。有一个公式判断(名字我忘了)可以判断。详见ACC......
  • AtCoder Regular Contest 170 A-C
    A-YetAnotherABProblem贪心。定义下标\(i\)满足\(S[i]=B,T[i]=A\)为\(BA\)型,\(S[i]=B,T[i]=A\)为\(AB\)型,\(AA\)型、\(BB\)型同理。对所有\(BA\)型的下标\(i\)去匹配其右侧的第一个\(AB\)型的下标\(j\),匹配成功则对下标\(i\)和\(j\)进行操作,若无法匹配,则对剩余的\(BA\)型......
  • AtCoder Regular Contest 170 D Triangle Card Game
    洛谷传送门AtCoder传送门赛后调了40min,哈哈。首先先把\(a,b\)排序。考虑先枚举Alice选的数\(a_i\),然后若\(\forallj,\existsk\nei,(a_i,b_j,a_k)\)能组成三角形,Alice就赢了。考虑简化条件。\((x,y,z)\)能形成三角形的充要条件是\(z\in(|x-y|,x+......
  • Toyota Programming Contest 2024#1(AtCoder Beginner Contest 337)
    ToyotaProgrammingContest2024#1(AtCoderBeginnerContest337)A-Scoreboard代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;usingpii=pair<ll,ll>;#definefifirst#definesesecondusingi128=__int128_t;void......
  • AtCoder Beginner Contest 337
    基本情况ABC秒了,D数组在空间复杂度上面第一次疯狂吃亏,吃了两次罚时过。赛后看官方题解,发现C做法薄纱我。C-LiningUp2https://atcoder.jp/contests/abc337/tasks/abc337_c这题一眼链表,我用双向链表实现,代码石山。官方题解就题论题,更本质。其实这题并没必要开双向链......
  • Toyota Programming Contest 2024#1(AtCoder Beginner Contest 337)
    ToyotaProgrammingContest2024#1(AtCoderBeginnerContest337)比赛链接A-Scoreboard思路简单的模拟,统计一下总分数就可以了Code#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongvoidsolve(){ intn; intans1=0; intans2=0; cin>>n; for......