2026.7.24思沁集训题解

· · 题解

2026.7.24思沁集训题解

A - 友好城市

题意

河两岸各有 N 个城市,南北岸城市一一对应。每对友好城市之间可以修建一条直线航道,要求任意两条航道不能交叉。求最多能批准修建的航道数量

思路

二分 + 贪心题目

过程

  1. 将每对城市按南岸坐标从小到大排序。若南岸坐标递增,则要保证航道不相交,对应的北岸坐标也必须严格递增。因此问题转化为:求排序后北岸坐标序列的最长上升子序列长度

    伪代码

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 2e5 + 5;
    struct P{
     int s, t;
    }c[N];
    bool cmp(P x, P y) {
     return x.s < y.s;
    }
    int a[N], dp[N], v[N];
    int n;
    int main() {
     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
     cin >> n;
     for(int i = 1;i <= n;i++){
         cin >> c[i].s >> c[i].t;
     }
     sort(c + 1, c + n + 1, cmp);
     for(int i = 1;i <= n;i++){
         a[i] = c[i].t;
     }
     memset(v, 0x3f, sizeof(v));
     int cnt = 1;
     for(int i = 1;i <= n;i++){
         二分求最长上升子序列
     }
     cout << cnt;
     return 0;
    }

    AC代码

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 2e5 + 5;
    struct P{
     int s, t;
    }c[N];
    bool cmp(P x, P y) {
     return x.s < y.s;
    }
    int a[N], dp[N], v[N];
    int n;
    int main() {
     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
     cin >> n;
     for(int i = 1;i <= n;i++){
         cin >> c[i].s >> c[i].t;
     }
     sort(c + 1, c + n + 1, cmp);
     for(int i = 1;i <= n;i++){
         a[i] = c[i].t;
     }
     memset(v, 0x3f, sizeof(v));
     int cnt = 1;
     for(int i = 1;i <= n;i++){
         int ans = lower_bound(v + 1, v + 1 + n, a[i]) - v;
         v[ans] = min(v[ans], a[i]);
         dp[i] = ans;
         cnt = max(cnt, ans);
     }
     cout << cnt;
     return 0;
    }

    C - 木棍加工

    题意

    一堆木头棍子共有 n 根,每根棍子的长度和宽度都是已知的,第一根棍子的准备时间为 1 分钟,之后若下一根棍子的长度和宽度都不超过上一根,则不需要准备时间,否则需要 1 分钟。求加工完所有木棍所需的最短准备时间

    思路

    二分求最长严格上升子序列(LIS)长度题目

    过程

  2. 将木棍按长度从大到小排序,长度相同时按宽度从大到小排序
  3. 排序后,将这些木棍可以连续加工。一旦宽度出现严格上升,准备时间 + 1

    伪代码

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 5005;
    struct P{
     int l, w;
    }a[N];
    bool cmp(P x, P y){
     if(x.l != y.l){
         return x.l > y.l;
     }
     return x.w > y.w;
    }
    int c[N];
    int v[N];
    int main(){
     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
     int n;
     cin >> n;
     for(int i = 1;i <= n;i++){
         cin >> a[i].l>> a[i].w;
     }
     sort(a + 1, a + n + 1, cmp);
     for(int i = 1;i <= n;i++){
         c[i] = a[i].w;
     }
     int res = 0;
     for(int i = 1;i <= n;i++){
         二分
     }
     cout << res;
     return 0;
    }

    AC代码

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 5005;
    struct P{
     int l, w;
    }a[N];
    bool cmp(P x, P y){
     if(x.l != y.l){
         return x.l > y.l;
     }
     return x.w > y.w;
    }
    int c[N];
    int v[N];
    int main(){
     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
     int n;
     cin >> n;
     for(int i = 1;i <= n;i++){
         cin >> a[i].l>> a[i].w;
     }
     sort(a + 1, a + n + 1, cmp);
     for(int i = 1;i <= n;i++){
         c[i] = a[i].w;
     }
     int res = 0;
     for(int i = 1;i <= n;i++){
         int x = c[i];
         int pos = lower_bound(v, v + res, x) - v;
         if(pos == res){
             v[res++] = x;
         }else{
             v[pos] = x;
         }
     }
     cout << res;
     return 0;
    }

    D - 导弹拦截

    题意

    有一个 n 个整数的序列,求最长不上升子序列长度和最长上升子序列长度

    思路

    一道很简单的求最长上升和最长不上升子序列题

    过程

  4. 第一问:直接用 贪心 + 二分 法就能解决
  5. 第二问:根据 Dilworth 定理,最少系统数等于最长上升子序列的长度,用标准 LIS 的贪心 + 二分即可

    伪代码

    #include<bits/stdc++.h>
    #define int long long
    using namespace std;
    const int N = 100005;
    int a[N], n;
    int dp1[N], dp2[N];
    signed main(){
     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
     int x;
     while(cin >> x){
         a[++n] = x;
     }
     int len1 = 0;
     for(int i = 1;i <= n;i++){
         模板
     }
     int len2 = 0;
     for(int i = 1;i <= n;i++){
         模板
     }
     cout << len1<< '\n'<< len2<< '\n';
     return 0;
    }

    AC代码

    #include<bits/stdc++.h>
    #define int long long
    using namespace std;
    const int N = 100005;
    int a[N], n;
    int dp1[N], dp2[N];
    signed main(){
     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
     int x;
     while(cin >> x){
         a[++n] = x;
     }
     int len1 = 0;
     for(int i = 1;i <= n;i++){
         int x = a[i];
         int pos = upper_bound(dp1, dp1 + len1, x, greater <int>()) - dp1;
         if(pos == len1){
             dp1[len1++] = x;
         }else{
             dp1[pos] = x;
         }
     }
     int len2 = 0;
     for(int i = 1;i <= n;i++){
         int x = a[i];
         int pos = lower_bound(dp2, dp2 + len2, x) - dp2;
         if(pos == len2){
             dp2[len2++] = x;
         }else{
             dp2[pos] = x;
         }
     }
     cout << len1<< '\n'<< len2<< '\n';
     return 0;
    }

    E - Shift vs. CapsLock

    题意

    有三种操作:

  6. 花费 X 毫秒单独按下 'a' 键。如果 Caps Lock 键指示灯关闭,屏幕字符串末尾会追加 a;如果指示灯开启,则追 A
  7. 花费 Y 毫秒同时按下 'a' 键和 Shift 键。如果 Caps Lock 键指示灯关闭,屏幕字符串末尾会追加A;如果指示灯开启,则追加 a
  8. 花费 Z 毫秒按下 Caps Lock 键。如果 Caps Lock 键指示灯关闭,则开启指示灯;如果指示灯已开启,则关闭它

