MXOI题解记录

· · 个人记录

CF1878A How Much Does Daytona Cost?题解

题目描述

有 t 组数据。每组数据给出一个长度为 n 的序列 a 和一个数字 k。

请你判断 a 中是否存在一个非空子段使得 k 在这个子段中出现的次数严格大于其它数字的出现次数。存在输出 YES,否则输出 NO。

分析&解法

题目中说是否存在一个非空子段,那就说明这个子段长度可以为 1 (就他自己),所以我们只需要遍历数组判断他是否存在就可以了。

Code:

::::info[Code:]

#include<iostream>
using namespace std;
int t,n,k;
int a[150];
int main(){
    cin >> t;
    while(t--){
        cin >> n >> k;
        bool f = 0;
        for(int i = 1;i <= n;i++){
            cin >> a[i];
            if(a[i] == k)
                f = 1;
        }
        if(f) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
} 

::::

CF1474A Puzzle From the Future

题目描述

意思就是给你一个 b 数组,让你构造一个 a 数组,使的按题目求得的 d 数组尽可能大。

分析&解法

根据题目可知,构造的 a 数组中只会包含0,1。
我们如果要让数组 d 尽可能大,就要尽可能让数组 a 数位更多且没有前导0,还要让 d 有尽可能多的2或1且前后不重复。
所以我们可以挨个遍历前一个数和当前的数,如果当前的数加一不等于前面的数。我们就给它赋值当前的数加一,否则就等于零。 这样就可以求出 a 数组了。

Code:

::::info[Code:]

#include<iostream>
#include<string>
using namespace std;
int t,n;
char s1[100005],s2[100005];
int main(){
    cin >> t;
    while(t--){
        cin >> n;
        cin >> s1;
        s2[0] = '1';
        for(int i = 1;i < n;i++){
            int a = s1[i] - '0',b = s2[i-1] - '0',c = s1[i-1] - '0';
            if(a + 1 != b + c) s2[i] = '1';
            else s2[i] = '0';
        }
        cout << s2 << endl;
    }
    return 0;
}

::::

CF1932A Thorns and Coins

这道题看上去是DP,实则是假DP,只需要模拟就行了

题目描述

给你一个行走路线,路上有金币和障碍,你一次可以走一步或两步,问最多能获得多少金币。

分析&解法

只需要模拟即可。

因为我们可以走一格,就相当于在没有障碍的情况下每个格子都能走到,所以只要这一格为金币,我们就可以吃到。

如果这一个为障碍,那么我们就可以判断是否能越到这个障碍,如果这一格为障碍且上一格也为障碍,就一定不能跨越(因为无论走一格还是两格都无法穿过这两个障碍)然后就 break 就行了。

Code:

::::info[Code:]

#include<iostream>
using namespace std;
int t,n;
char s[55];
int ans;
int main(){
    cin >> t;
    while(t--){
        cin >> n;
        cin >> s;
        ans = 0;
        for(int i = 0;i < n;i++){
            if(s[i] == '@') ans++;
            if(s[i] == '*' && s[i - 1] == '*') break;
        }
        cout << ans << endl;
    }
    return 0;
}

::::

CF1931C Make Equal Again

题目描述

进行至多一次区间赋值操作,使数组中的所有元素都相等的最小代价。

分析&解法

既然题目要求了至多一次,那么最终整个数组的元素要么是a_1,要么就是a_n。

我们进行两次统计,一次统计以a_1为最终元素的代价;一次统计以a_n为最终元素的代价。

最后取他俩的最小值就可以了。

Code

::::info[Code:]

#include<iostream>
using namespace std;
int t,n,a[200005];
int main(){
    cin.tie(0),cout.tie(0);
    ios::sync_with_stdio(false);
    cin >> t;
    while(t--){
        cin >> n;
        for(int i = 1;i <= n;i++)
            cin >> a[i];
        int ans1 = 0,ans2 = 0;
        int i = 1,j = n;
        while(a[i] == a[1]) i++;
        while(a[j] == a[1]) j--;
        ans1 = max(0,j - i + 1);
        i = 1,j = n;
        while(a[i] == a[n]) i++;
        while(a[j] == a[n]) j--;
        ans2 = max(0,j - i + 1);
        cout << min(ans1,ans2) << endl;
    }
    return 0;
}

::::

CF1714C Minimum Varied Number

题目描述

找出数码和为 s 的最小数字,使得其中的所有数字都是不同的。

分析&解法

如果想让一个数尽可能小,那么就要让他数位越少且高位上的数字越小。

那么我们可以让他的个位最大,越往前数越小(9,8,7,6...依次递减),这样就可以保证“数位越少且高位上的数字越小”的条件了。

Code:

::::info[Code:]

#include<iostream>
using namespace std;
int t,s;
int ans[50];
int main(){
    cin >> t;
    while(t--){
        cin >> s;
        int sy = s,i = 1,j = 9;
        while(1){
            if(sy > j) ans[i] = j;
            else{
                ans[i] = sy;
                break;
            }
            sy -= j,i++,j--;
        }
        for(int k = i;k >= 1;k--) cout << ans[k];
        cout << endl;
    }
    return 0;
}

::::

CF1409B Minimum Product

题目描述

选择 a 或 b 中的一个,将其减少 1。但操作后 a 不能小于 x,b 不能小于 y。

你的任务是,通过不超过 n 次操作,使 a 和 b 的乘积尽可能小,并输出这个最小值。

分析&解法

核心:两个数相乘,差越大,他们的积越小;越接近,他们的积越大。

所以我们可以分别让 a 最小和 b 最小,再分别取出他们的乘积,取最小值就行了。

Code:

::::info[Code:]

#include<bits/stdc++.h>
using namespace std;
int t;
int main() {
    cin >> t;
    while (t--) {
        long long a, b, x, y, n;
        cin >> a >> b >> x >> y >> n;
        long long ans = 1e18;
        int i = 2;
        while (i--) {
            int da = min(n, a - x);
            int db = min(n - da, b - y);
            ans = min(ans, (a - da) * 1ll * (b - db));
            swap(a, b);
            swap(x, y);
        }
        cout << ans << endl;
    }
}

::::

CF1515A Phoenix and Gold

题目描述

有 n 个互不相同的数,请构造一个序列使不存在前缀和为 x,如果没有请输出NO.

分析&解法

(个人见解)

如何构造这个序列呢?我们可以先给他排个序,在输出过程中如果发现有前缀和为 x 时,就先输出后一个数,再输出这一个数。

个人证明: (a 为 w_i 之前的前缀和,b 为 w_i,c 为 w_{i+1}) 如果 a + b = x 因为 b \ne c 所以 a + c \ne x

Code:

::::info[Code:]

#include <bits/stdc++.h>
using namespace std;
int t,n,x;
int w[105];
int main(){
    cin >> t;
    while(t--){
        cin >> n >> x;
        for(int i = 1;i <= n;i++) cin >> w[i];
        sort(w + 1,w + n + 1);
        int sum = 0;
        bool f = 0;
        for(int i = 1;i <= n;i++)
            sum += w[i];
        if(sum == x){
            cout << "NO\n";
            continue;
        }  
        sum = 0;
        cout << "YES\n";
        for(int i = 1;i <= n;i++){
            if(sum + w[i] == x){
                printf("%d %d ",w[i+1],w[i]);
                sum += w[i+1]+w[i],i++;
            }else{
                printf("%d ",w[i]);
                sum += w[i];
            }
        }
        cout << endl;
    }
    return 0;
}

::::

CF1501B Napoleon Cake

题目描述

对一个堆进行 n 次操作,每一次操作前堆都会增加一层。 第 i 次操作为标记堆顶 a_i 层。最后输出整个堆的标记情况。

分析&解法

模拟 + 差分 即可

维护一个差分数组,每次将 cha_{i+1} - 1,cha_{i-a_i+1} + 1,即将 [i-a_i+1,i] 的区间加一。

最后输出这个差分数组,如果 cha_i > 1,就输出 1 ,否则输出 0 。

Code:

::::info[Code:]

#include <bits/stdc++.h>
using namespace std;
int t, n;
int a[200005];
int main()
{
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        int b[200005] = {0};
        for (int i = 1; i <= n; i++)
        {
            if (a[i] == 0)
                continue;
            b[i + 1] -= 1;
            b[i - a[i] + 1 <= 0 ? 1 : i - a[i] + 1] += 1;
        }
        for (int i = 1; i <= n; i++)
            b[i] = b[i - 1] + b[i];
        for (int i = 1; i <= n; i++)
        {
            if (b[i] >= 1)
                cout << 1 << ' ';
            else
                cout << 0 << ' ';
        }
        cout << endl;
    }
    return 0;
}

