时隔一年的补题

2022 RoboCom 世界机器人开发者大赛-本科组(省赛)

T3 跑团

两种写法大同小异:

1.

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
#define Mod 7452454545
#include <map>
#include <set>
#include <unordered_map>
#include <vector>
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
#define PLL pair<long, long>
#define x first
#define y second
#define INF 0x3f3f3f3f
#define ULL unsigned long long
#define kanm7 ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
using namespace std;

map<int,int> st;

int main() {
    string s; cin >> s;
    int ma = 0, mn = 0;
    if(s[0] != '+' && s[0] != '-') s.insert(s.begin(), '+');
    int n = s.size();
    for(int i = 0; i < n; ) {
        int j = i + 1; // 运算数的开头
        while(j < n && s[j] != '+' && s[j] != '-') j ++;
        if(s[i] == '-') {
            int a = 0, b = 0, f = 1;
            for(int k = i + 1; k < j; k ++) {
                if(s[k] == 'd') {
                    f = 0;
                    continue;
                }
                if(f) a = a * 10 + s[k] - '0';
                else b = b * 10 + s[k] - '0';
            }
            if(f) {
                ma -= a, mn -= a;
            } else {
                if(!a) a ++;
                st[b] += a;
                ma -= 1 * a, mn -= b * a;
            }
        } else {
            int a = 0, b = 0, f = 1;
            for(int k = i + 1; k < j; k ++) {
                if(s[k] == 'd') {
                    f = 0;
                    continue;
                }
                if(f) a = a * 10 + s[k] - '0';
                else b = b * 10 + s[k] - '0';
            }
            if(f) {
                ma += a, mn += a;
            } else {
                if(!a) a ++;
                st[b] += a;
                ma += b * a, mn += 1 * a;
            }
        }
        i = j;
    }
    for(auto it : st) {
        cout << it.first << ' ' << it.y << endl;
    }
    cout << mn << ' ' << ma << endl;
    return 0;
}

2.就是第二种写法,decltype这个关键字用法还不是很熟。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
#define Mod 7452454545
#include <map>
#include <set>
#include <unordered_map>
#include <vector>
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
#define PLL pair<long, long>
#define x first
#define y second
#define INF 0x3f3f3f3f
#define ULL unsigned long long
#define kanm7 ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
using namespace std;
map<int,int> st;
vector<string> add, sub;

int get(string s) {
    int res = 0;
    for(int i = 0; i < s.size(); i ++) {
        res = res * 10 + s[i] - '0';
    }
    return res;
}

int main() {
    string s; cin >> s;
    // 把加减分类
    if(s[0] != '-') {
        s.insert(s.begin(), '+');
    }
    // cout << s << endl;
    int n = s.size();
    for(int i = 0; i < n; ) {
        int j = i + 1;
        while(j < n && (s[j] != '+' && s[j] != '-')) j ++;
        if(s[i] == '+') add.push_back(s.substr(i + 1, j - i - 1));
        else {
            sub.push_back(s.substr(i + 1, j - i - 1));
        }
        i = j;
    }
    // 把dy这样的前面补上1
    int s1 = 0, s2 = 0;
    for(int i = 0; i < add.size(); i ++) {
        regex r("^d");
        add[i] = regex_replace(add[i], r, "1d");
        regex r1("d");
        sregex_token_iterator pos(add[i].begin(), add[i].end(), r1, -1);
        decltype(pos) end;
        int f = 0;
        int x = 0, y = 0;
        for(; pos != end; pos ++) {
            if(!f) {
                f ++;
                x = get(pos->str());
            } else {
                y = get(pos->str());
            }
        }
        if(!y) {
            s1 += x, s2 += x;
            continue;
        }
        s1 += x * 1, s2 += x * y;
        st[y] += x;
    }
    for(int i = 0; i < sub.size(); i ++) {
        regex r("^d");
        sub[i] = regex_replace(sub[i], r, "1d");
        regex r1("d");
        sregex_token_iterator pos(sub[i].begin(), sub[i].end(), r1, -1);
        decltype(pos) end;
        int f = 0;
        int x = 0, y = 0;
        for(; pos != end; pos ++) {
            if(!f) {
                f ++;
                x = get(pos->str());
            } else {
                y = get(pos->str());
            }
        }
        if(!y) {
            s1 -= x, s2 -= x;
            continue;
        }
        s1 -= x * y;
        s2 -= x * 1;
        st[y] += x;
    }
    for(auto it : st) {
        cout << it.first << ' ' << it.second << endl;
    }
    cout << s1 << ' ' << s2 << endl;
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kanm7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值