-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
49 lines (42 loc) · 1.03 KB
/
main.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
// Author : Qi Zhang
// Date : 2018-12-11
#include <bits/stdc++.h>
using namespace std;
vector<string> mysplit(string &s, char t){
vector<string> info;
string cur;
for(auto c: s){
if(c == t){
if(cur != "") info.push_back(cur);
cur = "";
}
else cur += c;
}
if(cur != "") info.push_back(cur);
return info;
}
int findUnsortedSubarray(vector<int>& nums) {
int n = nums.size();
int l = n-1, min_num = INT_MAX;
for(int i = n-1; i >= 0; i--){
min_num = min(min_num, nums[i]);
if(nums[i] > min_num) l = i;
}
int r = 0, max_num = INT_MIN;
for(int i = 0; i < n; i++){
max_num = max(max_num, nums[i]);
if(nums[i] < max_num) r = i;
}
return l < r? r-l+1: 0;
}
int main()
{
string line;
while (getline(cin, line)) {
vector<string> tmp = mysplit(line, ',');
vector<int> nums;
for(auto c: tmp) nums.push_back(stoi(c));
cout << findUnsortedSubarray(nums) << endl;
}
return 0;
}