首页 > 其他分享 >Educational Codeforces Round 5 A-E

Educational Codeforces Round 5 A-E

时间:2023-09-01 19:22:24浏览次数:43  
标签:Educational cout int bmod Codeforces id sum tid Round

Educational Codeforces Round 5

image-20230901173739735

垫底咯,中间老师找我去聊了下新学期的机房借用和训练,但出去前就只有我没出E

目录

A Comparing Two Long Integers

模拟字符串比较

string del(string s)
{
    int n = s.size();
    for(int i = 0; i < n; i++)
    {
        if(s[i] != '0')
        {
            return s.substr(i, n - i);
        }
    }
    return "0";
}

void solve()
{       
    string s, t;   cin>>s>>t;
    s = del(s);
    t = del(t);
    // cout<<s<<" "<<t<<'\n';
    if(s.size() != t.size())
    {
        if(s.size() > t.size())
            cout<<">";
        else if(s.size() < t.size())
            cout<<"<";
        return;
    }
    else
    {
        int n = s.size();
        for(int i = 0; i < n; i++)
            if(s[i] < t[i])
            {
                cout<<"<";
                return;
            }
            else if(s[i] > t[i])
            {
                cout<<">";
                return;
            }
        cout<<"=";return;
    }
    return;
}

B Dinner with Emma

最大化行上最小列

const int N = 1e2 + 10;
 
int n, m, a[N][N];
 
void solve()
{       
    cin>>n>>m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            cin>>a[i][j];
    int res = 0;
    for(int i = 1; i <= n; i++)
    {
        int mav = 1e9 + 10;
        for(int j = 1; j <= m; j++)
            mav = min(a[i][j], mav);
        res = max(mav, res);
    }
    cout<<res<<'\n';
    return;
}

C The Labyrinth

连通块

const int N = 1e3 + 10;

int n, m, cnt;
char g[N][N];
int id[N][N];
int f[N * N];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};

void bfs(int t1, int t2, int tid)
{
    int sum = 1;
    queue<pair<int, int>> q;    q.push({t1, t2});
    id[t1][t2] = tid;
    while(!q.empty())
    {
        auto t = q.front(); q.pop();
        int tx = t.first;
        int ty = t.second;
        // cout<<tx<<" "<<ty<<'\n';
        for(int i = 0; i < 4; i++)
        {
            int x = tx + dx[i];
            int y = ty + dy[i];
            if(x < 1 || x > n || y < 1 || y > m || g[x][y] != '.' || id[x][y] != 0)
                continue;
            id[x][y] = tid;
            sum++;
            q.push({x, y});
        }
    }
    f[tid] = sum;
    // cout<<tid<<"  "<<sum<<'\n';

}

void solve()
{       
    cin>>n>>m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            cin>>g[i][j];
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            if(g[i][j] == '.' && id[i][j] == 0)
                bfs(i, j, ++cnt);
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            if(g[i][j] == '.') 
            {
                cout<<g[i][j];
                continue;
            }
            vector<int> tid;
            for(int k = 0; k < 4; k++)
            {
                int x = i + dx[k];
                int y = j + dy[k];
                tid.push_back({id[x][y]});
            }
            sort(tid.begin(), tid.end());
            tid.erase(unique(tid.begin(), tid.end()), tid.end());
            int s = 1;
            for(auto w : tid)
                s = s + f[w];
            cout<<s % 10;
        }
        cout<<'\n';
    }
    return;
}

D Longest k-Good Segment

求出最长满足条件的一段区间,满足的\(\texttt{不同数字个数} \leq k\)

二分其区间长度 \(len\),二分细节就是在数组上从左到右依次判断大小为 \(len\) 的窗口,用个数组和变量记录每个数字出现的次数和不同数字的个数。

const int N = 1e6 + 10;

int n, k, a[N], tl, tr;
int mp[N];
bool check(int len)
{
    for(int i = 0; i <= N - 10; i++)
        mp[i] = 0;
    tl = tr = -1;
    int sz = 0;
    for(int i = 1; i <= len; i++)
    {
        if(mp[a[i]] == 0)   sz++;
        mp[a[i]]++;
    }

    if(sz <= k) 
    {
        tl = 1, tr = len;
        return true;
    }

    for(int i = len + 1; i <= n; i++)
    {
        mp[a[i - len]]--;
        if(mp[a[i - len]] == 0) sz--;
        if(mp[a[i]] == 0) sz++;
        mp[a[i]]++;
        if(sz <= k)
        {
            tl = i - len + 1, tr = i;
            return true;
        }

    }
    return false;
}
void solve()
{       
    cin>>n>>k;
    for(int i = 1; i <= n; i++)
    {
        cin>>a[i];
    }
    int l = 1, r = n;
    while(l < r)
    {
        int mid = (l + r + 1) >> 1;
        if(check(mid))  l = mid;
        else r = mid - 1;
    }
    if(check(l))
        cout<<tl<<" "<<tr<<'\n';
    return;
}

E - Sum of Remainders

余数求和是同一道题

求\(n \bmod 1 + n \bmod 2 + n \bmod 3 + \dots + n \bmod m\)

\[n \bmod 1 + n \bmod 2 + n \bmod 3 + \dots + n \bmod m = \sum_{i = 1}^{m} n \bmod i \\ = \sum_{i = 1}^{m}(n - ⌊\dfrac{n}{i}⌋ \times i) \\ = n \times m - \sum_{i = 1}^{m}⌊\dfrac{n}{i}⌋ \times i \]