::::

CF1554A Cherry

题目描述

给定 n 个整数 a_1, a_2, \ldots, a_n。请你在所有满足 1 \le l < r \le n 的整数对 (l, r) 中,找到 max(a_l, a_{l+1}, \ldots, a_r) \cdot min(a_l, a_{l+1}, \ldots, a_r) 的最大值。

分析&解法

仔细分析就会发现,

假设我们有两个相邻的数 a,b\,(a\lt b),那么这个序列的解是 a\cdot b。现在我们向 b 后面添加一个数 c,对于这个序列的解改变有且仅有两种情况:

对于第一种情况,显然 b\cdot c\gt a\cdot c,所以我们将 a 移出序列可以得到更优解;对于第二种情况,显然解与 a 无关。

综上,我们只需要考虑序列长度为 2 的情况,对此我们只需要对所有数依次判断更新答案即可。

Code:

::::info[Code:]

#include <iostream>
using namespace std;
using ll = long long;
int t;
int n, a[1000006];
int main()
{
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; ++i)
            cin >> a[i];
        ll ans = -1e18;
        for (int i = 1; i < n; ++i)
            ans = max(ans, 1ll * a[i] * a[i + 1]);
        cout << ans << '\n';
    }
}

::::

P3029 [USACO11NOV] Cow Lineup S

