牛客小白月赛87

文章涉及C++编程中的多个技术点,包括数组操作(如排序、构造和切分)、查找算法(如逆序对计数)、以及高级数据结构(如莫比乌斯反演、容斥和树状数组)。展示了如何使用这些技术解决具体问题.
摘要由CSDN通过智能技术生成

A小苯的石子游戏

到自己直接取最大的即可

#include<bits/stdc++.h>
#define int long long
using namespace std; 
const int N=1e6+6;
const int mod=998244353;
int a[N];
void solve() {
	int n;
	cin >> n;
	for(int i=1;i<=n;i++) cin >> a[i];
	int j=0;
	int x=0,y=0;
	for(int i=n;i>=1;i--){
		j++;
		if(j&1) x+=a[i];
		else y+=a[i];
	}
	if(x>y){
		cout<<"Alice"<<'\n';
	}
	else
	cout<<"Bob"<<'\n';
}
signed main(){
	ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

B小苯的排序疑惑

排序之后首尾有一个在原位即可

#include<bits/stdc++.h>
#define int long long
using namespace std; 
const int N=1e6+6;
const int mod=998244353;
int a[N];
int b[N];
void solve() {
	int n;
	cin >> n;
    
	for(int i=1;i<=n;i++){
		cin >> a[i];
        b[i]=a[i];
	}
    sort(a+1,a+n+1);
	if(a[1]==b[1]||b[n]==a[n]){
		cout<<"YES"<<'\n';
	}
	else
	cout<<"NO"<<'\n';
}
signed main(){
	ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

CD小苯的IDE括号问题(hard)

string函数模拟过的但似乎复杂度不对?

#include<bits/stdc++.h>
#define int long long
using namespace std; 
const int N=1e6+6;
const int mod=998244353;
void solve() {
	int n,m;
	cin >> n >> m;
	string a;
	cin >> a;
	int j=0;
    a='#'+a+'#';
	int len=a.length();
	for(int i=0;i<a.length();i++){
		if(a[i]=='I'){
			j=i;
            a.erase(j,1);
            len--;
			break;
		}
	}
	int l=j-1;
	int r=j;
	while(m--){
		string s;
		cin >> s;
		if(s=="backspace"){
			if(a[l]=='('&&a[r]==')'){
				a.erase(l,1);
                l--;
                r--;
				a.erase(r,1);
				len-=2;
			}
			else if(a[l]!='#'){
				//l--;
				a.erase(l,1);
                 l--,r--;
				len--;
			}
		}
		 if(s=="delete"){
			if(a[r]!='#'){
				//r++;
				a.erase(r,1);
				len--;
			}
		}
        if(s=="->"){
            if(a[r]!='#'){
                l++;
                r++;
            }
        }
        if(s=="<-"){
            if(a[l]!='#'){
                l--;
                r--;
            }
        }
        
	}
	for(int i=1;i<=l;i++){
		cout<<a[i];
	}
    cout<<"I";
	for(int i=r;i<len-1;i++){
		cout<<a[i];
	}
}
signed main(){
	ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

E小苯的数组构造

每次ai<ai-1,让ai等于ai-1

#include<bits/stdc++.h>
#define int long long
using namespace std; 
const int N=1e6+6;
const int mod=998244353;
int a[N];
int b[N];
int c[N];
int d[N];
void solve() {
	int n;
	cin >> n;
    int mx=0;
    int f=1000000000;
	for(int i=1;i<=n;i++){
		cin >> a[i];
        a[i]+=f;
	}
	for(int i=1;i<=n;i++){
		b[i]=a[i]-a[i-1];
		if(b[i]>=0){
			c[i]=1;
		}
		else{
			d[i]=a[i-1]-a[i];
            mx=max(mx,d[i]);
            a[i]=a[i-1];
		}
	}
//     for(int i=1;i<=n;i++){
// 		cout<<d[i]<<' ';
// 	}
//     cout<<'\n';
    a[n+1]=1ll<<31;
	for(int i=n;i>=1;i--){
		if(c[i]==1){
            if(a[i+1]-a[i]+d[i]<=mx){
                d[i]=a[i+1]-a[i]+d[i];
                a[i]=a[i+1];
            }
            else{
                d[i]=mx;
                a[i]=a[i]+mx-d[i];
            }
		}
	}
	for(int i=1;i<=n;i++){
		cout<<d[i]<<' ';
	}
}
signed main(){
	ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

F小苯的数组切分

猜了一波结论

#include<bits/stdc++.h>
#define int long long
using namespace std; 
const int N=1e6+6;
const int mod=998244353;
int a[N];
int l[N];
int r[N];
int u[N];
void solve() {
	int n;
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> a[i];
	}
	r[n+1]=a[n];
    int mx=0;
    int ans=0;
    int q=1,p=1;
	for(int i=1;i<=n-1;i++){
		l[i]=l[i-1]^a[i];
	}
   // cout<<mx<<'\n';
	for(int i=n;i>2;i--){
		r[i]=r[i+1]&a[i];
        if(r[i]>mx){
            mx=r[i];
            p=i;
        }
	}
    for(int i=p-1;i>=2;i--){
        u[i]=u[i+1]|a[i];
    }
   // cout<<mx<<'\n';
    for(int i=2;i<=p-1;i++){
        ans=max(ans,l[i-1]+u[i]);
    }
	cout<<ans+mx<<'\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小苯的逆序对

莫比乌斯反演,容斥,树状数组,dp(赛时想着欧拉赛bit暴力)遗憾错失ak机会(时间也不太够了

#include<bits/stdc++.h>
#define int long long
using namespace std;
int c[5000005];
int b[5000005];
int pos[5000005];
int mp[5000005];
int dp[5000005];
int n,m;
int ans;
const int N=3e5;
int f[N],cnt=0;
bool vis[N];
struct node{
	int x,w;
}a[5000005];
int lowbit(int x){
	return (x&(-x));
}
void update(int x,int k){
	for(int i=x;i<=N;i+=lowbit(i)){
		c[i]+=k;
	}
}
int find(int l,int r){
    int t=l-1;
	int an=0;
	for(int i=r;i>=1;i-=lowbit(i)){
		an+=c[i];
	}
	for(int i=l-1;i>=1;i-=lowbit(i)){
		an-=c[i];
	}
	return an;
}

bool cmp(node a,node b){
	if(a.w!=b.w)
	return a.w<b.w;
	return a.x<b.x; 
}

void solve() {
	int n;
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> b[i];
		pos[b[i]]=i;
	}
	for(int i=n;i>=1;i--){
		vector<int>tmp;
		for(int j=i;j<=n;j+=i){
			tmp.push_back(pos[j]);
		}
		sort(tmp.begin(),tmp.end());
		int sz=tmp.size();
		for(int i=0;i<=sz+6;i++){
			c[i]=0;
		}
		for(auto k:tmp){
			dp[i]+=find(b[k]/i,sz);
			update(b[k]/i,1);
		}
		for(int k=2*i;k<=n;k+=i){
			dp[i]-=dp[k];
		}
	}
cout<<dp[1]<<'\n';
}
signed main(){
	cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
	int t = 1;
//	cin >> t;
	while(t--){
		solve();
	}
}

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值