CF1916 题单讲解

· · 个人记录

纯题面版

愚人节系列:

CF656A Da Vinci Powers

找规律:

输入 输出
1 2
2 4
3 8
4 16
\cdots \cdots
12 4096
13 8092
14 16184
\dots \dots
35 33940307968

CF656F Ace It!

解一元二次方程:

\begin{cases} x + 2 + 2 + y + 0 + 3 + 3 = 21 \\ x + 2 + 2 + 3 + 6 + 3 + 5 = 22 \\ x + 2 + 3 + 2 + 7 + 2 + 6 = 23 \end{cases}

算法系列

CF613B Skills

给定序列 a,你可以给每个元素加任意 \ge 0 的值,保证加的值总量 \le m。你的收益为 a_i = A 的数量 \times c_f 与 \min(a_1 , a_2 , \dots , a_n) \times c_m 的和。求最大收益。

CF633C Spy Syndrome 2

给定字符串 s 和 m 个字符串 t_i,s 的生成方式为在 t_i 中选一些串(可重复选择某一个串),将大写变为小写,反转后拼接在一起。求选的是哪些串及顺序。

CF653C Bear and Up-Down

若一个序列 t 满足:

则这个序列是美丽的。

给定不美丽的序列 a,请问有多少种方案交换 a_i , a_j(i < j) 使得序列 a 变成美丽序列。

数学系列

CF650B Image Preview