难度: 普及/提高−

分析&解法

先按照坐标从小到大排序,接着维护一个包含所有品种奶牛的单调队列,每次求一下成本取最小值就行了。 具体维护方法如下:

  1. 开始前先求出来一个包含所有品种奶牛的队列
  2. 每次把队尾多余的奶牛删掉
  3. 求成本
  4. 加入新奶牛

    Code:

    ::::info[Code:]

    #include <algorithm>
    #include <iostream>
    #include <map>
    using namespace std;
    int n;
    struct node{
     int x, id;
    } a[500005];
    bool cmp(node x1, node x2){
     return x1.x < x2.x;
    }
    map<int, int> mp;
    int cnt = 0, ans = 2e9 + 10;
    int main(){
     cin >> n;
     for (int i = 1; i <= n; i++){
         cin >> a[i].x >> a[i].id;
         mp[a[i].id]++;
         if (mp[a[i].id] == 1)
             cnt++;
     }
     sort(a + 1, a + n + 1, cmp);
     mp.clear();
     int dft = 0, l = 1, r;
     for (r = 1; r <= n; ++r){
         mp[a[r].id]++;
         if (mp[a[r].id] == 1)
             dft++;
         if (dft == cnt)
             break;
     }
     ans = min(ans, a[r].x - a[l].x);
     while (r <= n){
         while (mp[a[l].id] - 1 >= 1)
             mp[a[l].id]--, l++;
         ans = min(ans, a[r].x - a[l].x);
         mp[a[++r].id]++;
     }
     cout << ans;
     return 0;
    }

    ::::

    AT_abc141_d [ABC141D] Powerful Discount Tickets

    难度: 普及−(中位)

    分析&解法

    首先我们要知道,对一个价格为 X 的商品使用一张优惠券后的价格是 \left\lfloor \frac{X}{2} \right\rfloor元。

所以我们的贪心策略就是每次对最大的数使用一张优惠卷,使其价格折半。最后把剩下的价格加起来就行了。

因为每次要取最大的数,所以用一个堆(优先队列)来维护。

Code:

::::info[Code:]

#include<iostream>
#include<queue>
using namespace std;
int m;
int n;
int s[200005];
long long ans;
int main(){
    cin >> n;
    cin >> m;
    priority_queue<int> q;
    for(int i = 1;i <= n;i++){
        cin >> s[i];
        q.push(s[i]);
    }
    for(int i = 1;i <= m;i++){
        int now = q.top();
        q.pop();
        q.push(now / 2);
    }
    for(int i = 1;i <= n;i++){
        ans += q.top();
        q.pop();
    }
    cout << ans;
    return 0;
}

