牛客小白月赛87

说明

        年后第一次写题,已经麻了,这次的题很简单但居然只写了两道题。有种本该发挥80分的水平,但是只做出了20分的水平的感觉。不过剩下几个题(除了G题),比完赛一小时内就AC了。欢迎大家交流学习。(最后的G题没做,目前还不打算开始练这种题)

题目列表

A-小苯的石子游戏

思路

        模拟双方每次都取最大的,然后比较双方大小输出即可。

参考题解
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 1e2+5;
int a[N];
void solve(){
    int n;
    cin >> n;
    for(int i = 1;i<=n;i++) cin >> a[i];
    int Alice=0,Bob=0;
    for(int i = n;i>=1;i--){
        if((n&1)==(i&1)) Alice+=a[i];
        else Bob+=a[i];
    }
    if(Alice>Bob) cout << "Alice" << endl;
    else cout << "Bob" << endl;
}
signed main(){
    //小苯的石子游戏 
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int T = 1;
	cin >> T;
	while(T--){
		solve();
	}
	return 0;
}

 B-小苯的排序疑惑

思路

        模拟排一下即可。(我这场就是一直卡在这题,我一开始想太复杂了,if讨论没明白,害~)

参考题解
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 2e5+5;
int a[N],b[N],c[N];
int n;
bool check(int *a){
    for(int i = 1;i<=n-1;i++) if(a[i]>a[i+1]) return false;
    return true;
}
void solve(){
    cin >> n;
    for(int i = 1;i<=n;i++) cin >> a[i];
    for(int i = 1;i<=n;i++){
        b[i]=a[i];
        c[i]=a[i];
    }
    if(check(a)) cout << "YES" << endl;
    else{
        sort(b+1,b+n);
        sort(c+2,c+n+1);
        if(!check(b)&&!check(c)) cout << "NO" << endl;
        else cout << "YES" << endl;
    }
}
signed main(){
    //小苯的排序疑惑 
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int T = 1;
    cin >> T;
    while(T--){
        solve();
    }
    return 0;
}

C-小苯的IDE括号问题(easy)

思路

        用双指针模拟即可。

参考题解
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
// const int N = 2e5+5;
string s;
void solve(){
    int n,k;cin >> n >> k;
    cin >> s;
    int pos=s.find('I');
    int l=pos-1;
    int r=pos+1;
    while(k--){
        string op;
        cin >> op;
        if(op=="backspace"){
            if(l>=0){
                if(s[l]=='('&&s[r]==')'&&r<n) r++;
                l--;
            }
        }else{
            if(r<n) r++;
        }
    }
    for(int i = 0;i<=l;i++) cout << s[i];
    cout << 'I';
    for(int i = r;i<n;i++) cout << s[i];
    cout << endl;
}
signed main(){
    //小苯的IDE括号问题(easy) 
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int T = 1;
//     cin >> T;
    while(T--){
        solve();
    }
    return 0;
}

D-小苯的IDE括号问题(hard)

思路

        用两个数组模拟四种情况,用deque似乎也能实现,不过我还不太会用deque。

参考题解
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 2e5+5;

void solve(){
    int n,k;cin >> n >> k;
    string s;cin >> s;
    int pos = s.find('I');
    vector<char> l;
    vector<char> r;
    for(int i = 0;i<pos;i++){
        l.emplace_back(s[i]);
    }
    for(int i = pos+1;i<n;i++){
        r.emplace_back(s[i]);
    }
    reverse(r.begin(),r.end());
    while(k--){
        string op;
        cin >> op;
        if(op=="backspace"){
            if(l.size()){
                if(l.back()=='('&&r.back()==')'&&r.size()) r.pop_back();
                l.pop_back();
            }
        }else if(op=="delete"){
            if(r.size()) r.pop_back();
        }else if(op=="->"){
            if(r.size()){
                l.emplace_back(r.back());
                r.pop_back();
            }
        }else{
            if(l.size()){
                r.emplace_back(l.back());
                l.pop_back();
            }
        }
    }
//     for(int i = 0;i<l.size();i++) cout << l[i];
    for(char &c:l) cout << c;
    cout << 'I';
    if(r.size()){
        reverse(r.begin(),r.end());
        for(char &c:r) cout << c;
    }
//     for(int i = r.size()-1;i>=0;i--) cout << r[i];
    cout << endl;
}
signed main(){
    //小苯的IDE括号问题(hard) 
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int T = 1;
//     cin >> T;
    while(T--){
        solve();
    }
    return 0;
}

E-小苯的数组构造

思路

        读懂题就很好做,需要考虑前缀最大值,然后模拟一下。

参考题解
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 2e5+5;

void solve(){
    int n;cin >> n;
    vector<int> a(n+1);
    for(int i = 1;i<=n;i++) cin >> a[i];
    int maxn = a[1];
    vector<int> ans(n+1);
//     ans[1]=0;
    for(int i = 2;i<=n;i++){
        int now=max(0,maxn-a[i]);
        ans[i]=now;
        maxn=max(maxn,a[i]);
    }
    for(int i = 1;i<=n;i++) cout << ans[i] << " \n"[i==n];
}
signed main(){
    //小苯的数组构造  
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int T = 1;
//     cin >> T;
    while(T--){
        solve();
    }
    return 0;
}

G-小苯的逆序对

思路

        主要的思路是贪心,我们知道多个数如果做与运算,值肯定变小,根据题意,与的和就求最后一个数即可,前面的异或和或运算用前缀和的思想求一遍,然后最后用一个循环求和的最大值。

参考题解
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve(){
    int n;cin >> n;
//     cout << fixed << setprecision(20);
    vector<ll> a(n);
    for(ll& ai:a) cin >> ai;
    vector<ll> x(n);
    vector<ll> o(n);
    for(int i = 0;i<n-1;i++){
        if(i==0) x[i]=0LL^a[i];
        else x[i]=x[i-1]^a[i];
    }
    for(int i = n-2;i>=0;i--){
        o[i]=o[i+1]|a[i];
    }
    ll ans=0;
    for(int i = 0;i<n-2;i++){
        ans=max(ans,x[i]+o[i+1]+a.back());
    }
    cout << ans << endl;
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int T = 1;
//     cin >> T;
    while(T--){
        solve();
    }
    return 0;
}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Beau_Will

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值