跳到正文

Windtraveller

1765862

这个家伙很懒,什么也没有留下

在洛谷打开
资料、奖项与版本

资料

编号
1765862
抓取

Genshin_Impact.gif

C++
### 一维数组初始化

```cpp
for(int i=0;i<n;i++)
    a[i]=0;
C++
memset(a,0,sizeof(a));
C++
fill(a,a+n,0);

读入优化

快读详解(洛谷)

  • I.I.关闭同步流
C++
cin.tie(0)->sync_with_stdio(0);
cout.tie(0)->sync_with_stdio(0);
  • II.II.getchar() / getchar_unlocked()
C++
int read(){
	int res=0,flag=1;
	char ch;
	ch=getchar(); //ch=getchar_unlocked()
	if(ch=='-')
		flag=-1;
	else if(ch>='0' && ch<='9')
		res=ch-'0';
	while((ch=getchar() /*ch=getchar_unlocked()*/)>='0' && ch<='9')
		res=res*10+(ch-'0');
	return res*flag;
}
  • III.III.fread()
C++
char buf[1<<20],*p1,*p2;
#define gc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,1<<20,stdin)),*p1++)
int read(){
    int k=0,f=1,c=gc();
	for(;!isdigit(c);c=gc())
        if(c=='-')
            f=-1;
	for(;isdigit(c);c=gc())
        k=k*10+(c^48);
	return k*f;
}

三分查找

C++
//凸函数极大值
while(l < r) {
    int lmid=l+(r-l)/3;
    int rmid=r-(r-l)/3;
    if(calc(lmid)>=calc(lmid)) 
        r=rmid-1;
    else 
        l=lmid+1;
}
cout<<max(calc(l),calc(r));

线性筛素数

I.I.埃氏筛 >O(n log log n)O(n \ log \ log \ n)

C++
void sieve(int n){
	memset(f,true,sizeof(f));
	f[0]=f[1]=false;
    for(int i=2;i<=sqrt(n);i++)
    	if(f[i]==true)
    		for(int j=i*i;j<=n;j+=i)
				f[j]=false;
}

II.II.欧拉筛 >O(n)O(n)

C++
void euler(int n){
	memset(f,true,sizeof(f));
	f[0]=f[1]=false;
	for(int i=2;i<=n;i++){
		if(f[i]==true)
            prime[++cnt]=i;
		for(int j=1;j<=cnt && prime[j]<=n/i;j++){
            f[i*prime[j]]=false;
			if(i%prime[j]==0)
                break;
		}
	}
    return;
}

III.III.Miller-Rabin算法

C++
int mul(int a,int b,int mod){ //快速幂取模
    return (__int128)a*b%mod;
}
int pow(int b,int ep,int mod){
    int res=1;
    while(ep>0){
        if(ep&1)
            res=mul(res,b,mod);
        b=mul(b,b,mod);
        ep>>=1;
    }
    return res;
}
bool miller_rabin(int n,int a){ //Miller-Rabin素性测试
    int d=n-1,s=0,x;
    if(n%a==0)
        return false;
    while(d%2==0){
        d/=2;
        s++;
    }
    x=pow(a,d,n);
    if(x==1 || x==n-1)
        return true;
    for(int i=1;i<s;i++){
        x=mul(x,x,n);
        if(x==n-1)
            return true;
    }
    return false;
}
bool check(int n){ //判断是否为质数
    if(n<2) return false;
    else if(n==2 || n==3) return true;
    else if(n%2==0) return false;
    //基于Miller-Rabin算法的测试基
    int test[]={2,3,5,7,11,13,17,19,23,29,31,37};
    for(int a:test){
        if(a>=n)
            break;
        if(!miller_rabin(n,a))
            return false;
    }
    return true;
}

排序(一维数组)

I.I.冒泡排序

C++
for(int i=1;i<n;i++)
    for(int j=1;j<=n-i;j++)
        if(a[j]<a[j+1])
            swap(a[j],a[j+1]);

II.II.选择排序

C++
for(int i=1;i<n;i++){
    int p=i;
    for(int j=i+1;j<=n;j++)
        if(a[j]<a[p])
            p=j;
    swap(a[i],a[p]);
}

III.III.插入排序

C++
///std
for(int i=1;i<n;i++){
    int k=a[i];
    int j=i-1;
    while(j>=0 && a[j]>k){
        a[j+1]=a[j];
        j--;
    }
    a[j+1]=k;
}
C++
//折半插入
void mid_sort(){
    if(n<2)
        return;
    for(int i=1;i!=n;i++){
        int k=a[i];
        auto idx=upper_bound(a,a+i,k)-a;
        memmove(a+idx+1,a+idx,(i-idx)*sizeof(int));
        a[idx]=k;
    }
}

IV.IV.归并排序

C++
void merge(int l,int mid,int r){
	int p1=l,p2=mid+1,pos=l;
	while(p1<=mid && p2<=r){
		if(a[p1]<=a[p2])
			b[pos++]=a[p1++];
		else
			b[pos++]=a[p2++];
    }
	while(p1<=mid)
        b[pos++]=a[p1++];
	while(p2<=r)
        b[pos++]=a[p2++];
	for(int i=l;i<=r;i++)
		a[i]=b[i];
    return;
}	
void merge_sort(int l,int r){
	if(l>=r)
        return;
	int mid=(l+r)/2;
	merge_sort(l,mid);
	merge_sort(mid+1,r);
	merge(l,mid,r);
    return;
}
int main(){
    merge_sort(1,n);
}

V.V.sort >O(n log n)O(n \ log \ n)

C++
sort(a+1,a+1+n/*,cmp*/);

VI.VI.堆排序

C++
//小根堆->从小到大排
int n,x,cnt,a[maxn],hp[maxn];
void hp_push(int x){
	hp[cnt]=x;
	cnt++;
	int t=cnt-1;
	while(t>0){
		if(hp[(t-1)/2]<hp[t])
			break;
		swap(hp[(t-1)/2],hp[t]);
		t=(t-1)/2;
	}
    return;
}
void hp_pop(){
	if(cnt==0){
		cout<<"Empty!\n"; 
		return;
	}
	hp[0]=hp[cnt-1];
	cnt--;
	int t=0;
	while(t*2+1<cnt){
		int p=t*2+1;
		if(p<cnt-1 && hp[p]>hp[p+1])
            p++;
		if(hp[t]<=hp[p])
			break;
		swap(hp[p],hp[t]);
		t=p;
	}
    return;
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>x;
		hp_push(x);
	}
	for(int i=1;i<=n;i++){
		a[i]=hp[0];
		hp_pop();
	}
	return 0;
}

树结构

1.1.定义

C++
void push1(int u,int v,bool f){ //邻接表存储(通用树) 
	vector<int> tree[maxn]; //tree[u]存储节点u的所有子节点/邻居 
	if(f==false){ //无向树
		tree[u].push_back(v);
		tree[v].push_back(u); 
	}
	else //有向树 
		tree[u].push_back(v);
}
void push2(){ // 数组模拟 (仅完全二叉树 )
	int tree[maxn]; // tree[i]存储节点i的值
// 节点i(从1开始)的:
// 左子节点=2*i
// 右子节点=2*i+1
// 父节点=i/2(整除)
// 例如:tree[1]是根,tree[2]和tree[3]是根的左右子节点 
}

2.2.DFS

C++
void per_dfs(int dep){ //前序 根左右 
	if(tree[dep].size()==0) //叶子结点
		return;
	cout<<dep<<" ";
	for(int i=0;i<tree[dep].size();i++)
		per_dfs(tree[dep][i]);
}
void in_dfs(int dep){ //中序 左根右(仅二叉树)
	if(tree[dep].size()==0) //叶子结点
		return;
	if(tree[dep].size()>=1)
		in_dfs(tree[dep][0]);
	cout<<dep<<" ";
	if(tree[dep].size()>=2)
		in_dfs(tree[dep][1]);
}
void post_dfs(int dep){ //后序 左右根
	if(tree[dep].size()==0) //叶子结点
		return;
	for(int i=0;i<tree[dep].size();i++)
		post_dfs(tree[dep][i]); 
	cout<<dep<<" ";
}
void dfs(int dep,int f){ //通用树 
	cout<<dep<<" ";
	for(vector<int>::iterator it=tree[dep].begin();it!=tree[dep].end();it++){
		if(*it==f)
			continue; //不返回父节点
		dfs(*it,dep);
	}
}

堆结构

1.1.手写堆

C++
//小根堆
int cnt,hp[maxn];
bool hp_empty(){ //判空
	return cnt==0;
}
int hp_top(){ //取堆顶(最小值)
	return hp[0];
} 
void hp_push(int x){ //入堆
	hp[cnt]=x;
	cnt++;
	int t=cnt-1;
	while(t>0){
		if(hp[(t-1)/2]<=hp[t])
			break;
		swap(hp[(t-1)/2],hp[t]);
		t=(t-1)/2;
	}
    return;
}
void hp_pop(){ //去堆顶
	if(cnt==0){
		cout<<"Empty!\n"; 
		return;
	}
	hp[0]=hp[cnt-1];
	cnt--;
	int t=0;
	while(t*2+1<cnt){
		int p=t*2+1;
		if(p<cnt-1 && hp[p]>hp[p+1])
            p++;
		if(hp[t]<=hp[p])
			break;
		swap(hp[p],hp[t]);
		t=p;
	}
    return;
}
C++
//大根堆
int cnt,hp[maxn];
bool hp_empty(){ //判空
	return cnt==0;
}
int hp_top(){ //取堆顶(最大值)
	return hp[0];
}
void hp_push(int x){ //入堆
	hp[cnt]=x;
	cnt++;
	int t=cnt-1;
	while(t>0){
		if(hp[(t-1)/2]>=hp[t])
			break;
		swap(hp[(t-1)/2],hp[t]);
		t=(t-1)/2;
	}
    return;
}
void hp_pop(){ //去堆顶
	if(cnt==0){
		cout<<"Empty!\n"; 
		return;
	}
	hp[0]=hp[cnt-1];
	cnt--;
	int t=0;
	while(t*2+1<cnt){
		int p=t*2+1;
		if(p<cnt-1 && hp[p]<hp[p+1])
            p++;
		if(hp[t]>=hp[p])
			break;
		swap(hp[p],hp[t]);
		t=p;
	}
    return;
}

2.2.STL

C++
priority_queue<int> hp1; //大根堆
priority_queue<int,vector<int>,greater<int>> hp2; //小根堆
q.top() //取得堆顶元素,并不会弹出
q.pop() //弹出堆顶元素
q.push() //往堆里面插入一个元素
q.empty() //查询堆是否为空,为空则返回1否则返回0
q.size() //查询堆内元素数量

链表

C++
void init(){ //清空链表 
	tail=cnt=0;
	lis[tail].r=-1;
	mp.clear();
}
void add_tail(int x){ //尾部添加元素x  O(1)
	lis[++cnt]={tail,-1,x};
	lis[tail].r=cnt;
	tail=mp[x]=cnt;
}
void del_tail(){ //删除尾部元素  O(1)
	mp[lis[tail].val]=0;
	lis[lis[tail].l].r=-1;
	tail=cnt;
}
void insert(int k,int x,int opt){
	//opt=1:O(1) 元素k后添加; opt=2:O(k) 第k位后添加
	int nw=0;
	if(opt==1)
		nw=mp[x];
	else
		for(int i=1;i<=k;i++){
			nw=lis[nw].r;
			if(nw==-1)
				return;
		}
	if(nw==tail){
		add_tail(x);
		return;
	}
	lis[++cnt]={nw,lis[nw].r,x};
	lis[lis[nw].r].l=cnt;
	lis[nw].r=mp[x]=cnt;
}
void del(int x){ //删除元素x O(1)
	int nw=mp[x];
	if(nw==tail){
		del_tail();
		return;
	}
	mp[x]=0;
	lis[lis[nw].l].r=lis[nw].r;
	lis[lis[nw].r].l=lis[nw].l;
}
bool check(int x){ //检查链表内有无元素x O(1)
	return (mp[x]!=0);
}
void out(){ //输出链表  O(n)
	if(lis[0].r==-1){
		cout<<"NULL\n";
		return;
	}
	for(int i=lis[0].r;i!=-1;i=lis[i].r)
		cout<<lis[i].val<<" ";
	cout<<"\n";
}

并查集

C++
void init(){
    for(int i=1;i<=n;i++)
        f[i]=i;
    memset(sz,1,sizeof(sz));
}
int find(int u){
    if(u==f[u])
        return u;
    return f[u]=find(f[u]);
}
void merge(int u,int v){
    u=find(u);
    v=find(v);
    if(sz[u]<sz[v]){
        f[u]=v;
        sz[v]+=sz[u];
        return;
    }
    f[v]=u;
    sz[u]+=sz[v];
}
bool check(int u,int v){
    return (find(u)==find(v));
}

求星期几

C++
int dow(int y,int m,int d){
    static int t[]={0,3,2,5,0,3,5,1,4,6,2,4}; 
    y-=m<3; 
    return (y+y/4-y/100+y/400+t[m-1]+d)%7; 
}
共 1 篇
  1. 题解

    题解:P14802 [CCPC 2024 哈尔滨站] 凹包

    本文档为 CCPC 2024 哈尔滨站“凹包”问题的题解。 题目大意:给定二维平面点集,选择若干点连接成面积严格最大的简单凹多边形。 解题思路: 最大凹多边形面积可视为原凸包面积减去一个最小三角形面积(即向内凹陷的部分)。 首先求出所有点的凸包,若无非凸包点则无解。 对剩余的凸包内点再次求凸包。 利用双指针法遍历原凸包的每一条边,在内层凸包中寻找距离该边最近(即形成三角形面积最小)的点。 最终结果为原凸包面积减去计算出的最小面积差值。 代码实现:提供了基于几何算法(凸包、叉积、双指针)的完整 C++ 代码。

    1 赞