照片有两种摆放:`w` 与 `h`。 - 如果照片是 `w` 摆放的,则需要 $b + 1$ 时间看。 - 否则需要 $1$ 时间看。 如果照片已经被看过了,就不用再看一次了。 请问 $t$ 时间内最多能看多少照片。 ## CF613A Peter and Snow Blower 给定多边形绕多边形外一点旋转,求覆盖面积。 ## CF622D Optimal Number Permutation 定义 $d_i$ 为 $i$ 在序列中出现的位置的距离。 给定 $n$,请你构造一个长度为 $2 \times n$ 的序列,使得 $\sum\limits_{i = 1}\limits^{n}(n - i) * |d_i + i - n|$ 最小。 ## CF630E A rectangle ![pic](https://espresso.codeforces.com/1fb126f489c1c5387457312ee23f680e5a55fedb.png) 给定左上角和右下角,求包含多少个六边形的中心。# 纯题面版 # 题面 $+$ 题解 ## 愚人节系列: ## CF656A Da Vinci Powers 找规律: |输入|输出| |--|--| |$1$|$2$| |$2$|$4$| |$3$|$8$| |$4$|$16$| |$\cdots$|$\cdots$| |$12$|$4096$| |$13$|$8092$| |$14$|$16184$| |$\dots$|$\dots$| |$35$|$33940307968$| ### 解题思路 输出 $2^n - 10^2 \times \lfloor 2^{n - 13} \rfloor$。 ### 参考代码 ```cpp #include<bits/stdc++.h> using namespace std; long long n; inline long long ksm(long long a , long long b) { if(b < 0) { return 0; } long long cnt = 1; while(b) { if(b % 2) { cnt = cnt * a; } b /= 2; a = a * a; } return cnt; } int main() { ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; cout << ksm(2 , n) - 100 * ksm(2 , n - 13); return 0; } ``` ## CF656F Ace It! 解一元二次方程: $$\begin{cases} x + 2 + 2 + y + 0 + 3 + 3 = 21 \\ x + 2 + 2 + 3 + 6 + 3 + 5 = 22 \\ x + 2 + 3 + 2 + 7 + 2 + 6 = 23 \end{cases}$$ ### 解题思路 解得: $$\begin{cases} x = 1 \\ y = 10 \end{cases}$$ 所以将 $\texttt{A}$ 计算成 $1$,$\texttt{1}$ 计算成 $10$。 ### 参考代码 ```cpp #include<bits/stdc++.h> using namespace std; string s; int ans = 1; int main() { ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> s; for(int i = 1 ; i <= 6 ; i++) { if(s[i] == '1') { ans += 10; } else { ans += s[i] - '0'; } } cout << ans; return 0; } ``` ## 算法系列 ## CF613B Skills 给定序列 $a$,你可以给每个元素加任意 $\ge 0$ 的值,保证加的值总量 $\le m$。你的收益为 $a_i = A$ 的数量 $\times c_f$ 与 $\min(a_1 , a_2 , \dots , a_n) \times c_m$ 的和。求最大收益。 ### 解题思路 明显**二分**。发现**排序**后具有**单调性**。枚举能填满多少个位置然后二分剩下的能填到什么高度或枚举能填到多少高度然后二分剩下的可以填满多少。明显前者更好写,因为后者需要两个二分。 二分前预处理前缀和 $s_i = s_{i - 1} + a_i$。 想计算将 $(l , r)(a_l , a_{l + 1} , \dots , a_r \le p)$ 补到 $p$ 所需的货币为 $p * (r - l + 1) - (s_r - s_{l - 1})$。 因为 $m \le 10^{15}$,所以显然需要 **long long**。 ### 参考代码 ```cpp #include<bits/stdc++.h> #pragma GCC optimize(2) using namespace std; long long n , p , cf , cm , m , qzh[100005] , nxt[100005] , pre[100005] , l , r , mid , ans = -1 , anss , anst , ansss , anssd , minr = 1000000009; struct p_ { long long a; int tag; }a[100005]; inline bool cmp(p_ l , p_ r) { return l.a < r.a; } inline bool cmp2(p_ l , p_ r) { return l.tag < r.tag; } int main() { ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> p >> cf >> cm >> m; for(int i = 1 ; i <= n ; i++) { cin >> a[i].a; a[i].tag = i; } sort(a + 1 , a + n + 1 , cmp); for(int i = 1 ; i <= n ; i++) { qzh[i] = qzh[i - 1] + a[i].a; } a[n + 1].a = p; for(int i = 0 ; i <= n ; i++) { m -= (n - i) * p - qzh[n] + qzh[i]; if(m < 0) { m += (n - i) * p - qzh[n] + qzh[i]; continue; } l = 1; r = i; anst = 1; while(l <= r) { mid = (l + r) / 2; if(a[mid].a * mid - qzh[mid] <= m) { anst = mid; l = mid + 1; } else { r = mid - 1; } } if(anst == i) { anss = min(a[anst].a + (m - (a[anst].a * anst - qzh[anst])) / anst , p) * cm + (n - i) * cf; if(anss > ans) { ans = anss; ansss = i; anssd = min(a[anst].a + (m - (a[anst].a * anst - qzh[anst])) / anst , p); } } else { anss = min(min(a[anst].a + (m - (a[anst].a * anst - qzh[anst])) / anst , p) , a[anst + 1].a) * cm + (n - i) * cf; if(anss > ans) { ans = anss; ansss = i; anssd = min(min(a[anst].a + (m - (a[anst].a * anst - qzh[anst])) / anst , p) , a[anst + 1].a); } } m += (n - i) * p - qzh[n] + qzh[i]; if(i == 0) { break; } } for(int i = 1 ; i <= n ; i++) { if(i <= ansss) { a[i].a = max(a[i].a , anssd); } else { a[i].a = p; } } sort(a + 1 , a + n + 1 , cmp2); for(int i = 1 ; i <= n ; i++) { minr = min(minr , a[i].a); } ans = minr * cm; for(int i = 1 ; i <= n ; i++) { if(a[i].a == p) { ans += cf; } } cout << ans << '\n'; for(int i = 1 ; i <= n ; i++) { cout << a[i].a << ' '; } return 0; } ``` ## CF633C Spy Syndrome 2 给定字符串 $s$ 和 $m$ 个字符串 $t_i$,$s$ 的生成方式为在 $t_i$ 中选一些串(可重复选择某一个串),将大写变为小写,反转后拼接在一起。求选的是哪些串及顺序。 ### 解题思路 **哈希**套**记搜**卡常题。 首先将原串反过来,此时就会变成每个字符串都正序的组合。 对于每个单词,计算哈希值。 扫描原串,因为 $|w_i| \le 10^3$ 所以最多扫描 $10^4 \times 10^3 = 10^7$ 个位置。若对于位置 $i$,存在位置 $j \ge i$ 满足 $i$ 到 $j$ 的哈希值等于某个单词的哈希值,建一条从 $i$ 到 $j + 1$,边权为单词编号。 从点 $1$ 搜索到点 $n$,输出任意路径即可。 注意,使用 `map` 会出现 TLE,需要使用 `unordered_map` 节省常数。不建议使用广为流传的模数。 搜索时需要记忆化,如果一个点已确定没有通路,则不再搜索。 复杂度 $O(\sum |w_i| + n \times |w_i|)