\(⌊\dfrac{n}{i}⌋\) 最多只有 \(\sqrt{n}\) 种,接下来上数论分块

typedef long long ll;

const int mod = 1e9 + 7;

ll n, k;
void solve()
{
    cin>>n>>k;
    swap(n, k);
    __int128 res = (__int128)n * k;
    for(__int128 l = 1; l <= n; l++)
    {
        __int128 d = k / l, r;
        if(d == 0)
            r = n;
        else    
            r = min((__int128)n, (__int128)k / d);
        __int128 m = r - l + 1;
        res -= d * (l + r) * m / 2;
        l = r;
    }
    ll t =  res % mod;
    cout<<t<<endl;
}

标签:Educational,cout,int,bmod,Codeforces,id,sum,tid,Round
From: https://www.cnblogs.com/magicat/p/17672708.html

相关文章

  • Pinely Round 2 (Div. 1 + Div. 2)
    PinelyRound2(Div.1+Div.2)比赛链接因为第二天早上满课,所以这个比赛没有打,但是补题还是要有的。A题ChannelA题链接你是一个博主,有n个订阅者,当你发表了一篇博客时,会被订阅者看到,一开始有a名订阅者在线,随后给你q个指令,“+”代表有一个订阅者上线,“-”代表有一个订阅者下......
  • Educational Codeforces Round 148 (Rated for Div. 2)E. Combinatorics Problem(组合
    题目链接:https://codeforces.com/contest/1832/problem/E 题意:  当然这是化简后的题意,原题面和这个差距还是有点大的; 分析: 因为组合数有公式:  所以:   嗯,然后就没有了; 时间复杂度:O(n*k); 代码: #include<bits/stdc++.h>#defineintlonglong......
  • P9437 『XYGOI round1』一棵树 题解
    首先这是一道很明显的换根dp。首先注意到要拼接数,所以我们可以先处理出\(num_i=10^{x}\),使得\(10^x>a_i>10^{x-1}\),这样方便我们后面算贡献。我们以这棵树为例子来推状态转移方程。先假设\(dp_u\)表示以\(1\)为根时,从\(u\)的子树的点到\(u\)的权值和。那么\[......
  • C-小美的01串翻转_牛客周赛 Round 9
    链接:https://ac.nowcoder.com/acm/contest/63869/C来源:牛客网题目描述小美定义一个01串的权值为:每次操作选择一位取反,使得相邻字符都不相等的最小操作次数。例如,"10001"的权值是1,因为只需要修改一次:对第三个字符取反即可。现在小美拿到了一个01......
  • Educational Codeforces Round 118
    好烦,又是只有三题,讲课的老师实在是太吵了,没法思考细节A题开始还搞麻烦了B题纯诈骗,找最小的做y即可C题直接二分判断即可D题其实没看多久我就秒了,对于当前的数x来说无非就是mex=x-1mex=xmex=x+1\(f[x]\)表示mex=x,后面没有数\(g[x]\)表示mex=x,后面有x+1,并且只可能是x+1,不可......
  • Codeforces Round 888 (Div. 3)G. Vlad and the Mountains(数据结构,图论)
    题目链接:https://codeforces.com/contest/1851/problem/G 大致题意: 给出n个点m条边的无向图,每个点有点权h【i】。从点i到点j会消耗h【j】-h【i】的能量,如果小于0,那么就是恢复对应绝对值的能量。 进行q次询问,每次询问包含起点s,终点t,能量e,能量在移动过程中不能小......
  • Codeforces Round 887 (Div. 1)C. Ina of the Mountain(数据结构,反悔贪心)
    题目链接:https://codeforces.com/problemset/problem/1852/C 题意: 给定一个长度为n的序列和正整数k; 每次可以选取任意一个区间,将区间内每个数减1; 如果出现一个数变成0,那么那个数变成k; 问至少操作多少次可以使得每个数变成k; 分析: 将每个数值抽象为对应高度的......
  • Codeforces Round 892 (Div. 2)E. Maximum Monogonosity(动态规划,数学)
    题目链接:https://codeforces.com/contest/1859/problem/E 题意: 有长度为n的a和b俩个序列,定义f【l,r】=abs(a【l】-b【r】)+abs(b【l】-a【r】); 给正整数k,求 不相交的区间且  所有  区间的长度 的 和 为k的最大值 是多少? 分析: 这里借鉴一个佬......
  • Educational Codeforces Round 151 (Rated for Div. 2)E. Boxes and Balls(数学,动态规
    题目链接:https://codeforces.com/contest/1845/problem/E 题意: 给定长度为n且只含0和1的数组,你可以进行以下操作: 交换相邻的0和1; 给正整数k,问经过k次操作后,会有多少种本质不同的结果; 分析: 如果1比0多,我们可以把他们取反(让0比1多,结果是一样的) 设计状态dp[i][j......
  • Codeforces Round 885 (Div. 2)E. Vika and Stone Skipping(数学,质因数分解)
    题目链接:https://codeforces.com/problemset/problem/1848/E 大致题意: 打水漂,某人在海岸线以f(正整数)的力量扔出石头,会在f,f+(f-1),f+(f-1)+(f-2),........,f+(f-1)+.....+2+1,的位置接触水面; 现在给出坐标x和q次询问,每次询问给出一个y值,将x=x*y;假设石头在x的位置接触水面,问有多少......