• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            ZOJ1622 SWITCH解題報(bào)告

            Posted on 2010-09-20 09:31 李東亮 閱讀(330) 評論(0)  編輯 收藏 引用

             

            SWITCH

            題目描述如下:

            There are N lights in a line. Given the states (on/off) of the lights, your task is to determine at least how many lights should be switched (from on to off, or from off to on), in order to make the lights on and off alternatively.
            Input
            One line for each testcase.
            The integer N (1 <= N <= 10000) comes first and is followed by N integers representing the states of the lights ("1" for on and "0" for off).
            Process to the end-of-file.
            Output
            For each testcase output a line consists of only the least times of switches.
            Sample Input
            3 1 1 1
            3 1 0 1
            Sample Output
            1
            0

            分析:該題看似簡單但卻隱藏著陷阱,題目要求尋找的是最少的切換數(shù),故從第二盞燈開始判斷處理得出的結(jié)論是不一定正確的。通過分析可以發(fā)現(xiàn)該題其實(shí)只存在兩種情況:奇數(shù)位置的燈開著或者偶數(shù)位置的燈開著。這樣可以直觀的處理該題:取奇數(shù)位置燈開著需要切換燈狀態(tài)數(shù)與偶數(shù)位置燈開著需切換燈狀態(tài)數(shù)的較小值。這樣的話需要掃描兩邊燈的狀態(tài)數(shù)組,開銷較大。進(jìn)一步分析,設(shè)a為奇數(shù)位置的燈開著需要切換的燈數(shù),b為偶數(shù)位置燈開著需要切換的燈數(shù)。其實(shí)a+b=n。這樣本題就只需要掃描一遍數(shù)組,且進(jìn)一步優(yōu)化后存儲燈狀態(tài)的數(shù)組也可以省了。具體代碼如下:

             

             1#include <stdio.h>
             2#include <stdlib.h>
             3
             4int main(void)
             5{
             6    int n;
             7    int prev;
             8    int tmp;
             9    int cnt;
            10    int a;
            11    while (scanf("%d"&n) == 1)
            12    {
            13        prev = -1;
            14        cnt = 0;
            15        a = n;
            16        while (n--)
            17        {
            18            scanf("%d"&tmp);
            19            if (tmp == prev)
            20            {
            21                if (tmp == 0)
            22                {
            23                    prev = 1;
            24                }

            25                else
            26                {
            27                    prev = 0;
            28                }

            29                ++cnt;
            30                continue;
            31            }

            32            prev = tmp;
            33        }

            34        if (cnt > a/2)
            35            cnt = a-cnt;
            36        printf("%d\n", cnt);
            37    }

            38    return 0;
            39}

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            posts - 12, comments - 1, trackbacks - 0, articles - 1

            Copyright © 李東亮

            欧美亚洲色综久久精品国产| 色综合久久天天综合| 日韩欧美亚洲综合久久| 日日躁夜夜躁狠狠久久AV| 97超级碰碰碰久久久久| 精品久久久久久国产牛牛app| 欧美日韩精品久久久免费观看| 久久人人爽人人爽人人片AV不| 久久狠狠爱亚洲综合影院| 久久超乳爆乳中文字幕| 香港aa三级久久三级老师2021国产三级精品三级在 | 久久精品国产99国产精偷| 9999国产精品欧美久久久久久| 亚洲精品99久久久久中文字幕| 久久久亚洲欧洲日产国码aⅴ| 久久93精品国产91久久综合| 久久久久久久人妻无码中文字幕爆| 免费观看成人久久网免费观看| 精品久久久中文字幕人妻| 久久国产精品免费一区| 狠狠色丁香婷综合久久| 久久久久久精品成人免费图片 | 亚洲国产精品无码久久久秋霞2| 色综合合久久天天综合绕视看| 无码国产69精品久久久久网站| 日本精品久久久久久久久免费| 久久91综合国产91久久精品| 亚洲精品国精品久久99热一| 欧美久久久久久| 久久99国产精品久久99小说| 国产精品九九久久免费视频| 91精品国产综合久久四虎久久无码一级 | 狠狠人妻久久久久久综合| 99久久99这里只有免费费精品| 久久精品亚洲AV久久久无码| 伊人久久精品影院| 久久亚洲日韩看片无码| 亚洲精品乱码久久久久久自慰| 亚洲va国产va天堂va久久| 狠狠色婷婷久久一区二区三区| 久久亚洲精品成人av无码网站|