-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort Perm.cpp
70 lines (68 loc) Β· 1.24 KB
/
Sort Perm.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
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
long long n;
cin>>n;
vector<int>v(n);
for(auto&it:v)
cin>>it;
bool flag=0;
for (int i = 0; i < n - 2; i++) {
if (v[i] > v[i + 1] && v[i + 1] > v[i + 2]) flag = 1;
}
if (flag)
{
cout<<"0\n";
continue;
}
if (is_sorted(v.begin(),v.end()))
{
cout<<n*(n+1)/2<<'\n';
continue;
}
int mx=0;
for(int i=1;i<n;i++)
{
if (v[i]<v[i-1])
mx=max(mx,v[i-1]-v[i]);
}
auto can=[&](int mid)->bool{
vector<int>tmp=v;
for(int i=1;i<n;i++)
{
if (v[i]<v[i-1])
v[i]+=mid;
}
if (is_sorted(v.begin(),v.end()))
{
v=tmp;
return 1;
}
v=tmp;
return 0;
};
if (!can(mx))
{
cout<<0<<'\n';
continue;
}
ll l=mx,r=n,mid,ans=-1;
while(l<=r)
{
mid=l+r>>1;
if (can(mid))
ans=mid,l=mid+1;
else
r=mid-1;
}
auto f=[&](ll x)->ll{
return x*(x+1)/2;
};
cout<<f(ans)-f(mx-1)<<'\n';
}
}