Educational Codeforces Round 159 (Rated for Div. 2)

Educational Codeforces Round 159 (Rated for Div. 2)

A

贪心,看1和3的位置即可

#include 

using namespace std;

void solve()
{
    string s;
    cin >> s;
    int a = s.find('1') , b = s.find('3');
    if(a > b)cout << "31\n";
    else cout << "13\n";
}

int main()
{
    int T;
    cin >> T;
    while(T --){
        solve();
    }
}

B

当且仅当0和1相同紧挨着存在时成立

#include 

using namespace std;

void solve()
{
    string a , b;
    cin >> a;
    cin >> b;
    int n = a.size() , c0 = -1 , c1 = -1;
    for(int i = 0 ; i < n - 1; i ++){
        if(a[i]=='0'&&a[i+1]=='1'&&b[i]=='0'&&b[i+1]=='1'){
            cout<<"YES\n";
            return ;
        }
    }
    cout << "NO\n";
}

int main()
{
    int T;
    cin >> T;
    while(T --){
        solve();
    }
}

C

令cnt为当前数量,0n1一定已排序,0n2一定未排序

#include 

using namespace std;

void solve()
{
    string s;
    cin >> s;
    int cnt = 0 , n1 = 0 , n2 = 0 , n = s.size();
    bool f = true;
    //0~n1一定已排序,0~n2一定未排序
    for(int i = 0 ; i < n ;i ++){
        if(s[i] == '0'){
            if(cnt < 2 || cnt == n1)f = false;
            if(n2 == 0)n2 = cnt;
        }else if(s[i] == '1'){
            if(n2 != 0)f = false;
            n1 = cnt;
        }else if(s[i] == '+'){
            cnt ++;
        }else if(s[i] == '-'){
            cnt --;
            if(cnt < n1)n1 = cnt;
            if(cnt < n2)n2 = 0;
        }
    }
    if(f)cout << "YES\n";
    else cout << "NO\n";
}

int main()
{
    int T;
    cin >> T;
    while(T --){
        solve();
    }
}

D

枚举正数和负数的边界

#include
using namespace std;
int t,n,a[200005];
int main(){
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=1;i<=n;i++)cin>>a[i];
        int ans=INT_MAX,now=0,now1=0;
        //在0分界
        for(int i=1;i<n;i++)if(a[i]>=a[i+1])now++;
        ans=min(ans,now);
        for(int i=1;i<n;i++){
            //枚举在i分界
            if(a[i]>=a[i-1])now1++;
            if(a[i]>=a[i+1])now--;
            ans=min(ans,now+now1);
        }
        cout<<ans<<"\n";
    }
    return 0;
}

你可能感兴趣的:(算法,算法,c++,开发语言)