最大公因数
ll gcd(ll a, ll b)
{
return b == 0 ? a : gcd(b, a % b);
}
查找字符串a是否包含子串b,不是用strA.find(strB) > 0 而是 strA.find(strB) != string:npos
其中string:npos是个特殊值,说明查找没有匹配
long long ksm(long long a,long long b)
{
int ans = 1,base = a;
while(b != 0){
if(b & 1 != 0){// 使用位运算判断奇数,比模运算更快
ans *= base;
}
base *= base;
b >>= 1;// 直接右移一位,比除2块
}
return ans;
}
long long convert(string n, long long radix){
long long base = 1;
long long num = 0;
for(auto lt = n.rbegin(); lt != n.rend(); lt++){
int temp = isdigit(*lt) ? *lt - '0' : *lt - 'a' + 10;
num += temp * base;// 累加低位
base *= radix; // 增大底数
}
return num;
}
随机主元,排序和找第k大
#include<bits/stdc++.h>
int rand_partition(int left,int right,int a[])
{
int random_num=(round(1.0*rand()/RAND_MAX*(right-left)+left));
std::swap(a[left],a[random_num]);
int temp=a[left];
while(left<right)
{
while(left<right&&a[right]>temp) right--;
a[left]=a[right];
while(left<right&&a[left]<=temp) left++;
a[right]=a[left];
}
a[left]=temp;
return left;//主元经过一轮交换后的位置
}
void quick_sort(int left,int right,int a[])
{
if(left<right)
{
int pos=rand_partition(left,right,a);
quick_sort(left,pos-1,a);
quick_sort(pos+1,right,a);
}
}
int find(int left,int right,int a[],int k)
{
if(left==right) return a[left];
int p=rand_partition(left,right,a);
int m=p-left+1;
if(m==k) return a[p];
if(k<m) find(left,p-1,a,k);
else find(p+1,right,a,k-m);//找k减去m个
}
signed main(void)
{
srand((unsigned)time(NULL));
int n1,n2,k;
int a[400005];
scanf("%d",&n1);
for(int i=0;i<n1;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&n2);
for(int i=n1;i<n1+n2;i++)
{
scanf("%d",&a[i]);
}
if((n1+n2)&1) k=(n1+n2)/2+1;
else k=(n1+n2)/2;
std::cout<<find(0,n1+n2-1,a,k);
// std::sort(a,a+n1+n2);
// for(int i=0;i<n1+n2;i++)
// std::cout<<a[i]<<" ";
system("pause");
}
建树
#include<bits/stdc++.h>
int ans=0;
int pre[31];
int post[31];
int in[31];
int data[31];
int get_pre[31];
int n;
struct node
{
int value;
node *lchild,*rchild;
};
node* pre_create(int inl,int inr,int prel,int prer)
{
if(prel>prer) return NULL;
node *temp=new node;
temp->value=pre[prel];
int i;
for(i=inl;i<=inr;i++)
{
if(in[i]==temp->value) break;
}
int numleft=i-inl;
temp->lchild=pre_create(inl,i-1,prel+1,prel+numleft);
temp->rchild=pre_create(i+1,inr,prel+1+numleft+1,prer);
return temp;
}
node* post_create(int inl,int inr,int postl,int postr)
{
if(postl>postr) return NULL;
node *temp=new node;
temp->value=post[postr];
int i;
for(i=inl;i<=inr;i++)
{
if(in[i]==temp->value) break;
}
int numleft=i-inl;
temp->lchild=post_create(inl,i-1,postl,postl+numleft-1);
temp->rchild=post_create(i+1,inr,postl+numleft,postr-1);
return temp;
}
void pre_order(node *root)
{
get_pre[ans++]=root->value;
if(root->lchild!=NULL) pre_order(root->lchild);
if(root->rchild!=NULL) pre_order(root->rchild);
}
inline int read()
{
int w=1,x=0;
char ch=getchar();
while(ch>'9'||ch<'0')
{
if(ch=='-') w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-48,ch=getchar();
return w*x;
}
signed main(void)
{
n=read();
for(int i=1;i<=n;i++) pre[i]=read();
for(int i=1;i<=n;i++) post[i]=read();
for(int i=1;i<=n;i++) in[i]=i;
}