Educational Codeforces Round 12

Educational Codeforces Round 12

https://codeforces.com/contest/665
3/6:ABC

A. Buses Between Cities

比较烦人的一道题,看清题目模拟

#include <bits/stdc++.h>

using namespace std;
int a, ta, b, tb, h, m, ans;

int main () {
    scanf ("%d%d%d%d", &a, &ta, &b, &tb);
    scanf ("%d:%d", &h, &m);
    //printf ("%d %d %d %d %d:%d", a, ta, b, tb, h, m);
    int st = m + h * 60, cur = 5 * 60;
    while (cur < 24 * 60 && cur < st + ta) {
        if (st - cur < tb)    ans ++;
        cur += b;
    }
    cout << ans;
}

B. Shopping

模拟

#include <bits/stdc++.h>

using namespace std;
const int N = 105;
int n, m, k, ans, a[N];

int main () {
    cin >> n >> m >> k;
    for (int i = 1; i <= k; i++)    cin >> a[i];
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            int x;  cin >> x;
            for (int ii = 1; ii <= k; ii++) {
                if (a[ii] == x) {
                    ans += ii;
                    for (int jj = ii; jj > 1; jj--)     a[jj] = a[jj-1];
                    a[1] = x;
                    break;
                }
            }
        }
    }
    cout << ans << endl;
}

C. Simple Strings

贪心直接改

#include <bits/stdc++.h>

using namespace std;

int main () {
    string s;
    cin >> s;
    for (int i = 1; i < s.size (); i++) {
        if (s[i] == s[i-1]) {
            for (char ch = 'a'; ch <= 'z'; ch++) {
                if (ch != s[i]) {
                    if (i == s.size () - 1 || s[i+1] != ch) {
                        s[i] = ch;
                        break;
                    }
                }
            }
        }
    }
    cout << s;
}

D. Simple Subset

分类讨论。
四种情况取最大:

  1. 全1
  2. 全1 + 加一之后为质数的数
  3. 一奇 + 一偶 (要满足和为质数)
  4. 任意一个数
#include <bits/stdc++.h>

using namespace std;
const int N = 1005;
int a[N], n, cnt1;
int del_prime, odd, even;
int ans1, ans2, ans3, ans4, ans;
vector <int> v1, v2;

bool isPrime (int x) {
    if (x <= 2) return true;
    for (int i = 2; i * i <= x; i++) {
        if (x % i == 0)     return false;
    }
    return true;
}

int main () {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        if (a[i] & 1)   v1.push_back (a[i]);
        else    v2.push_back (a[i]);
        if (a[i] == 1)  cnt1++;
        if (!del_prime) {
            if (a[i] != 1 && isPrime (a[i] + 1)) del_prime = a[i];
        }
    }
    for (auto i : v1) {
        for (auto j : v2) {
            if (isPrime (i + j)) {
                odd = i, even = j;
                break;
            }
        }
        if (odd && even)    break;
    } 

    ans1 = cnt1; //全1
    if (del_prime)  ans2 = cnt1 + 1; //1+pr-1
    if (odd && even)    ans3 = 2; //一奇一偶
    ans4 = 1; //单独
    ans = max ({ans1, ans2, ans3, ans4});
    cout << ans << endl;
    //cout << prime << ' ' << del_prime << endl;
    //cout << cnt1 << endl;
    if (ans1 == ans || ans2 == ans) {
        while (cnt1 --) cout << "1 ";
        if (del_prime)  cout << del_prime;
    }
    else if (ans3 == ans)   cout << odd << ' ' << even;
    else    cout << a[1];
}

E. Beautiful Subarrays

01-Trie树
利用前缀异或和建立Trie树
现在在\(p\), 对于 \(k\)
当前位为 \(1\):跳到 \(!p\) 才能保证异或值为 \(1\)\(=k\)
当前位为 \(0\):跳到 \(!p\) 异或值为 \(1\)\(>k\);跳到 \(p\),异或值为0,不能确定,接着跳。
叶子节点 (异或和 \(=k\)) 单独计算贡献
一开始 \(sum=0\) 也要插入

// LUOGU_RID: 98727459
#include <bits/stdc++.h>
#define int long long

using namespace std;
const int M = 1e6 + 5, N = M * 21;
int son[N][2], cnt[N], idx, sum, n, m, ans;

void insert(int x) {
    int p = 0;
    for (int i = 30; i >= 0; i--) {
        int u = x >> i & 1;
        if (!son[p][u])    son[p][u] = ++idx;
        p = son[p][u];
        cnt[p]++;
    }
}

int query(int x) {
    int p = 0, res = 0;
    for (int i = 30; i >= 0; i--) {
        int u = x >> i & 1;
        if (m >> i & 1)     p = son[p][!u];
        else    res += cnt[son[p][!u]], p = son[p][u];
        if (!p)    break; //叶子
    }
    return cnt[p] + res; //叶子节点单独计算贡献
}

signed main () {
    cin >> n >> m;
    insert (sum);
    for (int i = 1; i <= n; i++) {
        int x;  cin >> x;
        sum ^= x;
        ans += query (sum);
        insert (sum);
    }
    cout << ans;
    //cout << ' ' << log(1e9);//21
}

F. Four Divisors

Min-25筛

posted @ 2023-01-05 14:05  Sakana~  阅读(43)  评论(0编辑  收藏  举报