Topic Portal
1 /*2 greedy: According to 0 or 1 start, if not, select the most recent exchange. Then select the minimum number of exchanges3 */4#include <cstdio>5#include <algorithm>6#include <cstring>7#include <string>8#include <cmath>9#include <vector>Ten#include <map> One#include <queue> A using namespacestd; - - Const intMAXN = A+Ten; the Const intINF =0x3f3f3f3f; - intA[MAXN], B[MAXN], C[MAXN]; - - intMainvoid)//uvalive 6832 Bit String reordering + { - //freopen ("a.in", "R", stdin); + A intN, M; at while(SCANF ("%d%d", &n, &m) = =2) - { - for(intI=1; i<=n; ++i) {scanf ("%d", &a[i]); C[i] =a[i];} - for(intI=1; i<=m; ++i) scanf ("%d", &b[i]); - - intCnt1 =0, Cnt2 =0;intnow =0;intp =0; in BOOLOk1 =true, Ok2 =true; - for(intI=1; I<=m && Ok1; ++i) to { + for(intj=1; j<=b[i]; ++j) - { the if(A[p+j]! =Now ) * { $ intK = p +J;Panax Notoginseng while(k <= n && a[k]! = Now) k++; - if(k = = n+1|| A[K]! = Now) {Ok1 =false; Break;} theCnt1 + = k-(p +j); +Swap (A[k], a[p+j]); A } the } +p + = B[i]; now =1-Now ; - } $ $now =1; p =0; - for(intI=1; I<=m && Ok2; ++i) - { the for(intj=1; j<=b[i]; ++j) - {Wuyi if(C[p+j]! =Now ) the { - intK = p +J; Wu while(k <= n && c[k]! = Now) k++; - if(k = = n+1|| C[K]! = Now) {Ok2 =false; Break;} AboutCnt2 + = k-(p +j); $Swap (c[p+J], C[k]); - } - } -p + = B[i]; now =1-Now ; A } + the //printf ("%d%d\n", Cnt1, Cnt2); - if(!OK1) printf ("%d\n", Cnt2); $ Else if(!OK2) printf ("%d\n", cnt1); the Elseprintf ("%d\n", Min (cnt1, cnt2)); the } the the return 0; -}
Greedy uvalive 6832 Bit String reordering