牛客训练营(一)补题

数值膨胀之美

M-数值膨胀之美_2025牛客寒假算法基础集训营1

迭代更新双指针(x、y)的值,从而得出答案。

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
pair<int,int> a[100005];
int b[100005];
int n;
void slove(){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i].first;
		a[i].second=i;
		b[i]=a[i].first;
	}
	a[n].first=2e9+5;
	sort(a,a+n);
	int x=a[0].second,y=a[0].second;
	int m=max(a[0].first*2,a[n-1].first);
	int sum=m-min(a[0].first*2,a[1].first);
	for(int i=1;i<n;i++){
		while(a[i].second<x){
			x--;
			m=max(m,b[x]*2);
		}
		while(a[i].second>y){
			y++;
			m=max(m,b[y]*2);
		}
		sum=min(sum,m-min(a[0].first*2,a[i+1].first));
	}
	cout<<sum<<endl;
}

signed main() {
	ios::sync_with_stdio(false);
    std::cin.tie(0);
    cout.tie(0);
    int T=1;
    //cin>>T;
    while(T--){
    	slove();
	}
}

硝基甲苯之袭

J-硝基甲苯之袭_2025牛客寒假算法基础集训营1

在赛时的时候,想过用传暴力去写,但是一定会超时,所以就没交,赛后看了其他人的代码,其实只需要改动一行就可以。(具体看代码)

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
int a[200005];
int b[200005];
int n;
int sum;
void slove(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		b[a[i]]++;
	}
	for(int i=1;i<=200000;i++){
		for(int j=i;j<=200000;j+=i){//改后的代码每次时+i,而不是+1,会大大提高时间效率
//for(int j=1;j<=200000;j++)这是之前的代码,这样时间复杂度大约在O(1e10)
			int x=i^j;
			if(gcd(x,j)==i&&x<=200000&&j<x){//这个位置就不能是gcd(i,j)而是改成gcd(x,j)
				sum+=b[x]*b[j];
			}
		}
	}
	cout<<sum<<endl;
}
signed main() {
	ios::sync_with_stdio(false);
    std::cin.tie(0);
    cout.tie(0);
    int T=1;
    //cin>>T;
    while(T--){
    	slove();
	}
}
相关推荐
阿巴~阿巴~1 天前
JsonCpp:C++ JSON处理利器
linux·网络·c++·json·tcp·序列化和反序列化
Promise4851 天前
贝尔曼公式的迭代求解笔记
笔记·算法
zmzb01031 天前
C++课后习题训练记录Day38
开发语言·c++
獭.獭.1 天前
C++ -- STL【string的使用】
c++·string·auto
福尔摩斯张1 天前
Linux进程间通信(IPC)机制深度解析与实践指南
linux·运维·服务器·数据结构·c++·算法
你好~每一天1 天前
未来3年,最值得拿下的5个AI证书!
数据结构·人工智能·算法·sqlite·hbase·散列表·模拟退火算法
杰克尼1 天前
3. 分巧克力
java·数据结构·算法
lijiatu100861 天前
C++ 类成员变量声明语法错误
java·开发语言·c++
zore_c1 天前
【C语言】带你层层深入指针——指针详解2
c语言·开发语言·c++·经验分享·笔记
cookies_s_s1 天前
项目--协程库(C++)前置知识篇
linux·服务器·c++