::::

CF1800C2 Powering the Hero (hard version)

难度: 普及−(中位)

分析&解法

可以发现,每个英雄牌所有的力量值都为其前未被使用的最大奖励牌的力量值。

所以我们可以维护一个大根堆(优先队列),存放奖励牌,这样每次就可以取到奖励牌的最大值了。

Code:

::::info[Code:]

#include<bits/stdc++.h>
using namespace std;
long long T;
void solve() {
    long long ans=0;
    priority_queue<long long> s;
    long long n;
    cin >> n ;
    for(long long i = 1; i <= n; i++) {
        long long x;
        cin>>x;
        if(x == 0 && s.size() > 0) {
            ans += s.top();
            s.pop();
        } else {
            s.push(x);
        }
    }
    cout<<ans<<'\n';
}
int main() {
    cin>>T;
    while(T--)
        solve();
    return 0;
}

::::

CF1353D Constructing the Array

难度: 普及−(上位)

分析&解法

因为每次都会取最长的区间,所以我们可以用一个大根堆(优先队列)维护一个最大的区间。

其中大根堆里要存储区间 [l,r] 的左右端点,要用到结构体。所以我们又要手写一个优先队列的比较函数(或代码中的cmp结构体)。

重点知识

结构体的优先队列: 实现按 l 降序排列,当 l 相同时按 r 升序排列。

  1. 结构体内部重构,例如
    struct node {
     int l, r;
     bool operator < (const node& x) const{
         if(l == x.l) return r > x.r;
         return l < x.l;
     }//这里的‘>’‘<’是优先级
    };
    priority_queue<node> q;
  2. 结构体外比较函数,例如
    struct cmp{
     bool operator () (const node& x,const node& y){
         if(x.l == y.l) return x.r > y.r;
         return x.l < y.l; 
     }//这里的‘>’‘<’是优先级
    };
    priority_queue<node,vector<node>,cmp> q;

    注意两者的使用方式!!!

    Code:

    ::::info[Code:]

    #include <queue>
    #include <iostream>
    using namespace std;
    int t, n;
    struct node {
     int l, r;
    };
    struct cmp{
     bool operator () (const node& x,const node& y){
         if(x.r - x.l + 1 == y.r - y.l + 1) return x.l > y.l;
         return x.r - x.l + 1 < y.r - y.l + 1;
     }
    };
    int main() {
     cin >> t;
     while (t--) {
         cin >> n;
         priority_queue<node,vector<node>,cmp> q;
         q.push({1, n});
         int a[200005] = {}, i = 1;
         while (!q.empty()) {
             node u = q.top();
             q.pop();
             int mid;
             if ((u.r - u.l + 1) % 2 == 0)
                 mid = (u.l + u.r - 1) / 2;
             else
                 mid = (u.l + u.r) / 2;
             a[mid] = i++;
             if (u.l <= mid - 1)
                 q.push({u.l, mid - 1});
             if (mid + 1 <= u.r)
                 q.push({mid + 1, u.r});
         }
         for (int i = 1; i <= n; i++)
             cout << a[i] << ' ';
         cout << '\n';
     }
    }

    ::::

    CF1579D Productive Meeting

    难度: 普及/提高-(下位)

    分析&解析

    贪心策略是:每次社交次数最大的人与第二大的人社交。 所以需要一个大根堆(优先队列)来维护社交次数最大的人。

    Code:

    ::::info[Code:]

    #include <iostream>
    #include <queue>
    #include <map>
    #include <algorithm>
    using namespace std;
    int t;
    int n;
    struct node {
     int sj, id;
    };
    struct cmp{
     bool operator () (const node& x,const node& y){
         return x.sj < y.sj;
     }
    };
    int main() {
     cin >> t;
     while (t--) {
         cin >> n;
         priority_queue<node,vector<node>,cmp> q;
         for (int i = 1; i <= n; i++) {
             int sj;
             cin >> sj;
             q.push({sj,i});
         }
         int ans = 0;
         node b[200010] = {};
         while(1) {
             node u = q.top();
             q.pop();
             node v = q.top();
             q.pop();
             if(u.sj == 0 || v.sj == 0) break;
             b[++ans] = {u.id,v.id};
             u.sj--,v.sj--;
             q.push(u),q.push(v);
         }
         cout << ans << '\n';
         for (int i = 1; i <= ans; i++)
             cout << b[i].sj << ' ' << b[i].id << '\n';
     }
     return 0;
    }

    ::::

    P14971 『GTOI - 2B』DDoS

    难度: 普及/提高-(中位)

    分析&解析

    题解做法: 首先,可以使用全部时间,进行 1 次操作。但可用代价可能不够,所以需要去除一些间隔,使得代价不大于 m;所以,去除长度更长的区间更优。

    Code:

    ::::info[Code:]

    #include <bits/stdc++.h>
    using namespace std;
    int n, m, mn = 1e9, mx;
    int jian[1000005];
    struct node {
     int l, r;
    } a[1000005];
    bool cmp1(node x, node y) {
     return x.l < y.l;
    }
    bool cmp2(int x, int y) {
     return x > y;
    }
    int main() {
     ios::sync_with_stdio(0);
     cin.tie(0), cout.tie(0);
     cin >> n >> m;
     for (int i = 1; i <= n; ++i) {
         cin >> a[i].l >> a[i].r;
         mn = min(mn, a[i].l);
         mx = max(mx, a[i].r);
     }
     sort(a + 1, a + n + 1, cmp1);
     int sz = 0;
     int nowr = a[1].r;
     // 处理间隔
     for (int i = 2; i <= n; ++i) {
         if (a[i].l > nowr + 1)
             jian[++sz] = a[i].l - nowr - 1, nowr = a[i].r;
         else
             nowr = max(nowr, a[i].r);
     }
     if (mx - mn + 1 <= m) {
         cout << 1;
         return 0;
     }
     int sy = mx - mn + 1 - m;
     int ans = 1;
     sort(jian + 1, jian + sz + 1, cmp2);
     for (int i = 1; i <= sz; ++i) {
         sy -= jian[i];
         ++ans;
         if (sy <= 0) {
             cout << ans;
             return 0;
         }
     }
     cout << -1;
     return 0;
    }

    ::::

    CF1777C Quiz Master

    难度: 普及/提高-(上位)

    分析&解析

    观察可以发现,我们选取的人一定是一个连续的区间。

