• <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>
            隨筆 - 87  文章 - 279  trackbacks - 0
            <2006年10月>
            24252627282930
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            潛心看書研究!

            常用鏈接

            留言簿(19)

            隨筆分類(81)

            文章分類(89)

            相冊

            ACM OJ

            My friends

            搜索

            •  

            積分與排名

            • 積分 - 216468
            • 排名 - 117

            最新評論

            閱讀排行榜

            評論排行榜

            Dominoes
            Time Limit:1000MS? Memory Limit:65536K
            Total Submit:1022 Accepted:333

            Description
            A domino is a flat, thumbsized tile, the face of which is divided into two squares, each left blank or bearing from one to six dots. There is a row of dominoes laid out on a table:


            The number of dots in the top line is 6+1+1+1=9 and the number of dots in the bottom line is 1+5+3+2=11. The gap between the top line and the bottom line is 2. The gap is the absolute value of difference between two sums.

            Each domino can be turned by 180 degrees keeping its face always upwards.

            What is the smallest number of turns needed to minimise the gap between the top line and the bottom line?

            For the figure above it is sufficient to turn the last domino in the row in order to decrease the gap to 0. In this case the answer is 1.
            Write a program that: computes the smallest number of turns needed to minimise the gap between the top line and the bottom line.

            Input
            The first line of the input contains an integer n, 1 <= n <= 1000. This is the number of dominoes laid out on the table.

            Each of the next n lines contains two integers a, b separated by a single space, 0 <= a, b <= 6. The integers a and b written in the line i + 1 of the input file, 1 <= i <= 1000, are the numbers of dots on the i-th domino in the row, respectively, in the top line and in the bottom one.

            Output
            Output the smallest number of turns needed to minimise the gap between the top line and the bottom line.

            Sample Input

            4
            6 1
            1 5
            1 3
            1 2
            

            Sample Output

            1
            

            Source
            CEOI 1997

            #include? < iostream >
            using ? namespace ?std;

            const ? int ?MAXN? = ? 8000 ;
            const ? int ?INF? = ? 1 ? << ? 28 ;

            struct ?DATA? {
            ????
            int ?da[MAXN];
            ????
            int ?dx;
            ????
            int ?q;
            }
            ;

            DATA?dp[
            2 * MAXN];
            bool ?f[ 2 * MAXN];
            int ?queue[MAXN],?front,?rear;
            int ?main()
            {
            ????
            int ?n;
            ????
            int ?a[MAXN],?x,?y;
            ????
            int ?i,?j,?k,?w,?l;
            ????
            int ?d? = ? 0 ;
            ????
            int ?ans? = ?INF;
            ????scanf(
            " %d " ,? & n);
            ????
            for ?(i = 0 ;?i < n;?i ++ )? {
            ????????scanf(
            " %d%d " ,? & x,? & y);
            ????????a[i]?
            = ?x? - ?y;
            ????????d?
            += ?a[i];
            ????}

            ????memset(f,?
            false ,? sizeof (f));
            ????dp[d
            + 7500 ].dx? = ?d;?dp[d + 7500 ].q? = ? 0 ;?f[d + 7500 ]? = ? true ;
            ????
            for ?(i = 0 ;?i < n;?i ++ )?dp[d + 7500 ].da[i]? = ?a[i];
            ????front?
            = ? 0 ;?rear? = ? 0 ;?w? = ? 0 ;
            ????
            do ? {
            ????????
            for ?(i = 0 ;?i < n;?i ++ )? {
            ????????????j?
            = ?dp[d + 7500 ].da[i];
            ????????????k?
            = ?d?? - ?j? * ? 2 ;
            ????????????
            if ?( ! f[k + 7500 ]? || ?dp[k + 7500 ].q? > ?w? + ? 1 )? {
            ????????????????
            if ?(k? == ? 0 )? {
            ????????????????????printf(
            " %d\n " ,?w? + ? 1 );
            ????????????????????system(
            " pause " );
            ????????????????????
            return ? 0 ;
            ????????????????}

            ????????????????f[k
            + 7500 ]? = ? true ;
            ????????????????queue[rear
            ++ ]? = ?k;
            ????????????????dp[k
            + 7500 ].dx? = ?k;
            ????????????????dp[k
            + 7500 ].q? = ?w? + ? 1 ;
            ????????????????
            for ?(l = 0 ;?l < n;?l ++ )?dp[k + 7500 ].da[l]? = ?dp[d + 7500 ].da[l];
            ????????????????dp[k
            + 7500 ].da[i]? = ? - dp[d + 7500 ].da[i];
            ????????????}

            ????????}

            ????????d?
            = ?queue[front ++ ];
            ????????w?
            = ?dp[d + 7500 ].q;
            ????}
            ? while ?(front? <= ?rear);??
            ????j?
            = ? 7500 ;
            ????
            bool ?isFind? = ? false ;
            ????
            for ?(i = 0 ;?i < 7500 ;?i ++ )? {
            ????????
            if ?(f[j + i])? {
            ????????????isFind?
            = ? true ;
            ????????????
            if ?(ans? > ?dp[j + i].q)?ans? = ?dp[j + i].q;
            ????????}

            ????????
            if ?(f[j - i])? {
            ????????????isFind?
            = ? true ;
            ????????????
            if ?(ans? > ?dp[j - i].q)?ans? = ?dp[j - i].q;
            ????????}

            ????????
            if ?(isFind)? break ;
            ????}

            ????printf(
            " %d\n " ,?ans);
            ????system(
            " pause " );
            ????
            return ? 0 ;
            }

            posted on 2006-10-29 20:42 閱讀(801) 評論(0)  編輯 收藏 引用 所屬分類: ACM題目
            国产精品热久久毛片| 久久天天躁狠狠躁夜夜不卡| 日日噜噜夜夜狠狠久久丁香五月| 久久人做人爽一区二区三区| 久久久久人妻一区二区三区| 久久人人爽人人爽人人AV | 精品免费tv久久久久久久| 国内精品久久久久影院日本| 精品欧美一区二区三区久久久| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 久久精品中文字幕有码| 精品久久久久久无码免费| 国产成人精品三上悠亚久久 | 国产成人久久精品区一区二区| 国产农村妇女毛片精品久久| 亚洲综合伊人久久综合| 国产精品成人久久久久三级午夜电影| 久久天天躁狠狠躁夜夜不卡 | 久久久久亚洲精品中文字幕| 欧洲精品久久久av无码电影| 一本大道久久a久久精品综合 | 性高朝久久久久久久久久| 无码国产69精品久久久久网站| 久久精品亚洲欧美日韩久久| 成人免费网站久久久| 久久人妻少妇嫩草AV蜜桃| 国产一区二区精品久久岳| 久久精品国产99国产精品澳门| 久久精品国产清自在天天线| 久久夜色撩人精品国产小说| 国内精品免费久久影院| 国产成人99久久亚洲综合精品 | 精品亚洲综合久久中文字幕| 人妻久久久一区二区三区| 久久久久久久久66精品片| 三级韩国一区久久二区综合| 久久久久免费视频| 香蕉久久永久视频| 2021国内精品久久久久久影院| 久久精品极品盛宴观看| 久久久久久免费视频|