参考代码

#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const long long mod = 1145142023 , base = 127;
int n , m , vis[10005][1005];
long long tmp , ht[10005];
char t[10005];
string s[100005];
unordered_map<long long , int> mp;
bool viq[10005];
inline bool dfs(int p)
{
    if(p == n + 1)
    {
        return 1;
    }
    if(viq[p])
    {
        return 0;
    }
    viq[p] = 1;
    for(int i = 1 ; i <= 1000 ; i++)
    {
        if(vis[p][i])
        { 
            if(dfs(p + i))
            {
                cout << s[vis[p][i]] << ' ';
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = n ; i >= 1 ; i--)
    {
        cin >> t[i];
    }
    cin >> m;
    for(int i = 1 ; i <= m ; i++)
    {
        cin >> s[i];
        tmp = 0;
        for(int j = 0 ; j < s[i].size() ; j++)
        {
            if('A' <= s[i][j] && s[i][j] <= 'Z')
            {
                tmp = (tmp * base + s[i][j] - 'A' + 1) % mod;
            }
            else
            {
                tmp = (tmp * base + s[i][j] - 'a' + 1) % mod;
            }
        }
        mp[tmp] = i;
    }
    for(int i = 1 ; i <= n ; i++)
    {
        tmp = 0;
        for(int j = i ; j <= min(n , i + 1000) ; j++)
        {
            tmp = (tmp * base + t[j] - 'a' + 1) % mod;
            if(mp.find(tmp) != mp.end())
            {
                vis[i][j - i + 1] = mp[tmp];
            }
        }
    }
    dfs(1);
    return 0; 
}

CF653C Bear and Up-Down

若一个序列 t 满足:

则这个序列是美丽的。

给定不美丽的序列 a,请问有多少种方案交换 a_i , a_j(i < j) 使得序列 a 变成美丽序列。

解题思路

暴力题。

发现错误位数 > 4 时无可行解,因为一次交换最多使得 4 个位置的合法性发生改变。

现在错误位置只有 1 到 3 个,所以可以暴力枚举每种可能有效的交换(交换位置或交换位置前一位有不合法位置)。然后检查交换位置前后还是否符合即可。

参考代码

#include<bits/stdc++.h>
using namespace std;
int n , a[150005] , ans;
vector<int> w;
inline bool check(int p)
{
    if(p < 1 || p >= n)
    {
        return 0;
    }
    if(p % 2 == 1)
    {
        return a[p] >= a[p + 1];
    }
    else
    {
        return a[p] <= a[p + 1];
    }
    return 0;
}
inline int chk(int p , int q)
{
    swap(a[p] , a[q]);
    bool flag = 1;
    for(int i = 0 ; i < w.size() ; i++)
    {
        flag &= !check(w[i]);
    }
    if(check(p - 1)|| check(p) || check(p + 1) || check(q - 1) || check(q) || check(q + 1))
    {
        flag = 0;
    }
    swap(a[p] , a[q]);
    if(flag)
    {
        return 1;
    }
    return 0;
}
int main()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = 1 ; i <= n ; i++)
    {
        cin >> a[i];
    }
    for(int i = 1 ; i < n ; i++)
    {
        if(check(i))
        {
            w.push_back(i);
        }
    }
    if(w.size() > 4)
    {
        cout << '0';
        return 0;
    }
    for(int i = 1 ; i <= n ; i++)
    {
        ans += chk(i , w[0]) + chk(i , w[0] + 1);
    }
    cout << ans - chk(w[0] , w[0] + 1);
    return 0;
}

