暑假刷题记录-2023

牛客周赛

1.

#include 

using namespace std;

int main()
{
    string s,t;
    cin>>s>>t;
//     s += s;
    for (int i = 0;i < t.size()-1;i ++) {
        s += s[i];
    }
    int ans = 0;
    for (int i = 0;i < s.size();i ++) {
//         cout<
        if (i+t.size()-1 < s.size()&&s.substr(i,t.size()) == t) {
            ans ++;
        }
    }
    cout<<ans<<endl;
    return 0;
}

2.

#include 

using namespace std;
const int N = 100010;
#define ll long long
int a[N];
char b[N];
ll dp[N][2];
int main()
{
    int n;cin>>n;
    for (int i = 1;i <= n;i ++) 
        cin>>a[i];
    for (int i = 1;i <= n;i ++) 
        cin>>b[i];
    ll sum = 0;
    for (int i = 1;i <= n;i ++) {
        if (b[i] == 'R'&&b[i-1] == 'B') {
            dp[i][1] = dp[i-1][0] + a[i] + a[i-1];
        } else if (b[i] == 'B'&&b[i-1] == 'R') {
            dp[i][1] = dp[i-1][0] + a[i] + a[i-1];
        }
        dp[i][0] = max(dp[i-1][1],dp[i-1][0]);
    }
    sum = max(dp[n][1],dp[n][0]);
    cout<<sum<<endl;
    return 0;
}

3.

#include 

using namespace std;
const int N = 100010;
#define ll long long
int a[N];
char b[N];
ll dp[N][2];
int main()
{
    int n;cin>>n;
    int h[10] = {0};
    while (n --) {
        int a,b;cin>>a>>b;
        if (a == 0) {
            int a = max(h[b],h[b+1]);
            h[b] =a + 3;h[b+1] = a + 1;
        } else if (a == 90) {
            if (h[b] >= h[b+1]&&h[b] >= h[b+2]) {
                h[b] = h[b+1] = h[b+2] = h[b] + 2;
            } else {
                h[b] = h[b+1] = h[b+2] = max(h[b+1],h[b+2]) + 1;
            }
        } else if (a == 180) {
            if (h[b] - 2 >= h[b+1]) 
                h[b] = h[b+1] = h[b]+1;
            else {
                h[b] = h[b+1] = h[b+1] + 3;
            }
        } else {
            int a = max(h[b],max(h[b+1],h[b+2]));
            h[b] = h[b+1] = a + 1;
            h[b+2] = a + 2;
        }
    }
    for (int i = 1;i <= 8;i ++) 
        cout<<h[i]<<" ";
    cout<<endl;
    return 0;
}

4.

#include
using namespace std;
#define int long long

int n,h,k;

signed main(){
    scanf("%lld%lld%lld",&n,&h,&k);
    vector<int> moster(n);
    for(int i=0;i<n;i++){
        int a,b;scanf("%lld%lld",&a,&b);
        moster[i]=(a/4)*3*b;
        if(a%4==0) moster[i]-=b;
        else if(a%4==3) moster[i]+=b;
    }
    sort(moster.begin(), moster.end());
    // for(int i:moster) cout<
    // cout<<"\n";
    for(int i=1;i<n;++i) moster[i]+=moster[i-1];

    int q;scanf("%lld",&q);
    while(q--){
        int x;scanf("%lld",&x);
        x=h+k*x;
        int tmp=lower_bound(moster.begin(),moster.end(),x)-moster.begin();
        printf("%lld ",tmp);
    }
    return 0;
}

暑假刷题记录-2023_第1张图片

你可能感兴趣的:(算法)