1,cf Differential Sorting;
1,cf differential sorting
题意:给你n个数的序列,你的操作是选则x<y<z的下标使得ax=ay-az,该操作不能超过n次,
问最终能否将序列变为非递减序列,不用使操作次数最小;
is_sorted()函数用法;
思路:
1,an<an-1,无解,无法操作an;
2,an>=0,一定有解, 将前边的所有元素换为an-1-0,题目不要求严格递增;
3,an<0,1)初始升序,有解,0;否则-1;
因为若中间喷到ai>ai+1,而ai+1之后都是升序且an<0,负数-负数一定增大,所以无解;
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for( int i=(a);i<(n);++i)
#define rep2(i,a,n) for( int i=(a);i<=(n);++i)
#define per1(i,n,a) for( int i=(n);i>(a);i--)
#define per2(i,n,a) for( int i=(n);i>=(a);i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i,b) memset((a),(i),sizeof (b))
#define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define eb emplace_back
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define dbug(y) cout<<(y)<<"\n"
#define dbug2(a,b) cout<<(a)<<" "<<(b)<<"\n"
#define dbug3(a,b,c) cout<<(a)<<" "<<(b)<<" "<<(c)<<"\n"
#define dbug4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<"\n"
#define yi first
#define er second
#define tulun int e[N],ne[N],h[N],w[N],idx;
#define add2(a,b) e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define add3(a,b,c) w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define T_solve() int T;cin>>T;while(T--)solve();
#define pi 3.14159265358979323846
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long long,long long> PLL;
typedef double dob;
const int N=2e5+10;
#define int LL
int a[N];
int n;
void solve()
{
cin>>n;
rep2(i,1,n)cin>>a[i];
if(a[n]<a[n-1])dbug(-1);
else if(a[n]>=0)
{
dbug(n-2);
rep2(i,1,n-2)
{
dbug3(i,n-1,n);
}
}
else
{
if(is_sorted(a+1,a+n+1))dbug(0);
else dbug(-1);
}
}
signed main()
{
quick_cin();
T_solve();
return 0;
}