1)next_permutation()【#include <algorithm>】
Function: Find the function of the next permutation of a sort, you can traverse all permutations.
Reference case: Noip Popularity Group 2004 Martian
#include<bits/stdc++.h>
using namespace std;
int a[10005],n,m;
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)scanf("%d",&a[i]);
while(m--) next_permutation(a,a+n);
for(int i=0;i<n-1;i++) printf("%d ",a[i]); printf("%d",a[n-1]);
return 0;
}
The opposite function is prev_permutation (that is, a function that seeks the previous permutation of a sort)
A good article http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html
2)nth_element 【#include<algorithm>】
Function: This function is mainly used to discharge the k-th smallest integer in the array element and place it in the array, which can be called at any time, which is very practical.
(Array name, array name + kth smallest element, array name + number of elements)
Reference case: Luogu P1923 [Deep Base 9. Example 4] Find the k-th smallest number
#include<bits/stdc++.h>
using namespace std;
long long n,k,a[5000010];
int main()
{
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
nth_element(a,a+k,a+n);//使第k小整数就位
printf("%d",a[k]);//调用第k小整数
}
Article recommended https://blog.csdn.net/sugarbliss/article/details/88050145
3) Dichotomous thinking
If the title specifies something with "maximum minimum" or "minimum maximum", then this thing should satisfy the boundedness (obviously) and monotonicity (can be seen) of the binary answer.
Example: Luogu P2440 Wood Processing
#include<iostream>
#include<cstdio>
using namespace std;
int n,k,maxx=0,minn=200000000,ans=0;
int a[50000000];
bool judgee(int x)
{
int alll=0;
for(int i=1;i<=n;i++)
{
alll+=a[i]/x;
}
if(alll<k)return false;
else return true;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(minn>a[i]) minn=a[i];
if(maxx<a[i]) maxx=a[i];
}
int mid,l=1,r=maxx;
while(l<=r)
{
mid=(l+r)/2;
if(judgee(mid))
{
ans=mid;
l=mid+1;
}
else r=mid-1;
}
printf("%d",ans);
return 0;
}
4) Fast reading
int read(){//我喜欢快读
int num = 0;
char c;
bool flag = false;
while ((c = getchar()) == ' ' || c == '\n' || c == '\r');
if (c == '-') flag = true;
else
num = c - '0';
while (isdigit(c = getchar()))
num = num * 10 + c - '0';
return (flag ? -1 : 1) * num;
}
5) reverse flip function
Reverse string function in STL \color{red}\text{reverse()}reverse()
The format is: reverse(reverse( array name .begin().begin() ,, array name .end().end() ););
Luogu P5705 [Deep Base 2. Example 7] Number Reversal
#include<bits/stdc++.h> //文件头
using namespace std;
string a; //定义字符串
int main()
{
cin>>a; //输入
reverse(a.begin(),a.end()); //反转
cout<<a; //输出
return 0; //养成好习惯
}
6) Quick platoon template
void qasort(int l,int r)
{
int i,j,mid,p;
i=l;j=r;
mid=a[(l+r)/2];
do
{
while (a[i]<mid) i++;
while (a[j]>mid) j--;
if(i<=j)
{
p=a[i];a[i]=a[j];a[j]=p;
i++;j--;
}
}while(i<=j);
if(l<j) qasort(l,j);
if(i<r) qasort(i,r);
}
7) Find x to the power of y pow(x, y);
Included in the header file math.h