Codeforces Round #695 (Div. 2)

Codeforces Round #695 (Div. 2)

A. Wizard of Orz

本来应该是987。。。的,因为这里可以操作一个数所以可以偷一个989然后就012。。。往后接。可以让他最大。

#include <bits/stdc++.h>

#define int long long
using namespace std;
//const int mod = 998244353;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 10;

void solve() {
    
    
    int n;
    cin>>n;
    if(n<=3){
    
    
        if(n==1) cout<<9;
        if(n==2) cout<<98;
        if(n==3) cout<<989;
    } else{
    
    
        cout<<989;
        string t="0123456789";
        for (int i = 0; i < n - 3; ++i)
            cout<<t[i%10];
    }
    cout<<"\n";
}

signed main() {
    
    
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--)
        solve();
    return 0;
}


B. Hills And Valleys

直接遍历一遍每个数都改为左右俩个数时峰减少的数量,记个最大值最后和原数组峰个数相减即可

#include <bits/stdc++.h>

//#pragma GCC optimize(2)
#define int long long
using namespace std;
//const int mod = 998244353;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 10;

int a[maxn];
int n;

int check(int i){
    
    
    if(i==0||i==n-1) return 0;
    return (a[i+1]>a[i]&&a[i-1]>a[i])||(a[i+1]<a[i]&&a[i-1]<a[i]);
}

void solve() {
    
    
    cin>>n;
    int count=0;
    for (int i = 0; i < n; ++i) cin>>a[i];
    for (int j = 1; j < n-1; ++j) {
    
    
        if(check(j)) count++;
    }
    int max_=0;
    for (int i = 1; i < n-1; ++i) {
    
    
        int yl=check(i-1)+check(i)+check(i+1);
        int l=a[i-1],r=a[i+1],m=a[i];
        a[i]=l;
        int bl=check(i-1)+check(i)+check(i+1);
        max_=max(max_,yl-bl);
        a[i]=r;
        int br=check(i-1)+check(i)+check(i+1);
        max_=max(max_,yl-br);
        a[i]=m;
    }
    cout<<count-max_<<"\n";
}

signed main() {
    
    
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--)
        solve();
    return 0;
}


猜你喜欢

转载自blog.csdn.net/weixin_45436102/article/details/112589943