pair的用法 make_pair

pair<string, int> p1(“123”, 99), p2, p3;

p2.first = “abc”, p2.second = 2;

p3 = make_pair(“dce”, 1);

cout << p1.first << ” “ << p1.second;

pair<string, int> 相当于一个类型名称,如果要创建一个这个类型的数组,可以写vector<pair<string, int>>

bitset 位运算

#include <bitset>

0101101010    //一个bitset类型的变量
9876543210  //对应元素的下标

初始化
bitset在定义时,要明确bitset含有多少位,不满左边会自动补0
bitset<5> bit1(“11101”); //最简单的初始化
bitset<n> b; //b有n位,每位都为0
bitset<n> b(u); //b是unsigned long型u的一个副本
bitset<n> b(s); //b是string对象s中含有的位串的副本

查找
bit1.size()//返回二进制总位数
bit1.count()  // 返回bit1里1的个数
bool flag = bit1.any(); // 等价于bit1.count() != 0时返回true
bool flag = bit1.none(); // 等价于bit1.count() == 0时返回true
bool flag = bit1.test(5); // bit[5]等于1的时候返回true,表示这一位被设置了数

操作
bit1.set()//所有位设为1
bit1.set(pos)//下标为pos的位设为1
bit1.reset//所有位设为0
bit1.reset(pos)//下标为pos的位设为0
bit1.flip()//所有位取反
bit1.flip(pos)//pos位取反
bit1.to_ulong()//返回一个unsigned long long 整数

binary_search()、upper_bound()、lower_bound() 二分查找

vector<int> a = {0,1,2,2,3,4}; 使用前提是a已经是升序排列

cout << binary_search(a.begin(), a.end(), 3); // 找是否存在3,return false or true

auto it = upper_bound(a.begin(), a.end(), 2);// 从左到右返回第一个大于2的数字的地址
auto itt = lower_bound(a.begin(), a.end(), 2);// 从左到右返回第一个大于等于2的数字的地址
// 找不到就返回a.end()

sort()、stable_sort()、partial_sort()、nth_element()、greater()、is_sorted()

sort(a, a+5); // 默认从小到大,int数组的排序
sort(v.begin(), v.end()); // vector数组排序
sort中使用的是快排和插排

stable_sort(v.begin(), v.end());

partial_sort(a, a+2, a+n); // 前2个数字将是正确顺序
partial_sort(v.begin(), v.begin() + 3, v.end()); // 对数组中元素部分排序,前3个将是正确顺序

nth_element(v.begin(), v.begin() + 6, v.end()); // 只有v[6]处放置的是正确的元素,其余不管~

sort(v.begin(), v.end(), greater<int>()); // 从大到小排,algorithm头文件

bool flag = is_sorted(a, a+n);
bool flag = is_sort(v.begin(), v.end());

LeetCode 779. K-th Symbol in Grammar

On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.

Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed).

Examples:
Input: N = 1, K = 1
Output: 0

Input: N = 2, K = 1
Output: 0

Input: N = 2, K = 2
Output: 1

Input: N = 4, K = 5
Output: 1

Explanation:
row 1: 0
row 2: 01
row 3: 0110
row 4: 01101001
Note:

N will be an integer in the range [1, 30].
K will be an integer in the range [1, 2^(N-1)].

题目大意:在第一行,我们写一个0.现在在后面的每一行中,我们看前一行,用01代替0出现的每一个,每一次出现1代表10。现在问第N行第K数字是什么数字

分析:用递归,已知N == 1的时候返回0,为了知道第N行第K个数字的值,只要知道它在第N-1行的第(K+1)/2个数字对应的值即可,因为0对应01,1对应10,那么如果K是奇数只需和原数字相同即可,如果K是偶数只需对原对应数字取反即可~

 

LeetCode 643. Maximum Average Subarray I

Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value.

Example 1:
Input: [1,12,-5,-6,50,3], k = 4
Output: 12.75
Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75
Note:
1 <= k <= n <= 30,000.
Elements of the given array will be in the range [-10,000, 10,000].

题目大意:给定一个由n个整数组成的数组,找到具有最大平均值的给定长度k的连续子数组。你需要输出最大的平均值~

分析:sum[i]保存0~i个数字的和,每次将(sum[i]-sum[i-k])的最大值保存在ans中,然后返回ans*1.0/k