首页 > 其他分享 >Educational Codeforces Round 161 (Rated for Div. 2)

Educational Codeforces Round 161 (Rated for Div. 2)

时间:2024-01-20 11:13:29浏览次数:31  
标签:Educational Rated ind1 ind2 int sum Codeforces else --

基本情况

A犯病卡半小时。

主要就是太着急,题目没有彻底分析清楚就开始想一些错误做法。

C最优想法出来的慢。

E比较好想。

C. Closest Cities

Problem - C - Codeforces

就,显然是能走最近城市就走,不行就不走。

一开始弄了一个自作聪明的预处理,但实际上每次查询还是 \(\operatorname O(n)\) 的复杂度,T 了第二个点。

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 1);
    vector<bool> tag(n + 1);
    vector<int> ind1, ind2;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) {
        if (a[i + 1] - a[i] < a[i] - a[i - 1]) {
           tag[i] = true;
           ind1.emplace_back(i); 
        }
        else {
            tag[i] = false;
            ind2.emplace_back(i);
        }
    }
    int q;
    cin >> q;
    int x, y;
    while(q--) {
        cin >> x >> y;
        ll sum = 0;
        if (x <= y) {
            if (x == 1) {
                x++, sum++;
            }
            auto p = lower_bound(ind1.begin(), ind1.end(), x);
            for (int i = x; i < y; i++) {
                if (tag[i]) {
                    sum++;
                    p++;
                }
                else {
                    if (p == ind1.end()) {
                        sum += a[y] - a[i];
                        break;
                    }
                    int temp = *p;
                    if (temp > y) {
                        sum += a[y] - a[i];
                        break;
                    }
                    else {
                        sum += a[temp] - a[i];
                        i = temp;
                    }
                }
            }
        }
        else {
            if (x == n) {
                x--, sum++;
            }
            auto p = lower_bound(ind2.begin(), ind2.end(), x);
            if (p != ind2.begin() && *p != x) p--;
            for (int i = x; i > y; i--) {
                if (!tag[i]) {
                    sum++;
                    p--;
                }
                else {
                    if (p == ind2.begin()) {
                        sum += a[i] - a[y];
                        break;
                    }
                    if (*p < y) {
                        sum += a[i] - a[y];
                        break;
                    }
                    else {
                        sum += a[i] - a[*p];
                        i = *p;
                    }
                }
            }
        }
        cout << sum << endl;
    }
}

但这题显然可以把所有距离的答案都预处理掉,查询的时候直接类似前缀和 \(O(1)\)。

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 1);
    vector<int> ind1(n + 1), ind2(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i];
    ind1[1] = 0;
    ind1[2] = ind1[1] + 1;
    for (int i = 3; i <= n; i++) {
        if (a[i - 1] - a[i - 2] > a[i] - a[i - 1]) {
            ind1[i] = ind1[i - 1] + 1;
        }
        else {
            ind1[i] = ind1[i - 1] + a[i] - a[i - 1];
        }
    }
    ind2[n] = 0;
    ind2[n - 1] = ind2[n] + 1; 
    for (int i = n - 2; i >= 1; i--) {
        if (a[i + 2] - a[i + 1] > a[i + 1] - a[i]) {
            ind2[i] = ind2[i + 1] + 1;
        }
        else {
            ind2[i] = ind2[i + 1] + a[i + 1] - a[i];
        }
    }
    int q;
    cin >> q;
    int x, y;
    while(q--) {
        cin >> x >> y;
        if (x <= y) {
            cout << ind1[y] - ind1[x] << endl;
        }
        else {
            cout << ind2[y] - ind2[x] << endl;
        }
    }
}

这题 \(+3\),应该在第一次 T 了之后就重写做法了,但是还想着在原来那个方法上面优化,导致疯狂罚时。

标签:Educational,Rated,ind1,ind2,int,sum,Codeforces,else,--
From: https://www.cnblogs.com/kdlyh/p/17976161

相关文章

  • Codeforces Round 920 (Div. 3)
    题目链接点这里这场div3F题的算法很基础,但是我此前居然完全没接触过。(芙莉莲震惊.jpg)不过这下能够算法沙漠面积--了。CF1921ASquarevoidsolve(){intx1=1e9,y1=1e9,x2=-1e9,y2=-1e9,x,y;for(inti=1;i<=4;++i){cin>>x......
  • 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\)......