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

AtCoder Beginner Contest 332

时间:2023-12-12 18:57:31浏览次数:35  
标签:AtCoder typedef const Beginner int ll long 332 solve

AtCoder Beginner Contest 332

A - Online Shopping

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
typedef pair<ll, ll> pii;

void solve()
{
    int n, s, k;
    cin >> n >> s >> k;
    vector<int> v(n + 1);
    ll m = 0;
    for (int i = 1; i <= n; i++)
    {
        int a, b;
        cin >> a >> b;
        m += a * b;
    }
    if (m >= s)
    {
        cout << m << endl;
    }
    else
    {
        cout << m + k << endl;
    }
}

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

    return 0;
}

B - Glass and Mug

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
typedef pair<ll, ll> pii;

void solve()
{
    int k, g, m;
    cin >> k >> g >> m;
    int a = 0;
    int b = 0;
    for (int i = 1; i <= k; i++)
    {
        if (a == g)
        {
            a = 0;
        }
        else if (b == 0)
        {
            b = m;
        }
        else
        {
            int t = min(b, g - a);
            a += t;
            b -= t;
        }
    }
    cout << a << ' ' << b << endl;
}

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

    return 0;
}

C - T-shirts

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
typedef pair<ll, ll> pii;

void solve()
{
    int n, m;
    cin >> n >> m;
    string s;
    cin >> s;
    vector<int> cnt(5, 0);
    ll ans = 0;
    cnt[1] = m;
    int i = 0;
    for (auto c : s)
    {
        if (c == '0')
        {
            cnt[2] = 0;
            cnt[1] = m;
        }
        else if (c == '1')
        {
            if (cnt[1] > 0)
            {
                cnt[1]--;
            }
            else
            {
                cnt[2]++;
            }
        }
        else
        {
            cnt[2]++;
        }
        ans = max(ans, ((ll)cnt[2]));
        // cout << (i++) << ' ' << abs(cnt[1]) + (ll)abs(cnt[2]) << endl;
    }
    ans = max(ans, ((ll)cnt[2]));
    cout << ans << endl;
}

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

    return 0;
}

D - Swapping Puzzle

解题思路:

\(bfs\)搜索所有状态,遇到第一个和\(b\)一样的变换距离就是最短变换距离。没遇到就是无解。

所有状态数\(5! \times5! = 14400\)。

当然,也可以直接枚举所有状态,一一判断是否和\(b\)相等。变换距离为行列序号最终逆序对数量。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
typedef pair<ll, ll> pii;

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n + 1, vector<int>(m + 1)), b(n + 1, vector<int>(m + 1));
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> b[i][j];
        }
    }

    map<vector<vector<int>>, int> d;
    queue<vector<vector<int>>> q;
    d[a] = 0;
    q.push(a);
    while (q.size())
    {
        auto t = q.front();
        q.pop();
        if (t == b)
        {

            cout << d[t] << endl;
            return;
        }
        bool f = false;
        for (int i = 1; i < n; i++)
        {
            // cout << i << endl;
            auto r = t;
            swap(r[i], r[i + 1]);
            if (r == b)
            {
                cout << d[t] + 1 << endl;
                return;
            }
            if (!d.count(r))
            {
                d[r] = d[t] + 1;
                q.push(r);
            }
        }
        for (int i = 1; i < m; i++)
        {
            // cout << i << endl;
            auto r = t;
            for (int j = 1; j <= n; j++)
            {
                swap(r[j][i], r[j][i + 1]);
            }
            if (r == b)
            {
                cout << d[t] + 1 << endl;
                return;
            }
            if (!d.count(r))
            {
                d[r] = d[t] + 1;
                q.push(r);
            }
        }
        // cout << "dfsa" << endl;
    }

    cout << -1 << endl;
}

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

    return 0;
}

E - Lucky bag

解题思路:

数据很小,可贪心随机化。

遍历物品,再所有物品集,将当前的物品放入总和最小的物品集,如此分配。

建议看看状态压缩dp写法。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
typedef pair<ll, ll> pii;

