POJ 3276 Face the right (common technique-ruler)

Source: Internet
Author: User

"Topic link": Click here~~

"The main idea": N cattle lined up in a row of 1<=n<=5000. Each cow is either forward or backward. In order for all cows to face the front, the farmer can turn the K-head continuous cow to 1<=k<=n each time, the minimum number of operations m and the corresponding minimum k.

"Idea": because the switching interval reversal order has no effect on the results, so from left to right for the need to flip the cow to reverse, while recording the effect of the other cows in the interval cal sum, for the last part of the interval can not be flipped check whether there is a reverse bull, if there is a scheme failure. The thought of this problem deserves careful consideration, and often there is an infinite state, which becomes a finite state.

Code:

/***************poj 3276 (sq. ft.) *face the right way***************/#include <iostream> #include <stdio.h># Include <string.h>using namespace Std;const int n=5005;int t,n,m;int f[n]; Flip state, 1 flip, 0 not flip int dir[n];    Input status f<>0 b<>1char op[2];int calc (int k) {//enum K, flip order from left to right memset (f,0,sizeof (F));   int i,res=0;    Number of consecutive flips int sum=0;            for (i=0; i+k<=n; ++i) {if ((dir[i]+sum) &1) {//flip odd number of times change state, even several times do not change res++;        F[i]=1;  } Sum+=f[i];    Ruler if (i-k+1>=0) sum-=f[i-k+1];        } for (; i<n; ++i) {//Determine if the remainder has a reverse bull if ((dir[i]+sum) &1) return-1;    else if (i-k+1>=0) sum-=f[i-k+1]; } return res;    void Solve () {int k=1,m=n;        for (int i=1; i<=n; ++i) {int M=calc (i);            if (m>0&&m<m) {m=m;        K=i; }} printf ("%d%d\n", k,m);}     int main () {while (scanf ("%d", &n)!=eof) {for (int i=0; i<n; ++i) {scanf ("%s", op);       if (op[0]== ' F ') dir[i]=0;        else dir[i]=1;    } solve (); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3276 Face the right (common technique-ruler)

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.