0%

可以在set中找到小于某个数的元素的个数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main(){
ordered_set<int> s;
s.insert(1);
s.insert(2);
s.insert(3);
s.insert(4);
cout << s.order_of_key(1) << endl; // the number of elements in the s less than 1
cout << s.order_of_key(2) << endl; // the number of elements in the s less than 2
cout << s.order_of_key(3) << endl; // the number of elements in the s less than 3
cout << s.order_of_key(4) << endl; // the number of elements in the s less than 4
cout << *s.find_by_order(0) << endl; // print the 0-th smallest number in s(0-based)
auto it = s.find_by_order(1);
cout << *it << endl;//3
}
阅读全文 »

code

1
2
3
4
5
6
7
8
#include<memory>
int main() {
std::unique_ptr<int> up,up1;
up = std::unique_ptr<int>(new int(20));
up1 = std::unique_ptr<int>(std::move(up));

std::cout<<*up1<<std::endl; //结果:20
}
阅读全文 »

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//iterative version
for(int mask = 0; mask < (1<<N); ++mask) {
dp[mask][-1] = A[mask];
//handle base case separately (leaf states)
for(int i = 0;i < N; ++i){
if(mask & (1<<i))
dp[mask][i] = dp[mask][i-1] + dp[mask^(1<<i)][i-1];
else
dp[mask][i] = dp[mask][i-1];
}
F[mask] = dp[mask][N-1];
}

//memory optimized, super easy to code.
for(int i = 0; i<(1<<N); ++i)
F[i] = A[i];
for(int i = 0;i < N; ++i) {
for(int mask = 0; mask < (1<<N); ++mask){
if(mask & (1<<i))
F[mask] += F[mask^(1<<i)];
}
}
阅读全文 »