void solve()
{
    int n, d;
    cin >> n >> d;
    vector<int> w(n + 1);
    double s = 0;
    for (int i = 1; i <= n; i++)
    {
        cin >> w[i];
        s += w[i];
    }
    double avg = s / d;
    int t = 2000000;
    double ans = 1e18;
    while (t--)
    {
        int k = 1;
        s = 0;
        vector<int> b(d + 1, 0);
        random_shuffle(w.begin() + 1, w.end());
        for (int j = 1; j <= n; j++)
        {
            for (int i = 1; i <= d; i++)
            {
                if (b[i] < b[k])
                {
                    k = i;
                }
            }
            b[k] += w[j];
            k = 1;
        }
        for (int i = 1; i <= d; i++)
        {
            s += (b[i] - avg) * (b[i] - avg);
        }
        s = s / d;
        ans = min(1.0 * s, ans);
    }
    printf("%.15lf", ans);
}

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

    return 0;
}

F - Random Update Query

解题思路:

我们在\((l,r)\)中随机选取一个位置的概率为\(\frac{1}{r - l + 1}\),修改后的值为\(x\),所以该位置不变的概率为\(\frac{r-l}{r-l+1}\)。

所以,对于区间中每个位置的期望值影响为\(val = val \times \frac{r-l}{r-l + 1} + \frac{x}{r-l + 1}\)。

区间乘法和加法,懒标记线段树。

代码:

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
const int N = 2e5 + 10;
const int Base = N / 2;
const int M = 2 * N;
const int P = 998244353;
typedef pair<int, int> PII;
typedef long long ll;
typedef long long LL;
int n, m;
ll w[N];
vector<int> primes;
const LL mod = 1e9 + 7;
unordered_map<char, int> q;
const int p = 998244353;
string s;
vector<double> ys;

ll qmi(ll a, ll b)
{
    ll res = 1;
    while (b)
    {
        if (b & 1)
        {
            res = (res * a) % p;
        }
        b >>= 1;
        a = (a * a) % p;
    }
    return res;
}

struct Node
{
    int l, r;
    LL sum, add, mul;
} tr[N * 4];

void pushup(int u)
{
    tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum % p;
}

void eval(Node &u, LL add, LL mul)
{
    u.sum = (u.sum * mul % p + ((LL)add * (u.r - u.l + 1))) % p;
    u.mul = u.mul * mul % p;
    u.add = ((u.add * mul % p) + add) % p;
}

void pushdown(int u)
{
    eval(tr[u << 1], tr[u].add, tr[u].mul);
    eval(tr[u << 1 | 1], tr[u].add, tr[u].mul);
    tr[u].add = 0;
    tr[u].mul = 1;
}

void build(int u, int l, int r)
{
    if (l == r)
    {
        tr[u] = {l, r, w[r], 0, 1};
        return;
    }
    tr[u] = {l, r, 0, 0, 1}; // 这道题记得初始化mul = 1
    int mid = l + r >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    pushup(u);
}

void modify(int u, int l, int r, LL add, LL mul)
{
    if (tr[u].l >= l && tr[u].r <= r)
    {
        eval(tr[u], add, mul);
        return;
    }
    pushdown(u);
    int mid = tr[u].l + tr[u].r >> 1;
    if (l <= mid)
    {
        modify(u << 1, l, r, add, mul);
    }
    if (r > mid)
    {
        modify(u << 1 | 1, l, r, add, mul);
    }
    pushup(u);
}

LL query(int u, int l, int r)
{
    if (tr[u].l >= l && tr[u].r <= r) // 这里条件写错了
    {
        return tr[u].sum;
    }
    pushdown(u);
    int mid = tr[u].l + tr[u].r >> 1;
    LL res = 0;
    if (l <= mid)
    {
        res = query(u << 1, l, r);
    }
    if (r > mid)
    {
        res = (res + query(u << 1 | 1, l, r)) % p;
    }
    return res;
}

void solve()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> w[i];
    }
    build(1, 1, n);
    // cout << 1 << endl;
    for (int i = 1; i <= m; i++)
    {
        ll l, r, x;
        cin >> l >> r >> x;
        ll len = r - l + 1;
        modify(1, l, r, (x * qmi(len, p - 2)) % p, (len - 1) * qmi(len, p - 2) % p);
        // cout << (x * qmi(len, mod - 2)) % mod << ' ' << (len - 1) * qmi(len, mod - 2) % mod << endl;
    }
    for (int i = 1; i <= n; i++)
    {
        cout << query(1, i, i) << ' ';
    }
}
int main()
{
    int t;
    // init(N);
    t = 1;
    while (t--)
    {
        solve();
    }

    return 0;
}

