AtCoder Beginner Contest 043

A - Children and Candies (ABC Edit)

n = int(input())
print( n * (n+1) // 2 )

B - Unhappy Hacking (ABC Edit)

用栈模拟一下?但是栈的遍历比较麻烦这里用vector实现

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


int main(){
    string s;
    cin >> s;
    vector<char> q;
    for( auto c : s ){
        if( c == 'B' ){
            if( q.size() ) q.pop_back();
        }
        else q.push_back(c);
    }
    for( auto c : q )
        cout << c;
    return 0;
}

C - Be Together

数据范围太小了,直接枚举目标数字的大小就好了

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


int main(){
    int n , res = INT_MAX;
    cin >> n;
    vector<int> a(n);
    for( auto &i : a ) cin >> i;
    for( int i = -100 , cnt ; i <= 100 ; i ++ ){
        cnt = 0;
        for( auto j : a ) cnt += (i-j)*(i-j);
        res = min( res , cnt );
    }
    cout << res;
}

D - Unbalanced

其实最短的平衡串只有两种情况aaaba,更长的平衡串一定包含这两种,所以遍历一下就好了。

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


int main(){
    string s;
    cin >> s;
    for( int i = 0 ; i + 1 < s.size() ; i ++ )
        if( s[i] == s[i+1] ) cout << i+1 << " " << i+2 , exit(0);
    for( int i =0 ; i + 2 < s.size() ; i ++ )
        if( s[i] == s[i+2] ) cout << i+1 << " " << i+3 , exit(0);
    cout << "-1 -1";
    return 0;

}
posted @ 2023-01-18 18:32  PHarr  阅读(36)  评论(0编辑  收藏  举报