2025.2.27做题记录

1743C:Save the Magazines

题目链接:https://codeforces.com/problemset/problem/1743/C

经验:有关数组越界,从后往前数要防止数到超出0的范围,同时要防止尾部a[0]添加超过1次。

Ps:感觉自己写的代码好冗长(っ╥╯﹏╰╥c)

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

int main(){
    ll t;
    cin >> t;
    while(t--){
        ll n;
        cin >> n;
        string s;
        cin >> s;
        ll a[n];
        ll ans = 0;
        for (ll i = 0; i < n;i++){
            cin >> a[i];
        }
        for (ll i = n - 1; i >= 0;i--){
            if(s[i]=='1'){
                if(i==0){
                    ans += a[i];
                    break;
                }
                if(s[i-1]=='0'){
                    if(a[i-1]>a[i]){
                        ans += a[i - 1];
                        i--;
                    }else{
                        ans += a[i];
                        i--;
                    }
                }else{
                    ll origin = a[i];
                    bool c = false;
                    while(s[i-1]!='0'){
                        if(a[i-1]<origin){
                            ans += origin;
                            origin = a[i - 1];
                        }else{
                            ans += a[i - 1];
                        }
                        if(i==1){
                            ans += origin;
                            c = true;
                            break;
                        }
                        i--;
                    }
                    if(!c){
                        if(a[i-1]>origin){
                            ans += a[i - 1];
                        }else{
                            ans += origin;
                        }
                        i--;
                    }else{
                        break;
                    }
                }    
            }
        }
        cout << ans << endl;
    }
    return 0;
}

1994B:Fun Game

题目链接:https://codeforces.com/problemset/problem/1994/B

经验:一直卡在第十个样例超时,因为我把字符串的比较写在循环里面了,而两个字符串如果长度相等的话,时间复杂度是O(n)的,这样叠加起来就导致第十个样例超时了。两个字符串的比较只有长度不等的时候,时间复杂度是O(n)的。

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

int main(){
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        string s, t;
        cin >> s >> t;
        if(s==t){
            cout << "YES" << endl;
            continue;
        }
        for (int i = 0; i < n;i++){
            if (s[i] == '1')
            {
                cout << "YES" << endl;
                break;
            }
            else if (t[i] == '1')
            {
                cout << "NO" << endl;
                break;
            }
            
        } 
        
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值