Codeforces Round #753 (Div. 3) ABCD

A. Linear Keyboard
B. Odd Grasshopper
C. Minimum Extraction
D. Blue-Red Permutation

A. Linear Keyboard

请添加图片描述
思路分析
思维题都不算。。。读懂题意。。。有手就行。。。

AC代码

#include <iostream>
#include <algorithm>

using namespace std;
typedef pair<int,int> PII;
typedef long long ll;

const int N = 1e3 + 10;

int t;
string s;
string str;
int ans;

int main() {
    cin >> t;
    while(t--) {
        cin >> str;
        cin >> s;
        ans = 0;
        for(int i = 1; i < s.size(); i++) {
            int a,b;
            for(int j = 0; j < str.size(); j++) {
                if(s[i - 1] == str[j])
                    a = j + 1;
                if(s[i] == str[j])
                    b = j + 1;
            }
            ans += abs(a-b);
        }
        cout << ans << endl;
    }
    return 0;
}

B. Odd Grasshopper

请添加图片描述
思路分析
简单分析,发现4个为一组,会回归 x0,因此只需要求最后几次偏移量就可以了,分奇偶情况,奇偶刚好相反,最后记得加上偏移量

AC代码

#include <iostream>
#include <algorithm>

using namespace std;
typedef pair<int, int> PII;
typedef long long ll;

const int N = 1e3 + 10;

int t;
ll n, m;
ll ans;

int main() {
    cin >> t;
    while (t--) {
        cin >> m >> n;
        ans = 0;
        ll p = n / 4;
        ll k = p * 4;
        n = n % 4;
        // 如果m是奇数
        if (m & 1) {
            for (int i = 0; i < n; i++) {
                if (i == 0)
                    ans += k + 1;
                if (i == 1)
                    ans -= k + 2;
                if (i == 2)
                    ans -= k + 3;
            }
            //  如果m是偶数
        } else {
            for (int i = 0; i < n; i++) {
                if (i == 0)
                    ans -= k + 1;
                if (i == 1)
                    ans += k + 2;
                if (i == 2)
                    ans += k + 3;
            }
        }
        ans += m;
        cout << ans << endl;
        
    }
    return 0;
}

C. Minimum Extraction

请添加图片描述
思路分析
先将原来的数组进行排序,然后存入 v[0],后续存入的值即为 v[i] - v[i - 1],利用的是数列都增减相同数字时,相邻两项的差值不变。

AC代码

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
 
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
 
const int N = 1e3 + 10;
 
int t;
ll n;
 
int main() {
    cin >> t;
    while (t--) {
        cin >> n;
 
        vector<ll> v;
        for(int i = 0; i < n; i++) {
            ll a;
            cin >> a;
            v.push_back(a);
        }
        if(n == 1) {
            cout << v[0] << endl;
            continue;
        }
        set<ll> s;
        sort(v.begin(), v.end());
        s.insert(v[0]);
        for(int i = 1; i < v.size(); i++)
            s.insert(v[i] - v[i - 1]);
        cout << *s.rbegin() << endl;
    }
    return 0;
}

D. Blue-Red Permutation

请添加图片描述
思路分析
贪心策略:按照颜色作为第一优先级,蓝色排在前面,红色排在后面;其次是大小,小的排在前面,大的排在后面。

AC代码

#include <iostream>
#include <algorithm>
 
using namespace std;
typedef pair<int, char> PII;
typedef long long ll;
 
const int N = 2e5 + 10;
 
int t;
int n;
PII a[N];
string s;
 
bool cmp(PII a, PII b) {
    if(a.second == b.second)
        return a.first < b.first;
    else
        return a.second < b.second;
}
 
bool solve() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i].first;
    }
    cin >> s;
    for (int i = 1; i <= n; i++) {
        a[i].second = s[i - 1];
    }
    sort(a + 1, a + 1 + n, cmp);
    for (int i = 1; i <= n; i++) {
        if (a[i].second == 'B' && a[i].first < i) return 0;
        if (a[i].second == 'R' && a[i].first > i) return 0;
    }
    return 1;
}
 
 
int main() {
    cin >> t;
    while (t--) {
        if (solve())
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值