Educational Codeforces Round 29

Educational Codeforces Round 29

https://codeforces.com/contest/863
复健训练
太久没练直接变身傻逼(难道原来就不是吗,笑)

A. Quasi-palindrome

直接去除后缀0即可(WA了两发评价为弱智)

#include <bits/stdc++.h>

using namespace std;

int main () {
    string s;
    cin >> s;
    string t;
    int ed = s.size ();
    for (int i = s.size () - 1; i >= 0; i--) {
        if (s[i] != '0')   break;
        ed = i;
    }
    for (int i = 0; i < ed; i++)    t += s[i];
    
    //cout << t << endl;
    string tt = t;
    reverse (t.begin (), t.end ());
    if (t == tt)    cout << "YES";
    else    cout << "NO";
}

B. Kayaking

暴力模拟即可

#include <bits/stdc++.h>
#define ll long long

using namespace std;
const int N = 105;
int a[N], b[N], n;

int main () {
    cin >> n;
    n *= 2;
    for (int i = 1; i <= n; i++)    cin >> a[i];
    sort (a + 1, a + n + 1);
    ll ans = 0;
    for (int i = 2; i <= n; i += 2)    ans += a[i] - a[i-1];
    for (int i = 1; i < n; i++) {
        for (int j = i + 1; j <= n; j++) {
            ll sum = 0, id = 0;
            for (int k = 1; k <= n; k++) {
                if (k == i || k == j)   continue;
                b[++id] = a[k];
            }
            for (int k = 2; k <= id; k+=2)    sum += b[k] - b[k-1];
            ans = min (ans, sum);
        }
    }
    cout << ans << endl;
}

C. 1-2-3

暴力模拟找环,注意细节

#include <bits/stdc++.h>
#define ll long long

using namespace std;
typedef pair<ll, ll> pii;
const int N = 5;
int a[N][N], b[N][N];
ll n, x, y, ansa, ansb, dx, dy;
set<pii> s;
vector<pii> v;

void change (int p1, int p2, ll &num1, ll &num2) {
    if (p1 == p2)   return ;
    if (p1 == 1) {
        if (p2 == 2)    num2 ++;
        else    num1 ++;
    }
    else if (p1 == 2) {
        if (p2 == 3)    num2 ++;
        else    num1 ++;
    }
    else {
        if (p2 == 1)    num2 ++;
        else    num1 ++;
    }
}

int main () {
    cin >> n >> x >> y;
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            cin >> a[i][j];
        }
    }
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            cin >> b[i][j];
        }
    }
    n --;
    change (x, y, ansa, ansb);
    while (n && 1) {
        int tx = x, ty = y;
        x = a[tx][ty], y = b[tx][ty];
        if (s.count ({x, y})) {
            ll len = v.size (), id = 0;
            for (int i = 0; i < v.size (); i++) {
                if (x == v[i].first && y == v[i].second) {
                    //cout << i << endl;
                    len -= i, n -= i, id = i;
                    break;
                }
            }
            for (int i = 0; i < id; i++)    change (v[i].first, v[i].second, ansa, ansb);
            //cout << len << endl;
            //cout << n << endl;
            //cout << ansa << ' ' << ansb << endl;
            ll cnt = n / len, res = n % len;
            for (int i = id; i < v.size (); i++)    change (v[i].first, v[i].second,  dx, dy);
            ansa += dx * cnt, ansb += dy * cnt;
            for (int i = id; i < id + res; i++)   change (v[i].first, v[i].second, ansa, ansb);
            break;
        }
        s.insert ({x, y}), v.push_back ({x, y});
    }
    //for (auto i : v)    cout << i.first << ' ' << i.second << endl;
    
    cout << ansa << ' ' << ansb << endl;
}

D. Yet Another Array Queries Problem

这场唯一1A的题,泪目
正难则反,逆推思想,从小的数据范围入手

#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 5;
int a[N], n, m, q, ans[N];

struct Node {
    int type, l, r;
}p[N];

int main () {
    cin >> n >> m >> q;
    for (int i = 1; i <= n; i++)    cin >> a[i];
    for (int i = m; i >= 1; i--)    cin >> p[i].type >> p[i].l >> p[i].r;
    while (q --) {
        int x;
        cin >> x;
        // if (ans[x]) {
        //     cout << ans[x] << ' ';
        //     continue;
        // }
        //逆推
        for (int i = 1; i <= m; i++) {
            int type = p[i].type, l = p[i].l, r = p[i].r;
            if (x < l || x > r) continue; //不在范围内
            if (type == 1) {
                if (x == l)     x = r;
                else    x--;
            }
            else {
                x = l + (r - x);
            }
            //cout << x << ' ';
        }
        ans[x] = a[x];
        cout << ans[x] << ' ';
        //cout << endl;
    }
}

//m小,根据m来反推

E. Turn Off The TV

考虑两种情况:包含和边界相交,答案就出来了:

#include <bits/stdc++.h>

using namespace std;
//typedef pair<int, int> pii;
const int N = 2e5 + 5;
//pii p[N];
int n;

struct pii {
    int id, first, second;
}p[N];

bool cmp (pii p1, pii p2) {
    if (p1.first == p2.first)   return p1.second < p2.second;
    return p1.first < p2.first;
}

int Contain (pii p1, pii p2) {
    if (p1.first == p2.first && p1.second <= p2.second)  return 1;
    if (p2.second <= p1.second)  return 0;
    return -1;
}

int main () {
    cin >> n;
    for (int i = 1; i <= n; i++)    cin >> p[i].first >> p[i].second, p[i].id = i;
    sort (p + 1, p + n + 1, cmp);
    //for (int i = 1; i <= n; i++)    cout <<  p[i].first << ' ' << p[i].second << endl;
    
    //包含
    for (int i = 2; i <= n; i++) {
        int dx = Contain (p[i-1], p[i]);
        if (dx != -1) {
            cout << p[i - dx].id << endl;
            return 0;
        }
    }
    
    for (int i = 2; i < n; i++) {
        int l = p[i].first, r = p[i].second;
        // //边界相邻
        // if (r - l == 1 && p[i-1].second == l && p[i+1].first == r) {
        //     cout << p[i].id << endl;
        //     return 0;
        // }
        //边界香蕉
        if (p[i+1].first - p[i-1].second <= 1) {
            cout << p[i].id << endl;
            return 0;
        }
    }

    cout << -1;
}

//包含or相邻

F. Almost Permutation

以为是构造,结果是网络流,耗费了好时间想分类讨论,结果是算法基础不行,明天补

G. Graphic Settings

今天本来还打算写一套abc的,太磨蹭了,果然不练就会完蛋。

posted @ 2023-07-10 23:51  Sakana~  阅读(12)  评论(0编辑  收藏  举报