bzoj 1831
思路:随便猜一猜填的数字是不下降的,反证很好证明,然后就没了。。
#include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PII pair<int, int> #define PLI pair<LL, int> #define ull unsigned long long using namespace std; const int N = 1e4 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; int n, k, a[N]; struct Bit { int a[101]; void modify(int x, int val) { for(int i = x; i <= 100; i += i & -i) a[i] += val; } int sum(int x) { int ans = 0; for(int i = x; i; i -= i & -i) ans += a[i]; return ans; } int query(int l, int r) { if(l > r) return 0; return sum(r) - sum(l-1); } } b1, b2; int main() { scanf("%d%d", &n, &k); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = n; i >= 1; i--) if(~a[i]) b2.modify(a[i], 1); LL ans = 0; for(int i = 1; i <= n; i++) { if(~a[i]) { b2.modify(a[i], -1); b1.modify(a[i], 1); ans += b1.query(a[i]+1, 100); } else { int ret = inf; for(int j = 1; j <= 100; j++) { ret = min(ret, b1.query(j+1, 100) + b2.query(1, j-1)); } ans += ret; } } printf("%lld\n", ans); return 0; } /* */