标签:AtCoder,typedef,const,Beginner,int,ll,long,332,solve
From: https://www.cnblogs.com/value0/p/17897586.html

相关文章

  • AtCoder Grand Contest 001
    比赛链接A-BBQEasy从小到大排序以后,答案就是所有奇数位置之和。B-MysteriousLight发现去掉前两次反射以后,剩下的是一个在平行四边形内反射的过程,且形式类似于辗转相除。具体地,\[F(n,x)=\begin{cases} -n&x=0\\ 2x\lfloor\frac{n}{x}\rfloor+F(x,n\bmodx)&x>0\e......
  • AtCoder Regular Contest 169
    A-PleaseSign某个\(A_i\)对\(A_1\)的贡献是\(\binom{10^{100}}{\mathrm{dep}_i}\),所以深度为\(d\)的节点的\(A_i\)之和只要不为\(0\),其贡献就一定远大于深度\(<d\)的所有点的贡献之和。从大到小找到第一个和非零的深度即可。B-SubsegmentswithSmallSums直......
  • ABC332G
    题面有\(n\)种颜色的球,第\(i\)种颜色的球有\(a_i\)个,有\(m\)个盒子,第\(i\)个盒子能装\(b_i\)个球,第\(i\)个颜色的球在第\(j\)个盒子中最多装\(ij\)个,求最多能装多少个球。\(n\le500,m\le5\times10^5a_i,b_i\le10^{13}\)。题解要注意到这是个网络流模......
  • AtCoder Beginner Contest 332 (D)
    题目链接思路:这就是一个二维的全排列问题代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;#defineLNF0x3f3f3f3f3f3f3f3f#defineINF0x3f3f3f3f#defineIOSios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);#definepllp......
  • ATCoder 332 A-D
    A: OnlineShopping(读懂题即可)packageAtCoder.begin332;importjava.util.Scanner;publicclassA{publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);intn=sc.nextInt(),s=sc.nextInt(),k=sc.nextInt(......
  • 【题解】AtCoder abc322_f Random Update Query
    传送门:https://atcoder.jp/contests/abc332/tasks/abc332_f容易发现,对于一个位置$i$,$A_i$的最终值是由对$i$的最后一次赋值操作决定的;因此,将所有操作按时间顺序倒过来考虑,则由第$j$次操作决定$A_i$最终值的概率为"在第$(j+1)$~$m$次操作中没有修改过$i$的概率"与"第......
  • AtCoder Regular Contest 169 (ARC169)
    怎么有人ARCA卡了半天的?A.PleaseSign考虑\(i\)位置上的数,下次它被加到\(P_i\),再下次被加到\(P_{P_i}\),因为这个序列有性质\(P_i<i\),这样加若干轮一定会到达\(1\)。令所有的\(i\)向\(P_i\)连边,则这是一棵以\(1\)为根的树。设\(f_i=\sum\limits_{j=1}^n[dep_......
  • AtCoder_abc332
    AtCoder_abc332比赛链接A-OnlineShoppingA题链接题目大意这里有\(N\)件商品,第\(i\)件商品价格为\(P_i\),你要购买\(Q_i\)件,除了购买的费用外,他还要支付运费。如果购买的总价大于\(S\),运费为0元,否则他需要支付\(K\)元的运费。他一共要花多少钱呢?解题思路无代码//Prob......
  • 【题解】AtCoder abc332_g Not Too Many Balls
    传送门:https://atcoder.jp/contests/abc332/tasks/abc332_g看完题,第一眼反应为最大流。建模方式为:以颜色为左部点,盒子为右部点,源点$S$向颜色$i$连一条容量为$A_i$的边,盒子$j$向汇点$T$连一条容量为$B_j$的边,颜色$i$向盒子$j$连一条容量为$ij$的边;在这张图......
  • AtCoder Beginner Contest 332
    AtCoderBeginnerContest332A-OnlineShoppingintmain(){IOS;for(_=1;_;--_){cin>>n>>m>>k;llans=0;rep(i,1,n){lla,b;cin>>a>>b;ans+=a......