首页 > 其他分享 >Codeforces Round 920 (Div. 3)

Codeforces Round 920 (Div. 3)

时间:2024-01-20 10:36:08浏览次数:36  
标签:++ Codeforces else y1 int 920 -- Div y2

题目链接点这里

这场 div3 F 题的算法很基础,但是我此前居然完全没接触过。(芙莉莲震惊.jpg)
FrierenShock
不过这下能够算法沙漠面积--了。

CF1921A Square

void solve()
{
    int x1 = 1e9, y1 = 1e9, x2 = -1e9, y2 = -1e9, x, y;
    for (int i = 1; i <= 4; ++i)
    {
        cin >> x >> y;
        x1 = min(x, x1);
        y1 = min(y, y1);
        x2 = max(x, x2);
        y2 = max(y, y2);
    }
    cout << (x2 - x1) * (y2 - y1) << endl;
}

CF1921B Arranging Cats

void solve()
{
    int n;
    cin >> n;
    string s1, s2;
    cin >> s1 >> s2;
    int diff = 0, ans = 0;
    for (int i = 0; i < n; ++i)
    {
        if (s1[i] == '0' && s2[i] == '1')
        {
            if (diff < 0)
            {
                ++ans;
                ++diff;
            }
            else
            {
                ++diff;
            }
        }
        else if (s1[i] == '1' && s2[i] == '0')
        {
            if (diff > 0)
            {
                ++ans;
                --diff;
            }
            else
            {
                --diff;
            }
        }
    }
    cout << ans + abs(diff) << endl;
}

CF1921C Sending Messages

每次发信息前都贪心的进行抉择,选最优的方案。若最优的方案都无法满足则 NO,否则 YES。注意数数数清楚。

void solve()
{
    ll n, f, a, b, m;
    cin >> n >> f >> a >> b;
    ll last = 0, sumi = 0;
    for (int i = 1; i <= n; ++i)
    {
        cin >> m;
        if (i == 1)
        {
            if ((m - last) * a >= b)
                sumi += b;
            else
                sumi += (m - last) * a;
            last = m;
            continue;
        }
        if ((m - last) * a >= b)
            sumi += b;
        else
            sumi += (m - last) * a;
        last = m;
    }
    if (sumi < f)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}

CF1921D Very Different Array

容易想到将数组排序并翻转,从而尽量将大数和小数匹配。接着贪心的从 brr 的两端选出绝对值之差更大的数,直到已选了出 n 个数。

void solve()
{
    ll n, m, ans = 0;
    cin >> n >> m;
    vector<ll> arr(n), brr(m);
    for (int i = 0; i < n; ++i)
        cin >> arr[i];
    for (int i = 0; i < m; ++i)
        cin >> brr[i];
    sort(all(arr));
    sort(all(brr));
    reverse(all(brr));
    int l = 0, r = m - 1, cl = 0, cr = n - 1, cnt = 0;
    while (cl <= cr && cnt <= n)
    {
        if (abs(brr[l] - arr[cl]) >= abs(brr[r] - arr[cr]))
        {
            ans += abs(brr[l] - arr[cl]);
            ++l, ++cl;
        }
        else
        {
            ans += abs(brr[r] - arr[cr]);
            --r, --cr;
        }
    }
    cout << ans << endl;
}

CF1921E Eat the Chip

x1 >= x2 的时候不会发生相遇,是平局。然后考虑 x1 < x2。当 step 为偶数时,只会发生 Alice 赢或平局的情况。因为数据范围不大,这时可以暴力模拟每一步,并令 Bob 逃离 Alice,Alice 追赶 Bob。最后看是否追上即可判断结果。step 为奇数的讨论同上。

void solve()
{
    ll h, w, x1, y1, x2, y2;
    cin >> h >> w >> x1 >> y1 >> x2 >> y2;
    ll step = abs(x2 - x1 - 1);
    if (x1 >= x2)
    {
        cout << "Draw" << endl;
    }
    else if (step % 2 == 0)
    {
        for (int i = 1; i <= step; ++i)
        {
            if (i % 2 == 1)
            {
                if (y2 > y1)
                    ++y1;
                else if (y2 < y1)
                    --y1;
            }
            else if (i % 2 == 0)
            {
                if (y2 > y1 && y2 < w)
                    ++y2;
                else if (y2 < y1 && y2 > 1)
                    --y2;
            }
        }
        if (abs(y2 - y1) <= 1)
            cout << "Alice" << endl;
        else
            cout << "Draw" << endl;
    }
    else
    {
        for (int i = 1; i <= step; ++i)
        {
            if (i % 2 == 1)
            {
                if (y2 > y1 && y1 > 1)
                    --y1;
                else if (y2 < y1 && y1 < w)
                    ++y1;
            }
            else if (i % 2 == 0)
            {
                if (y2 > y1)
                    --y2;
                else if (y2 < y1)
                    ++y2;
            }
        }
        if (abs(y2 - y1) <= 1)
            cout << "Bob" << endl;
        else
            cout << "Draw" << endl;
    }
}

CF1921F Sum of Progression

这题用到下面两个算法。

  1. 带权前缀和

    sum(l-1) = a1 + 2*a2 + 3*a3 + ... + (l-1)*a(l-1)
    sum(r) = a1 + 2*a2 + 3*a3 + ... + l*al + ... + r*ar
    sum(r) - sum(l-1) = l*al + ... + r*ar = (l - 1)*(al + ... + ar) + al + ... + ar
    
  2. 根号分治

    找到两种算法,复杂度分别为 O(D) 与 O(n/D) (或者类似的复杂度)。每次根据数据大小选取具体使用的算法。取分界点 D=sqrt(n),可将复杂度降至 O(q*sqrt(n))。

