Bootstrap

Atcoder Beginner Contest353 A~E题解

A - Buildings

题目:
给一个数组H,从左往右找比第一个元素大的元素下标

思路:
模拟

static void solve() throws IOException {
    int n = pInt();
    int[] a = pIntArray(1);
    int t = a[1];
    for (int i = 2; i < a.length; i ++) {
        if (a[i] > t) {
            out.println(i);
            return;
        }
    }
    out.println(-1);
}

B - AtCoder Amusement Park

题目:
有一个最多容纳K个人的演出,有N组人在排队,第i组人有Ai 个人,要求每次放一组人进去且总人数不能超过K个人,当无法继续放入时开始演出,接着继续放人进入,直到没人排队为止,求开始演出的次数

思路:
模拟

static void solve() throws IOException {
    int[] ins = pIntArray(0);
    int n = ins[0], k = ins[1];
    int[] a = pIntArray(0);
    int ans = 0, cur = 0;
    for (int i = 0 ; i < n; i ++) {
        if (cur + a[i] > k) {
            cur = 0;
            ans ++;
        }
        cur += a[i];
    }
    if (cur > 0) ans ++;
    out.println(ans);
}

C - Sigma Problem

题目:
定义 f ( x , y ) = ( x + y ) m o d    1 0 8 f(x,y) = (x+y) \mod 10^8 f(x,y)=(x+y)mod108 ,给一个正整数序列A,求 ∑ i = 1 N − 1 ∑ j = i + 1 N f ( A i , A j ) \sum^{N-1}_{i=1} \sum^{N}_{j=i+1}f(A_i, A_j) i=1N1j=i+1Nf(Ai,Aj)

思路:
观察发现A的顺序任意交换不会影响最后的结果,而f相当于x+y大于等于1e8的时候就会减去1e8,题目的数据Ai<1e8就意味着x+y<2e8,最多减去一次1e8;
先进行排序,对于每个元素x,通过二分找到使得x+y>=1e8的第一个y,此时y右边的所有数加上x都会大于等于1e8,那么就需要减去这些元素的个数*1e8

static void solve() throws IOException {
    final long mod = (long) 1e8;
    int n = pInt();
    int[] a = pIntArray(0);
    long ans = 0;
    Arrays.sort(a);
    for (int i = 0; i < n; i ++) {
        ans += (long) a[i] * (n - 1);
        int l = i + 1, r = n - 1;
        while (l < r) {
            int mid = l + r >> 1;
            if (a[i] + a[mid] >= mod) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        if (l < n && a[i] + a[l] >= mod) {
            ans -= (n - l) * mod;
        }
    }
    out.println(ans);
}

D - Another Sigma Problem

题目:
给一个整数序列A,定义 f ( x , y ) f(x,y) f(x,y) 为将x和y前后拼接成的整数,求 ∑ i = 1 N − 1 ∑ j = i + 1 N f ( A i , A j ) \sum^{N-1}_{i=1} \sum^{N}_{j=i+1}f(A_i, A_j) i=1N1j=i+1Nf(Ai,Aj),结果对998244353取模

思路:
仔细观察其实可以发现,每个数x作为前面一部分的时候,相当于x*10^(y的位数)+y;
如何统计10^(y的位数)这一部分呢,二重循环是会超时的;但是可以从最后一个开始统计;

static void solve() throws IOException {
    final long mod = 998244353;
    int n = pInt();
    long[] a = pLongArray(0);
    long ans = 0;
    long count = 0, sum = 0;
    for (int i = n - 1; i >= 0; i --) {
        ans = (ans + a[i] * count % mod + sum % mod) % mod;
        long t = a[i], cnt = 0;
        while (t > 0) {
            cnt ++;
            t /= 10;
        }
        sum += a[i];
        count += (long) Math.pow(10, cnt);
    }
    out.println(ans % mod);
}

E - Yet Another Sigma Problem

题目:
定义 f ( x , y ) f(x,y) f(x,y)为两个字符串x和y的最长公共前缀的长度 ,给一个字符串数组S,求 ∑ i = 1 N − 1 ∑ j = i + 1 N f ( S i , S j ) \sum^{N-1}_{i=1} \sum^{N}_{j=i+1}f(S_i, S_j) i=1N1j=i+1Nf(Si,Sj)

思路:
用字典树实现记录前缀,也就是每个点都多少个字符串经过,最后统计这些点构成的公共前缀对答案的贡献度即可;
比如adabcabda点经过3次,b点经过2次,分别的贡献度为3和1

static Map<Integer, Integer> map = new HashMap<>();
static class Trie {
    int[][] p = new int[26][(int) (3e5 + 5)];
    int idx = 0;
    {
        for (int i = 0; i < 26; i ++) {
            Arrays.fill(p[i], -1);
        }
    }
    void insert(String s) {
        int n = s.length();
        int cur = 0;
        for (int i = 0; i < n; i ++) {
            int t = s.charAt(i) - 'a';
            if (p[t][cur] == -1) {
                p[t][cur] = ++ idx;
            }
            cur = p[t][cur];
            map.compute(cur, (k, v) -> v == null ? 1 : v + 1);
        }
    }
}
static void solve() throws IOException {
    int n = pInt();
    String[] ins = pStringArray();
    Trie root = new Trie();
    long ans = 0;
    for (int i = 0; i < n; i ++) {
        root.insert(ins[i]);
    }
    for (int v : map.values()) {
        ans += (long) v * (v - 1) / 2;
    }
    out.println(ans);
}

;