易錯知識點:
1.sort函數的一般沒有聲明第三部分也就是cmp函數的話是從小到大排序,聲明cmp之后,他會按照對應的cmp規則進行排序比如
int cmp(int x, int t)
{
return x > y;
}
上面的x, y也看也可以是結構體
上面的代碼是按照從大到小的順序排列, 因為只有x > y才會返回true
2.排序區間,sort函數一般是(start, end, cmp)類似這樣的使用方法,st,ed是排序區間,但是他這個區間是左閉右開的,
比如sort(a + 2, a + 6)的話就是從a的第三個元素開始,排序到a的第6個元素,也就是從a[2]排序到a[5]! ! !
3.庫函數
因為sort函數是默認從小到大排序的(less
使用greater
sort(a, a + n, greater<int>());
如果在優先隊列priority_queue中使用greater
小根堆:
priority_queue<int, vector<int>, greater<int>> heap;
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010;
int a[N];
int cmp(int x, int y)
{
return x > y;
}
int main()
{
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i ++ )
a[i] = i + 1;
while(m --)
{
int q, p;
cin >> p >> q;
if(p == 0)
sort(a, a + q, cmp);
else
sort(a + q - 1, a + n);
}
for(int i = 0; i < n; i ++)cout << a[i] << ' ';
return 0;
}