给定一个由 A 和 a 组成的字符串 S,求最少需要花费多少毫秒才能使屏幕上显示的字符串等于 S

思路

这是一道 典型的 DP 问题

过程

  1. 定义一个 dp 数组,dp[i][0] 表示已经处理完前 i 个字符,且当前 CapsLock 状态为关/开时的最小耗时
  2. 转移方程:dp[i+1][st]=min(dp[i][st]+cost(st,c))
  3. 转移方程:dp[i+1][st⊕1]=min(dp[i[st]+Z+cost(st⊕1,c))
  4. 最终答案即为 min(dp[n][0], dp[n][1])

    伪代码

    #include<bits/stdc++.h>
    #define int long long
    using namespace std;
    const int N = 300005;
    const int INF = 4e18;
    int X, Y, Z;
    string S;
    int dp[N][2];
    signed main() {
     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
     cin >> X>> Y>> Z>> S;
     int n = S.size();
     dp[0][0] = 0;
     dp[0][1] = Z; 
     for(int i = 0;i < n;i++){
         dp[i + 1][0] = dp[i + 1][1] = INF;
         for(int st = 0;st <= 1;st++){
             int cost;
             if(S[i] == 'A'){
                 cost = (st == 0 ? Y : X);
             }else{
                 cost = (st == 0 ? X : Y);
             }
             转移1
             转移2
         }
     }
     cout << min(dp[n][0], dp[n][1]);
     return 0;
    }

    AC代码

    #include<bits/stdc++.h>
    #define int long long
    using namespace std;
    const int N = 300005;
    const int INF = 4e18;
    int X, Y, Z;
    string S;
    int dp[N][2];
    signed main() {
     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
     cin >> X>> Y>> Z>> S;
     int n = S.size();
     dp[0][0] = 0;
     dp[0][1] = Z; 
     for(int i = 0;i < n;i++){
         dp[i + 1][0] = dp[i + 1][1] = INF;
         for(int st = 0;st <= 1;st++){
             int cost;
             if(S[i] == 'A'){
                 cost = (st == 0 ? Y : X);
             }else{
                 cost = (st == 0 ? X : Y);
             }
             dp[i + 1][st] = min(dp[i + 1][st], dp[i][st] + cost);
             dp[i + 1][st ^ 1] = min(dp[i + 1][st ^ 1], dp[i][st] + Z + cost);
         }
     }
     cout << min(dp[n][0], dp[n][1]);
     return 0;
    }

    F - Consecutive Subsequence

    题意

    给定一个长度为 n 的整数数组,从中选择一个子序列,该子序列需构成连续整数的递增序列,且长度尽可能大。换句话说,所求序列应为形如 [x,x + 1,......,x + k - 1],k 为子序列长度

    思路

    这是一道 典型的 DP 问题

    过程

  5. 因为数字有亿点大,所以我们用 map 来存储 dp
  6. 对于每个元素 a_i,若是存在 dp[a_i - 1] 便可以将 a_i 作为结尾加入到 dp 里,转移方程 dp[x] = max(dp[x], dp[x - 1] + 1),否则就是 dp[x] = max(dp[x], 1) 自己作为一个序列
  7. 同时维护全局最大长度 len 及其对应的结束数值 val

    伪代码

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 2e5 + 5;
    int a[N];
    map <int, int> dp;
    int n;
    signed main(){
     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
     cin >> n;
     for(int i = 1;i <= n;i++){
         cin >> a[i];
     }
     int len = 0, val = 0;
     for(int i = 1;i <= n;i++){
         int x = a[i];
         if(dp.count(x - 1)){
             转移1
         }else{
             转移2
         }
         if(dp[x] > len){
             len = dp[x];
             val = x;
         }
     }
     cout << len<< '\n';
     int s = val - len + 1;
     for(int i = 1;i <= n;i++){
         if(a[i] == s){
             cout << i<< ' ';
             s++;
         }
     }
     return 0;
    }

    AC代码

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 2e5 + 5;
    int a[N];
    map <int, int> dp;
    int n;
    signed main(){
     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
     cin >> n;
     for(int i = 1;i <= n;i++){
         cin >> a[i];
     }
     int len = 0, val = 0;
     for(int i = 1;i <= n;i++){
         int x = a[i];
         if(dp.count(x - 1)){
             dp[x] = max(dp[x], dp[x - 1] + 1);
         }else{
             dp[x] = max(dp[x], 1);
         }
         if(dp[x] > len){
             len = dp[x];
             val = x;
         }
     }
     cout << len<< '\n';
     int s = val - len + 1;
     for(int i = 1;i <= n;i++){
         if(a[i] == s){
             cout << i<< ' ';
             s++;
         }
     }
     return 0;
    }