牛客周赛38

文章介绍了多个编程题目,涉及整数处理、字符串操作(如去除后缀、检查9的倍数)、等比数列、回文子序列检测、区间删除等,使用了贪心算法、双指针技巧以及数据结构(树状数组)解决复杂问题。
摘要由CSDN通过智能技术生成

A-小红的正整数自增

模拟题直接贴代码了,注意恰好是10的倍数的情况

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
    cin>>n;
	n %= 10;
	if(n==0)cout<<0<<endl;
    else cout<<10-n<<endl;
	return 0;
}

B-小红的抛弃后缀 

小知识点:一个数的是9的倍数的充要条件是这个数各个位数加起来是9的倍数

数字非常大,需要用字符串读入。去掉后缀,保留前缀,逆向思维,我们直接从正向枚举每一个字符。如果余数是0,统计下来

#include<bits/stdc++.h>
using namespace std;

int main()
{
	string s;
	cin >> s;
	int res = 0;
    long long cnt=0;
	for(int i=0;i<s.size();i++)
    {
        res=(res+s[i]-'0')%9;
        if(res==0) cnt++;
    }
    cout<<cnt<<endl;
    return 0;
}

C-

这道题的核心就在k的取值,k是小于字符串的长度的一般,那么我们可以用最简单的aabbcc的构造方式,最多可以构造n/2个,刚好满足条件。剩下的随便补上不相同的字符即可

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n,k;
    cin>>n>>k;
    string ans="";
    
    string a="abc",b="def";
    for(int i=0,j=0;i<k;i++)
    {
        ans+=a[j];
        ans+=a[j];
        j++;
        if(j==3) j=0;
    }
    int t=n-ans.size();
    for(int i=0,j=0;i<t;i++)
    {
        ans+=b[j];
        j++;
        if(j==3)j=0;
    }
    cout<<ans<<endl;
    return 0;
}

D- 小红的平滑值插值

 

贪心思想:如果相邻两个数差的绝对值小于k,那么一定要在中间补上整数,使得这一段相邻两数的差的绝对值小于k,那么贪心的想,这个差值取得最大值k,能够使得使得添加的次数最少。

还有需要注意的地方是,如果是数组的平滑值小于k,那么我们需要操作一次,使得数组中存在一对相邻的数绝对值是k。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int a[N];

int main()
{
	int n, m;
	cin >> n >> m;
    
	for (int i = 0; i < n; i++)  cin >> a[i];

	LL cnt = 0;
    int f=0;//特判是否数组的平滑值小于m

	for (int i = 1; i < n; i++)
	{
        LL t=abs(a[i]-a[i-1]);
        if(t>=m) f=1;
		if (t > m)
		{
			cnt += t / m;
            if(t%m==0) cnt-=1;
		}
	}
    if(!f)cout<<1<<endl;
    else cout << cnt << endl;
	return 0;

}

E-小苯的等比数列

直接枚举公比,很暴力。。 

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int a[N];
int mp[N];
int main()
{
    int n;
    cin >> n;
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        mp[a[i]]++;
        ans = max(ans, mp[a[i]]);
    }

    for (int q = 2; q <= 20; q ++)
    {
        for (int i = 0; i < n; i++)
        {
            int now = a[i];
            int cnt = 0;
            while (now <= 2e5 && mp[now] > 0)
            {
                cnt++;
                now *= q;
            }
            ans = max(ans, cnt);
        }

    }
    
    cout << ans << endl;

    return 0;
}

 

D-小苯的回文询问

 

经过枚举可知,一个数组是否是好数组的充要条件是这个数组是否存在不相邻的两个数相等

用last[i]表示a[i]上次出现的位置,还需要处理相邻的情况,逆序处理last[i],如果last[i]=i-1,就last[i]=lat[last[i]],为什么要逆序呢,举出反例10111。对于询问是否存在子序列是好数组,就是是否存在不相邻的两个数,那么也就是是否存在last[i]是在区间中de,也即last[i]的最大值要大于左端点。区间最大值用线段树去维护。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
#define int long long
unordered_map<int,int>mp;
int a[N];
int last[N];
struct node{
    int l,r,mx;
}tr[N*4];
void pushup(int u)
{
    tr[u].mx=max(tr[u*2].mx,tr[u*2+1].mx);
}
void build(int u,int l,int r)
{
    if(l==r)
    {
        tr[u]={l,r,last[l]};
    }
    else
    {
        tr[u]={l,r};
        int mid=l+r>>1;
        build(u*2,l,mid);
        build(u*2+1,mid+1,r);
        pushup(u);
    }
}
int querry(int u,int l,int r)
{
    if(l<=tr[u].l&&tr[u].r<=r)return tr[u].mx;
    int mid=tr[u].l+tr[u].r>>1;
    int ans=0;
    if(l<=mid) ans=max(ans,querry(u*2,l,r));
    if(r>mid)  ans=max(ans,querry(u*2+1,l,r));
    return ans;
}
signed main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    int n,q;
    cin>>n>>q;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        if(mp.count(a[i]))
        {
            last[i]=mp[a[i]];
        }
        mp[a[i]]=i;
    }
    
    for(int i=n;i;i--)
    {
        if(last[i]==i-1) last[i]=last[last[i]];
    } 
    build(1,1,n);
    //查询是否存在回文序列-->是否存在last i在区间当中--->(last_i)max>=ll
    //查询区间最大值  线段树
    while(q--)
    {
        int ll,rr;
        cin>>ll>>rr;
        if(querry(1,ll,rr)>=ll) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
        
    }
    return 0;
}

G-小红的区间删除

可以发现的是具有单调性的,也即左端点是随着右端点的向右移动而向右移动的,那么我们可以用双指针(滑动窗口)取维护区间。还有就是删除一个数字对数组逆序对个数的影响是,减少了前边比他大的个数+后边比他小的个数,那么这样就可以用树状数组去维护了。还需要用到两个树状数组。具体代码如下。记得m的读入是long long 类型(卡了好久)。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
#define int long long
int tr1[N], tr2[N];
int a[N];
int lowbit(int x)
{
	return x & -x;
}
void add(int tr[], int x, int v)
{
	for (int i = x; i < N; i += lowbit(i))
	{
		tr[i] += v;
	}
}
int sum(int tr[], int x)
{
	int res = 0;
	for (int i = x; i; i -= lowbit(i))
	{
		res += tr[i];
	}
	return  res;
}
signed main()
{
    int n, k;
	cin >> n >> k;
    int total = 0;
    
	for (int i = 1; i<=n ; i++)  scanf("%d",&a[i]);
    
	for(int i=n;i;i--)
    {
        total += sum(tr2,a[i]-1);
		add(tr2, a[i], 1);
    }
    
	LL res = 0;
	if (total >= k) res++; //一个也不删

	for (int r = 1, l = 1; r <= n; r++)
	{
		add(tr2, a[r], -1);

		total -= sum(tr2, a[r] - 1);
		total -= (sum(tr1, N-1)-sum(tr1,a[r]));
        
		while (l <= r && total < k)
		{
			add(tr1, a[l], 1);
			total += sum(tr2, a[l] - 1);
		    total += sum(tr1, N-1)-sum(tr1,a[l]);
            l++;
        }
        res+=r-l+1;
	}
	printf("%lld\n",res);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值