351E. Jeff and Permutation
生活随笔
收集整理的這篇文章主要介紹了
351E. Jeff and Permutation
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
351E. Jeff and Permutation
題意:
一個長度為n的序列,你可以選擇一些位置,使其變成相反數(shù),問逆序?qū)ψ钌偈嵌嗌?#xff1f;
題解:
對于第i位,我們開始考慮他能決定的逆序?qū)?#xff1f;對于其他任意位置j,只有abs(a[i])>abs(a[j])的時候,他才會有決定作用
現(xiàn)在我們考慮i的左側(cè)比他絕對值小的數(shù)有tot1個,右側(cè)有tot2個,當(dāng)i為正時會與右側(cè)的數(shù)組成逆序?qū)?#xff0c;為負(fù)時會與左側(cè)的數(shù)組成逆序?qū)?#xff0c;所以我們就看tot1和tot2誰小,決定了i的正負(fù)取值
代碼:
#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; } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的351E. Jeff and Permutation的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AGAGA XOOORRR CodeFo
- 下一篇: 以家人之命唐灿的扮演者是谁 以家人之名简