【解题报告】Codeforces Round #410 (Div. 2)

题目链接


A. Mike and palindrome(Codeforces 798A)

思路

本题的入手点为“恰好改变一个字符”。
因为能且仅能改变一个字符,所以可以枚举被改变的字符,接着尝试将其改变成任意跟原字符不同的字符,然后判断新的字符串是否是回文串。
注意,每次判断完新字符串是否是回文串后要将字符串改回原串,这样才不会影响下次修改。

代码

#include <bits/stdc++.h>
using namespace std;

string s, t;
int n;

// 判断字符串是否是回文串
bool isP(string s) {
    n = s.size();
    for(int i = 0; i < (n + 1) / 2; i++) {
        if(s[i] != s[n - i - 1]) {
            return false;
        }
    }
    return true;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> s;
    n = s.size();
    t = s;
    // 枚举被改变的字符
    for(int i = 0; i < n; i++) {
        // 枚举改变成的字符 
        for(int j = 0; j < 26; j++) {
            if(s[i] == 'a' + j) {
                continue;
            }
            t[i] = 'a' + j;
            if(isP(t) == true) {
                puts("YES");
                return 0;
            }
            // 要改回去,不然影响下次判断
            t[i] = s[i];
        }
    }
    puts("NO");
    return 0;
}

B. Mike and strings(Codeforces 798B)

思路

本题的入手点是,给的字符串数量并不多,于是可以用暴力的方法处理。
可以证明,选定某个字符串,将其它字符串都改成它,这一定是最优的策略。那么我们可以枚举这个“模板”字符串 Si ,接着可以计算出每个字符串改变成它所需要的操作次数 Resi ,然后将 Resi 跟当前最优解 ans 比较并且更新。
枚举的复杂度是 O(n) ,将字符串做一系列操作以确定是否能变成模板串的复杂度是 O(n2) (因为字符串不是用链表存储的,所以删除首字符的复杂度为 O(n) ),比较两个字符串是否相等的复杂度为 O(n) 。于是总的复杂度就是 O(n4)
如上述,可以用链表来将这个算法优化至 O(n3)

代码

#include <bits/stdc++.h>
using namespace std;

const int maxn = 55, INF = 55 * 55;
string S[maxn], T[maxn];
int n, res, ans;

// 判断其它字符串是否能变成S[idx]
int solve(int idx) {
    int res = 0;
    for(int i = 1; i <= n; i++) {
        if(idx == i) {
            continue;
        }
        T[i] = S[i];
        int m = T[i].size();
        bool ok = false;
        // 反复将首字符加到尾部
        for(int j = 0; j < m; j++) {
            if(T[i] == S[idx]) {
                ok = true;
                res += j;
                break;
            }
            T[i].push_back(T[i][0]);
            T[i] = T[i].substr(1, m);
        }
        if(ok == false) {
            return -1;
        }
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> S[i];
        T[i] = S[i];
    }
    ans = INF;
    // 枚举模板字符串
    for(int i = 1; i <= n; i++) {
        res = solve(i);
        if(res < 0) {
            cout << -1 << endl;
            return 0;
        }
        ans = min(ans, res);
    }
    cout << ans << endl;
    return 0;
}

C. Mike and gcd problem(Codeforces 798C)

思路

本题的入手点是,尝试了若干数据后可以发现,问题的本质是奇偶性而不是最大公约数。
首先,对整个序列求一个 GCD ,若 GCD 大于 1 的话就可以直接输出答案了。
否则,先不考虑整个数组。考虑数组中的两个相邻元素 aiai+1 ,若它们是互质的,那么最多可以通过两次操作将他们改成 gcd(ai,ai+1)=2 的形式。为什么呢?我们考虑奇数偶数之间的加减关系:

  • 奇数 ± 奇数 = 偶数
  • 偶数 ± 偶数 = 偶数
  • 奇数 ± 偶数 = 奇数
  • 偶数 ± 奇数 = 奇数

那么我们有两种变换策略:

  1. 若两个相邻的数是互质的且两个数的奇偶性相同的话,则最多一次操作就可以让他们的 gcd 变成 2
  2. 若两个相邻的数是互质的且两个数的奇偶性不同的话,则通过两次操作(先变成两个奇数,再变成两个偶数)就可以让他们的 gcd 变成 2

到这里,我们别无选择,只能用这种两种策略,来拼凑出一个解。为什么这样是最优的呢?因为找不出更优的策略了(哈哈~抱歉^^)。那么问题就转化成怎么用这两种策略来将数组中的所有数变成偶数。这里同样有两种方法:

  1. 贪心。先将所有相邻的奇数通过一次变换双双变成偶数,再将所有的相邻的奇偶性不同的数通过一次变换双双变成偶数。
  2. 动态规划。如果对贪心的算法不放心的话用动态规划是最保险的。令 d[i][0] 表示当前考虑完第 i 个数,之前的数( a[k],k<i )都被改成偶数,且 a[i] 被改变成奇数时的最少修改次数。 d[i][1] 同理,是 a[i] 被改变成偶数的情况。状态转移的细节见代码中的注释。