这题的带权前缀和按公差分为 lim 组,分别进行预处理。分界点可取 200,大于两百的数据暴力求解,小于两百的数据用带权前缀和直接计算答案。

int n, q, lim = 200;
vector<vector<ll>> pre(lim, vector<ll>(1e5 + lim * 2 + 1));
vector<vector<ll>> sumi(lim, vector<ll>(1e5 + lim * 2 + 1));
 
void solve()
{
    cin >> n >> q;
    vector<ll> arr(n + 1);
    
    for (int i = 0; i < n; ++i)
        cin >> arr[i];
    for (int d = 1; d < lim; ++d)
        for (int i = 0; i < n; ++i)
        {
            pre[d][i + d] = pre[d][i] + arr[i];
            sumi[d][i + d] = sumi[d][i] + arr[i] * (i / d + 1);
        }
    while (q--)
    {
        ll s, d, k;
        cin >> s >> d >> k;
        --s;
        if (d < lim)
        {
            int r = s + d * k, l = s;
            cout << sumi[d][r] - sumi[d][l] - (l / d) * (pre[d][r] - pre[d][l]) << " ";
        }
        else
        {
            ll ans = 0;
            for (int i = 0; i < k; ++i)
                ans += arr[i * d + s] * (i + 1);
            cout << ans << " ";
        }
    }
}

标签:++,Codeforces,else,y1,int,920,--,Div,y2
From: https://www.cnblogs.com/tllwtg/p/17976110/2024-01-19-CF_920

相关文章

  • 【LGR-172-Div.4】洛谷入门赛 #19 题解
    比赛链接:\(link\)T1分饼干I题目描述洛谷网校举行了期末考试,同学们经过课程的学习,考出了优异的成绩。Z在考试中获得了第一名,yz在考试中获得了第二名,老师决定买一些饼干奖励两名小朋友。老师买了三盒饼干,第一盒有\(a\)块饼干,第二盒有\(b\)块饼干,第三盒有\(c\)块饼干......
  • Educational Codeforces Round 161 (Rated for Div. 2)
    A.TrickyTemplate思维有点难转过来,而且当时在C也能匹配c这卡了很久#include<bits/stdc++.h>usingnamespacestd;voidsolve(){ intn; cin>>n; stringa,b,c; cin>>a>>b>>c; intcnt=0; intf=0; for(inti=0;i<n;i++){ if(a[i]!=c[i]&&a......
  • Codeforces Round 919 (Div. 2)
    目录写在前面ABCDE写在最后写在前面比赛地址:https://codeforces.com/contest/1920考试周突然发癫想打cf但是觉得肯定打不完所以拿小号打的。手速慢了没上大分呃呃A求出上下界来,再求被限制的数有多少个在区间内即可。///*By:Luckyblock*/#include<bits/stdc++.h>#de......
  • Codeforces Round 920 (Div. 3)
    A.Square#include<bits/stdc++.h>usingnamespacestd;voidsolve(){ inta1,b1,a2,b2,a3,b3,a4,b4; cin>>a1>>b1>>a2>>b2>>a3>>b3>>a4>>b4; ints1,s2; if(a1==a2){ s1=abs(b1-b2); s2=abs(b3-b4); }e......
  • Educational Codeforces Round 161 (Rated for Div. 2)
    题目链接:EducationalCodeforcesRound161(RatedforDiv.2) PS:A开的很快,B读个假题意,C又想歪了,导致E没时间写,最后十分钟开E思路对了但是没时间了,赛后很快过了。。。A.TrickyTemplate题意:定义模板串t与字符串s:1:如果模板串的某一项为小写字母,那么模板串与字符串的该......
  • Educational Codeforces Round 161 (Rated for Div. 2)
    目录写在前面ABCDEF写在最后写在前面比赛地址:https://codeforces.com/contest/1922D没调出来亏炸了,第一次体验赛后五分钟过题的快感。痛苦的大二上终于结束了,本学期一半的痛苦都来自于傻逼大物实验。下学期课少了好多,而且早八和晚八都少的一批,集中上一波分了就。A题面太长......
  • Codeforces 920 (div3)
    Problem-A-Codeforces没什么问题,几个ifelse语句,判断一下条件;#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;intmain(){intk;cin>>k;while(k--){intx1,y1,x2,y2,x3,y3,x4,y4;......
  • Codeforces edu 161 (div2)
    Problem-A-Codeforces思维题,判断c字符串的每一位是否都能在相对应的a字符串或者b字符串里面找到;如果都能找到的话就输出NO;否则输出YES;#include<bits/stdc++.h>usingnamespacestd;intmain(){std::ios::sync_with_stdio(false);cin.tie(0);cout.tie......
  • Educational Codeforces Round 161 (Rated for Div. 2)
    EducationalCodeforcesRound161(RatedforDiv.2)比赛链接A.TrickyTemplate思路:貌似只要想到了就可以,我是记录了一下字符串c和字符串a和b之间的满足数,如果等于n表示一定不存在,否则就是存在的Code:#include<bits/stdc++.h>usingnamespacestd;#defineintlonglon......
  • CodeForces & AtCoder rating 规则简述
    译者:rui_er,转载请注明出处。(备份自2020年11月2日的同名博客)本博客为了方便自己查阅,同时也方便大家了解,但因为我英语很菜,所以难免有翻译错的地方,还请评论区纠正。未注明资料来源的均为常识积累。1CodeForcesrating规则1.1CodeForcesrating与名字颜色换算设\(r\)......