零碎知识的备忘录

这篇博客总结了C++编程中的实用技巧和算法应用,包括next_permutation、nth_element函数,二分查找思路,快读,字符串翻转,快速排序模板,幂运算,输入输出优化,以及位运算优化等,结合洛谷例题进行讲解。
摘要由CSDN通过智能技术生成

0)在G++不支持%lf,如果G++提交请用%f,如果C++提交请用%lf 。

1)next_permutation()【#include <algorithm>】

作用:求一个排序的下一个排列的函数,可以遍历全排列。

参考案例:Noip普及组2004 火星人

#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;
}

与之完全相反的函数还有prev_permutation(就是一个求一个排序的上一个排列的函数)

一个不错的文章   http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html

2) elelment【#include<algorithm>】

1.nth_element

作用:这个函数主要用来将数组元素中第k小的整数排出来并在数组中就位,随时调用,可谓十分实用。

#include<bits/stdc++.h>
using namespace std;
long long n,k,a[5000010];
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    nth_element(a+1,a+k+1,a+n);//使第k小整数就位 
    printf("%d",a[k]);//调用第k小整数
}

在这里插入图片描述

你会发现,第3大的数3到了该去的位置,并且在3前面的数都比3小,在3后面的数都比3大 

2.max_element / min_element

#include <iostream>
#include <algorithm>
using namespace std;
int main(void)
{
	int a[6] = {5, 3, 2, 6, 1, 4};
	cout<<max_element(a, a+6) - a<<endl;// 输出为3 
	cout<<*max_element(a, a+6)<<endl;//输出为 6 
	cout<<min_element(a, a+6) - a<<endl;// 输出为4 
	cout<<*min_element(a, a+6)<<endl;	 //输出为1 
	return 0; 
}

3)二分思路<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值