代码1(贪心)

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 1e5 + 10, INF = 1e8;
ll x, y, n, g, ans, a[maxn];

ll gcd(ll x, ll y) {
    return y ? gcd(y, x % y) : x;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    g = a[1];
    for(int i = 2; i <= n; i++) {
        g = gcd(g, a[i]);
    }
    if(g > 1) {
        cout << "YES" << endl << 0 << endl;
        return 0;
    }
    // 处理相邻两个数都是奇数的情况
    for(int i = 1; i < n; i++) {
        if(a[i] % 2 != 0 && a[i + 1] % 2 != 0) {
            x = a[i];
            y = a[i + 1];
            a[i] = x - y;
            a[i + 1] = x + y;
            ans++;
        }
    }
    // 处理剩下的情况
    for(int i = 1; i < n; i++) {
        if(a[i] % 2 == 0 && a[i + 1] % 2 == 0) {
            continue;
        }
        for(int j = 1; j <= 2; j++) {
            x = a[i];
            y = a[i + 1];
            a[i] = x - y;
            a[i + 1] = x + y;
            ans++;
        }
    }
    cout << "YES" << endl << ans << endl;
    return 0;
}

代码2(DP)

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 1e5 + 10, INF = 1e8;
ll x, y, n, g, ans, a[maxn], d[maxn][2];

ll gcd(ll x, ll y) {
    return y ? gcd(y, x % y) : x;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    g = a[1];
    for(int i = 2; i <= n; i++) {
        g = gcd(g, a[i]);
    }
    if(g > 1) {
        cout << "YES" << endl << 0 << endl;
        return 0;
    }
    // DP的初始化
    for(int i = 1; i <= n; i++) {
        d[i][0] = d[i][1] = INF;
    }
    if(a[1] % 2 == 0) {
        d[1][0] = 0;
    }
    else {
        d[1][1] = 0;
    }
    for(int i = 2; i <= n; i++) {
        if(a[i] % 2 == 0) {
            // 当前a[i]是偶数且保持不变的情况
            d[i][0] = min(d[i-1][0], d[i-1][1] + 2);
        }
        else {
            // 当前a[i]是奇数且被修改成偶数时
            d[i][0] = min(d[i-1][0] + 2, d[i-1][1] + 1);
            // 当前a[i]是奇数且保持不变时
            d[i][1] = d[i-1][0];
        }
    }
    ans = d[n][0];
    cout << "YES" << endl << ans << endl;
    return 0;
}

D. Mike and distribution(Codeforces 798D)

思路

本题的入手点是,将条件简化和排序后将问题简化。
首先注意到我们应该在子集中包含尽可能多的数,所以构造的子集中最好拿满 n2+1 个数。
其次题目的条件“子集和的两倍大于数组和”等价于“子集和大于子集的关于全集的补集的和”。
因为题目给了两个数组,同时考虑两个数组求子集会比较难。因此可以用一个三元组数组 C A B 的信息保存下来,其中 C[i]=(A[i],B[i],i) ,然后对 C A 从大到小的顺序排序。排序的结果是,若我们拿 C 中相对靠前的数就很容易令 A 的子集的和达到条件。但是 B 的子集和怎么办呢?
很容易证明,对于一个乱序的序列,当我们两个两个地遍历数组时(假设当前遍历到 a[i],a[i+1] ),每次都将 max(a[i],a[i+1]) 加入子集就能使子集满足条件。
回到问题中来。在我们对 C 排序后,可以两个两个地遍历数组,每次拿 C[i],C[i+1] B <script type="math/tex" id="MathJax-Element-44">B</script> 较大的就可以了。如果数组元素有奇数个的话,先将第一个元素加入数组,然后在两个两个遍历数组即可。

代码

#include <bits/stdc++.h>
using namespace std;

typedef tuple <int, int, int> t;
const int maxn = 1e5 + 5;
t ts[maxn];
vector <int> ans;
int n, a, b, c, d, idx, idx2;
int x[maxn], y[maxn];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> x[i];
    }
    for(int i = 1; i <= n; i++) {
        cin >> y[i];
    }
    for(int i = 1; i <= n; i++) {
        ts[i] = t(x[i], y[i], i);
    }
    ts[0] = t(0, 0, 0);
    // 排序
    sort(ts + 1, ts + n + 1);
    // 将A最大的先加入子集
    tie(a, b, idx) = ts[n];
    ans.push_back(idx);
    // 两个两个遍历
    for(int i = n - 1; i >= 1; i -= 2) {
        tie(a, b, idx) = ts[i];
        tie(c, d, idx2) = ts[i - 1];
        if(b > d) {
            ans.push_back(idx);
        }
        else {
            ans.push_back(idx2);
        }
    }
    cout << ans.size() << endl;
    for(int val : ans) {
        cout << val << ' ';
    }
    return 0;
}

(其它题目略)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值