所以我们可以用双指针(Two-pointer)算法来解决这个问题。

本问题需要求出每个数的因数,所以我们可以预处理(n log_2 n)出来每个数的因数。

双指针过程中,用数组 vis_i 表示在 [l,r] 的区间中因数 i 的出现次数,cnt 表示不同的因数的个数。每次循环在 r < n 且 cnt < m 的情况下将 r 加一,并把 a_r 的因数加到 vis 中。如果 cnt = m 就对目前的区间取极差 (a_r - a_l) 做最小值。然后因为 l 要加一,所以要把 a_l 的因数从 vis 中减去。

Code:

::::info[Code:]

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int M = 1e5, N = 1e5 + 10;
int t, n, m, cnt, ans, a[N], vis[N];
vector<int> v[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> t;
    for (int i = 1; i <= M; i++)
        for (int j = i; j <= M; j += i)
            v[j].push_back(i);
    while (t--)
    {
        cin >> n >> m;
        ans = M;
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        sort(a + 1, a + n + 1);
        for (int l = 1, r = 0; l <= n; l++)
        {
            while (r < n && cnt < m)
            {
                r++;
                for (int x : v[a[r]])
                {
                    if (x <= m)
                    {
                        if (!vis[x])
                            cnt++;
                        vis[x]++;
                    }
                }
            }
            if (cnt == m)
                ans = min(ans, a[r] - a[l]);
            for (int x : v[a[l]])
            {
                if (x <= m)
                {
                    vis[x]--;
                    if (!vis[x])
                        cnt--;
                }
            }
        }
        cout << (ans < M ? ans : -1) << endl;
    }
    return 0;
}

::::