嘉兴网站公司哪家好,ueditor wordpress插件,网站开发及技术路线,做租房网站351E. Jeff and Permutation
题意#xff1a;
一个长度为n的序列#xff0c;你可以选择一些位置#xff0c;使其变成相反数#xff0c;问逆序对最少是多少#xff1f;
题解#xff1a;
对于第i位#xff0c;我们开始考虑他能决定的逆序对#xff1f;对于其他任意位…351E. Jeff and Permutation
题意
一个长度为n的序列你可以选择一些位置使其变成相反数问逆序对最少是多少
题解
对于第i位我们开始考虑他能决定的逆序对对于其他任意位置j只有abs(a[i])abs(a[j])的时候他才会有决定作用 现在我们考虑i的左侧比他绝对值小的数有tot1个右侧有tot2个当i为正时会与右侧的数组成逆序对为负时会与左侧的数组成逆序对所以我们就看tot1和tot2谁小决定了i的正负取值
代码
#include cmath
#include cstdio
#include iostream
using namespace std;const int N 2005;int n, a[N], tot1, tot2, ans;int read() {int x 0, f 1; char s;while((s getchar()) 9 || s 0) if(s -) f -1;while(s 0 s 9) x (x 1) (x 3) (s ^ 48), s getchar();return x * f;
}int main() {n read();for(int i 1; i n; i) a[i] read(), a[i] abs(a[i]);for(int i 1; i n; i) {tot1 tot2 0;for(int j 1; j i; j) if(a[j] a[i]) tot1;for(int j i 1; j n; j) if(a[j] a[i]) tot2;ans min(tot1, tot2);}printf(%d\n, ans);return 0;
}