数学系列

CF650B Image Preview

照片有两种摆放:`w` 与 `h`。 - 如果照片是 `w` 摆放的,则需要 $b + 1$ 时间看。 - 否则需要 $1$ 时间看。 如果照片已经被看过了,就不用再看一次了。 请问 $t$ 时间内最多能看多少照片。 ### 解题思路 推式子题。 首先,这是一个环,所以我们可以将原数组赋值一份,$a_{i + n} = a_i$,做到**破环成链**,方便后续处理。 考虑求一段区间 $(l,r)(l \le n + 1 , r \ge n + 1)$ 所需的最小时间。 设 $a_i$ 为看一张图的时间花费,即: - 若照片为 `w`,则 $a_i = b + 1$。 - 若照片为 `h`,则 $a_i = 1$。 设 $s_i = s_{i - 1} + a_i$。 则区间 $(l , r)$ 的花费为 $s_r - s_{l - 1} + (r - l + \min(r - n - 1 , n - l + 1)) \times a$,纪委 $d$。 明显可以使用**双指针**进行统计,每次将 $r$ 向右移,若 $d > T$,则将 $l$ 向右移,知道 $d \le T$。 最大的 $r - l + 1$ 即为答案。 ### 参考代码 ```cpp #include<bits/stdc++.h> #pragma GCC optimize(2) using namespace std; int q[1000005] , n , a , b , t , l , r , cnt , ans; char c; int main() { ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> a >> b >> t; for(int i = 1 ; i <= n ; i++) { cin >> c; if(c == 'w') { q[i] = q[i + n] = b + 1; } else { q[i] = q[i + n] = 1; } if(i > 1) { cnt += q[i]; } } l = 2; r = n; while(l <= n + 1 && r <= n + n) { r++; cnt += q[r]; while(r - l + 1 > n) { cnt -= q[l]; l++; } while(cnt + (r - l + min(r - n - 1 , n - l + 1)) * a > t) { cnt -= q[l]; l++; } ans = max(ans , r - l + 1); } cout << ans; return 0; } ``` ## CF613A Peter and Snow Blower 给定多边形绕多边形外一点旋转,求覆盖面积。 ### 解题思路 $$ ans = \pi (\max(dis)^2 - \min(dis)^2)$$ 最远点一定在一个点上。最近点在一个线段的垂线上或端点上。做垂线判断即可。 ### 参考代码 ```cpp #include<bits/stdc++.h> #pragma GCC optimize(2) using namespace std; int n; long double x , y , a[1000005] , b[1000005] , mind = 1e18 , maxd; const long double pi = 3.14159265359; inline long double mindis(int p , int q) { long double d1 = (a[q] - a[p]) * (x - a[p]) + (b[q] - b[p]) * (y - b[p]); if(d1 <= 0) { return sqrtl((x - a[p]) * (x - a[p]) + (y - b[p]) * (y - b[p])); } d1 = sqrtl(d1); long double d2 = sqrtl((a[p] - a[q]) * (a[p] - a[q]) + (b[p] - b[q]) * (b[p] - b[q])); if(d1 >= d2) { return sqrtl((x - a[q]) * (x - a[q]) + (y - b[q]) * (y - b[q])); } long double hx = a[p] + (a[q] - a[p]) * d1 * d1 / d2 / d2 , hy = b[p] + (b[q] - b[p]) * d1 * d1 / d2 / d2; return sqrtl((hx - x) * (hx - x) + (hy - y) * (hy - y)); } int main() { ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> x >> y; for(int i = 1 ; i <= n ; i++) { cin >> a[i] >> b[i]; maxd = max(maxd , sqrtl((a[i] - x) * (a[i] - x) + (b[i] - y) * (b[i] - y))); } mind = mindis(1 , n); for(int i = 1 ; i < n ; i++) { mind = min(mind , mindis(i , i + 1)); } printf("%.8Lf" , pi * maxd * maxd - pi * mind * mind); return 0; } ``` ## CF622D Optimal Number Permutation 定义 $d_i$ 为 $i$ 在序列中出现的位置的距离。 给定 $n$,请你构造一个长度为 $2 \times n$ 的序列,使得 $\sum\limits_{i = 1}\limits^{n}(n - i) * |d_i + i - n|$ 最小。 ### 解题思路 最小值一定是 $0$。 此时 $1$ 之间距离是 $n - 1$,$2$ 之间距离是 $n - 2$,$3$ 之间距离是 $n - 3 \cdots \ \! \cdots$。$n$ 之间的距离任意。 所以构造两个序列并拼接:$1 , 3 , 5 , \dots , 5 , 3 , 1 , 2 , 4 , 6 , \dots , 6 , 4 , 2$。将 $n$ 放在两个未被使用的位置即可。 ### 参考代码 ```cpp #include<bits/stdc++.h> #pragma GCC optimize(2) using namespace std; int n , a[1000005]; int main() { ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i = 1 ; i + i <= n ; i++) { a[i] = a[n - i + 1] = i * 2 - 1; a[i + n] = a[n + n - i] = i * 2; } for(int i = 1 ; i <= n + n ; i++) { if(!a[i]) { a[i] = n; } } for(int i = 1 ; i <= n + n ; i++) { cout << a[i] << ' '; } return 0; } ``` ## CF630E A rectangle ![pic](https://espresso.codeforces.com/1fb126f489c1c5387457312ee23f680e5a55fedb.png) 给定左上角和右下角,求包含多少个六边形的中心。 ### 解题思路 观察可得,$(x + y) \bmod 2 = 0$ 的位置才有中心,所以直接计算即可。 若按照奇偶分成两类,则共有 $\lfloor\frac{x_2 + 1}{2}\rfloor - \lfloor \frac{x_1 + 1}{2}\rfloor + 1$ 行,每行有 $\lfloor\frac{y_2 + 1}{2}\rfloor - \lfloor \frac{y_1 + 1}{2}\rfloor + 1$ 个和 $\lfloor\frac{x_2 - 1}{2}\rfloor - \lfloor \frac{x_1 + 1}{2}\rfloor + 1$ 行,每行有 $\lfloor\frac{y_2 - 1}{2}\rfloor - \lfloor \frac{y_1 + 1}{2}\rfloor + 1$ 个。 将两式相加即为答案。 注意 C++ 是向 $0$ 取整,所以不能直接转换 **int**。可行的解决方案:全体偏移 $10^9$,此时答案不受影响。 ### 参考代码 ```cpp #include<iostream> #pragma GCC optimize(2) using namespace std; long long x1 , x2 , y1 , y2 , cc , cd , rc , rd , ans; int main() { ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> x1 >> y1 >> x2 >> y2; x1 += 1000000000; x2 += 1000000000; y1 += 1000000000; y2 += 1000000000; cc = (x2 + 1) / 2 - (x1 + 1) / 2 + 1; cd = (x2 - 1) / 2 - (x1 + 1) / 2 + 1; rc = (y2 + 1) / 2 - (y1 + 1) / 2 + 1; rd = (y2 - 1) / 2 - (y1 + 1) / 2 + 1; cout << cc * rc + cd * rd; return 0; } ```