Educational Codeforces Round 167 (Rated for Div. 2)解题报告(A-C)

Educational Codeforces Round 167 (Rated for Div. 2)解题报告(A-C)

A. Catch the Coin

链接:A. Catch the Coin
题目:
在这里插入图片描述

解题思路:
显然只需要y >= -1 即可收集到硬币

代码:

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

int main() {
    int n, x, y;
    cin >> n;
    while (n--) {
        cin >> x >> y;
        if (y < -1) cout << "NO\n";
        else cout << "YES\n";
    }
    return 0;
}

B - Substring and Subsequence

链接:B - Substring and Subsequence
题目:在这里插入图片描述

解题思路:
b为字符串的连续子串,故a中的重复元素必须与b连续匹配,找出最长的匹配长度,在将两字符串长度相加减去匹配长度即为答案

代码:

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

int main() {
    int n;
    cin >> n;
    while (n--) {
        string a, b;
        cin >> a >> b;
        int begin = 0;
        int n = a.size(), m = b.size();
        int ans = n + m;
        for (int i = 0; i < m; i++) {
            int j = i;
            for (auto c : a) {
                if (b[j] == c) j++;
            }
            ans = min(ans, m+n-(j-i)); //m+n-(j-i)就是匹配长度
        }
        cout << ans << '\n';
    }
    return 0;
}

C - Two Movies

链接:C - Two Movies
题目:
在这里插入图片描述
解题思路:
先将更喜欢其中某部电影的人肯定对更喜欢的那部电影评分更有利,先记录下来,然后再对评价一样的人进行遍历讨论

代码:

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

int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (auto& x : a) cin >> x;
    for (auto& x : b) cin >> x;
    int x = 0, y = 0, neg = 0, pos = 0;
    for (int i = 0; i < n; ++i) {
      if (a[i] > b[i]) {
        x += a[i];
      } else if (a[i] < b[i]) {
        y += b[i];
      } else {
        neg += (a[i] < 0); //两部都讨厌的人数
        pos += (a[i] > 0); //两部都喜欢的人数
      }
    }
    int ans = -1e9;
    //遍历两边评价一样的人
    for (int i = -neg; i <= pos; ++i)
        //当i为负值时,都不喜欢的人中有|i|个不喜欢的人在第一部电影,有neg-|i|个在第二部电影,有pos个都喜欢的人在第二部电影处 
        //喜欢第一部评分减|i|,变为x-|i|,因为i为负值,|i| = -i,x+i
        //喜欢第二部评分加pos,减neg-|i|,变为y + pos - (neg-|i|),因为i为负值,|i| = -i,y + (pos - neg - i)
        
        //i为正值时,i代表变为都喜欢的人,同理,y - neg + (pos - i) == y + (pos - neg - i)

        //此时,第一部评分为 x+i, 第二部评分为 y + (pos - neg - i)
        ans = max(ans, min(x + i, y + (pos - neg - i))); //在所有情况中,选择"这两个值中最小值"最大的选项。
    cout << ans << '\n';
  }
}

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值