Greedy uvalive 6832 Bit String reordering

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.