-
Notifications
You must be signed in to change notification settings - Fork 26
/
16-16.cpp
149 lines (119 loc) · 3.29 KB
/
16-16.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <bits/stdc++.h>
#include "base.h"
using namespace std;
typedef __int128_t int128;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
typedef long double ld;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
// const int mod = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rrep1(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()
template <class T>
using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T>
using max_queue = priority_queue<T>;
int dir4[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int dir8[8][2] = {{0, 1}, {1, 1}, {1, 0}, {1, -1},
{0, -1}, {-1, -1}, {-1, 0}, {-1, 1}};
template <class T>
void chmin(T& a, T b) {
a = min(a, b);
}
template <class T>
void chmax(T& a, T b) {
a = max(a, b);
}
constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);
const double pi = acos(-1.0), eps = 1e-7;
const int inf = 0x3f3f3f3f, ninf = 0xc0c0c0c0, mod = 1000000007;
const int max3 = 2010, max4 = 20010, max5 = 200010, max6 = 2000010;
/*
unordered_map / unordered_set
lower_bound 大于等于
upper_bound 大于
reserve 预先分配内存
reverse(all(vec)) 反转
sum = accumulate(a.begin(), a.end(), 0ll);
__builtin_popcount 一的个数
vector / array : upper_bound(all(vec), v)
map: m.upper_bound(v)
区间个数: std::distance(v.begin(), it)
map/set distance 复杂度 O(N)
vector/数组 distance 复杂度 O(1)
size_t found=str.find(string/char/char*);
std::string::npos
排序,小于是升序:[](auto&a, auto&b){ return a < b; })
优先队列 priority_queue<Node>:top/pop/push/empty
struct Node {
Node(int t = 0) : t(t) {}
int t;
// 小于是最大堆,大于是最小堆
bool operator<(const Node& that) const { return this->t < that.t; }
};
*/
class Solution {
bool IsSorted(vector<int>& array) {
if (array.empty()) return true;
int pre = array.front();
for (auto v : array) {
if (v < pre) return false;
pre = v;
}
return true;
}
public:
vector<int> subSort(vector<int>& array) {
if (IsSorted(array)) {
return {-1, -1};
}
int n = array.size();
vector<int> preMax(n), subMin(n);
int pre = array.front();
for (int i = 0; i < n; i++) {
preMax[i] = max(array[i], pre);
pre = preMax[i];
}
int sub = array.back();
for (int i = n - 1; i >= 0; i--) {
subMin[i] = min(array[i], sub);
sub = subMin[i];
}
int leftAns = -1, rightAns = -1;
for (int i = 0; i < n; i++) {
if (subMin[i] < array[i]) {
leftAns = i;
break;
}
}
for (int i = n - 1; i >= 0; i--) {
if (preMax[i] > array[i]) {
rightAns = i;
break;
}
}
return {leftAns, rightAns};
}
};
int main() {
// vector<double> ans = {1.00000,-1.00000,3.00000,-1.00000};
// vector<vector<int>> cars = {{1, 2}, {2, 1}, {4, 3}, {7, 2}};
// TEST_SMP1(Solution, getCollisionTimes, ans, cars);
return 0;
}