• <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>

            The Fourth Dimension Space

            枯葉北風寒,忽然年以殘,念往昔,語默心酸。二十光陰無一物,韶光賤,寐難安; 不畏形影單,道途阻且慢,哪曲折,如渡飛湍。斬浪劈波酬壯志,同把酒,共言歡! -如夢令

            #

            ACM中java的使用

             

            這里指的java速成,只限于java語法,包括輸入輸出,運算處理,字符串和高精度的處理,進制之間的轉換等,能解決OJ上的一些高精度題目。

            1. 輸入:
            格式為:Scanner cin = new Scanner (new BufferedInputStream(System.in));

            例程:
            import java.io.*;
            import java.math.*;
            import java.util.*;
            import java.text.*;

            public class Main
            {
                public static void main(String[] args)
                {
                    Scanner cin = new Scanner (new BufferedInputStream(System.in));
                    int a; double b; BigInteger c; String st;
                    a = cin.nextInt(); b = cin.nextDouble(); c = cin.nextBigInteger(); d = cin.nextLine(); // 每種類型都有相應的輸入函數.
                }
            }


            2. 輸出
            函數:System.out.print(); System.out.println(); System.out.printf();
            System.out.print(); // cout << …;
            System.out.println(); // cout << … << endl;
            System.out.printf(); // 與C中的printf用法類似.

            例程:
            import java.io.*;
            import java.math.*;
            import java.util.*;
            import java.text.*;

            public class Main
            {
                public static void main(String[] args)
                {
                    Scanner cin = new Scanner (new BufferedInputStream(System.in));
                    int a; double b;
                    a = 12345; b = 1.234567;
                    System.out.println(a + " " + b);
                    System.out.printf("%d %10.5f\n", a, b); // 輸入b為字寬為10,右對齊,保留小數點后5位,四舍五入.
                }
            }

            規格化的輸出:
            函數:
            // 這里0指一位數字,#指除0以外的數字(如果是0,則不顯示),四舍五入.
                DecimalFormat fd = new DecimalFormat("#.00#");
                DecimalFormat gd = new DecimalFormat("0.000");
                System.out.println("x =" + fd.format(x));
                System.out.println("x =" + gd.format(x));


            3. 字符串處理
            java中字符串String是不可以修改的,要修改只能轉換為字符數組.

            例程:
            import java.io.*;
            import java.math.*;
            import java.util.*;
            import java.text.*;

            public class Main
            {
                public static void main(String[] args)
                {
                    int i;
                    Scanner cin = new Scanner (new BufferedInputStream(System.in));
                    String st = "abcdefg";
                    System.out.println(st.charAt(0)); // st.charAt(i)就相當于st[i].
                    char [] ch;
                    ch = st.toCharArray(); // 字符串轉換為字符數組.
                    for (i = 0; i < ch.length; i++) ch[i] += 1;
                    System.out.println(ch); // 輸入為“bcdefgh”.
            if (st.startsWith("a")) // 如果字符串以'0'開頭.
                    {
                        st = st.substring(1); // 則從第1位開始copy(開頭為第0位).
                    }
                }
            }


            4. 高精度
            BigInteger和BigDecimal可以說是acmer選擇java的首要原因。
            函數:add, subtract, divide, mod, compareTo等,其中加減乘除模都要求是BigInteger(BigDecimal)和BigInteger(BigDecimal)之間的運算,所以需要把int(double)類型轉換為BigInteger(BigDecimal),用函數BigInteger.valueOf().

            例程:
            import java.io.*;
            import java.math.*;
            import java.util.*;
            import java.text.*;

            public class Main
            {
                public static void main(String[] args)
                {
                    Scanner cin = new Scanner (new BufferedInputStream(System.in));
                    int a = 123, b = 456, c = 7890;
                    BigInteger x, y, z, ans;
                    x = BigInteger.valueOf(a); y = BigInteger.valueOf(b); z = BigInteger.valueOf(c);
                    ans = x.add(y); System.out.println(ans);
                    ans = z.divide(y); System.out.println(ans);
                    ans = x.mod(z); System.out.println(ans);
                    if (ans.compareTo(x) == 0) System.out.println("1");
                }
            }


            5. 進制轉換
            java很強大的一個功能。
            函數:
            String st = Integer.toString(num, base); // 把num當做10進制的數轉成base進制的st(base <= 35).
            int num = Integer.parseInt(st, base); // 把st當做base進制,轉成10進制的int(parseInt有兩個參數,第一個為要轉的字符串,第二個為說明是什么進制).  
            BigInter m = new BigInteger(st, base); // st是字符串,base是st的進制.

            //Added by abilitytao
            1.如果要將一個大數以2進制形式讀入 可以使用cin.nextBigInteger(2);
            當然也可以使用其他進制方式讀入;
            2.如果要將一個大數轉換成其他進制形式的字符串 使用cin.toString(2);//將它轉換成2進制表示的字符串
            例程:POJ 2305

            import java.io.*;
            import java.util.*;
            import java.math.*;

            public class Main
            {
                
            public static void main(String[] args)
                
            {
                    
            int b;
                    BigInteger p,m,ans;
                    String str ;
                    Scanner cin 
            = new Scanner (new BufferedInputStream(System.in));
                    
            while(cin.hasNext())
                    
            {
                        b
            =cin.nextInt();
                        
            if(b==0)
                            
            break;
                        p
            =cin.nextBigInteger(b);
                        m
            =cin.nextBigInteger(b);
                        ans
            =p.mod(m);
                        str
            =ans.toString(b);
                        System.out.println(str);
                    }

                }

            }

            //End by abilitytao


            6. 排序
            函數:Arrays.sort();至于怎么排序結構體,像C++里寫個cmp的方法,在java還不太清楚,希望有人指點下~~

            例程:
            import java.io.*;
            import java.math.*;
            import java.util.*;
            import java.text.*;

            public class Main
            {
                public static void main(String[] args)
                {
                    Scanner cin = new Scanner (new BufferedInputStream(System.in));
                    int n = cin.nextInt();
                    int a[] = new int [n];
                    for (int i = 0; i < n; i++) a[i] = cin.nextInt();
                    Arrays.sort(a);
                    for (int i = 0; i < n; i++) System.out.print(a[i] + " ");
                }
            }


            7. POJ高精度題目匯總:
            POJ 1131 1205 1220 1405 1503 1604 1894 2084 2305 2325 2389 2413 3101 3199


            轉自:http://hi.baidu.com/czyuan_acm/blog/item/d0bf7a439d90d21b72f05d69.html


            acm中Java的應用

            Chapter I.

            Java的優缺點各種書上都有,這里只說說用Java做ACM-ICPC的特點:

            (1) 最明顯的好處是,學會Java,可以參加Java Challenge    :)
            (2) 對于熟悉C/C++的程序員來說,Java 并不難學,找本書,一兩周業余時間就可以搞定了。當然,這里只是指一般編程,想熟悉所有的Java庫還是需要些時間的。
                  事實上,Java 只相當于C++的一個改進版,所有的語法都幾乎是C++的,很少有變動。
            (3) 在一般比賽中,Java程序會有額外的時間和空間,而實際上經過實驗,在執行計算密集任務的時候Java并不比C/C++慢多少,只是IO操作較慢而已。
            (4) Java 簡單而功能強大,有些東西用Java實現起來更為方便,比如高精度。
            (5) 用Java不易犯細微的錯誤,比如C/C++中的指針, “if (n = m) ... ” 等
            (6) 目前來看Eclipse已成基本配置,寫Java程序反而比C/C++更方便調試。在具體競賽時也算多一種選擇。
            (7) 學會Java對以后工作有好處。現在國外很多地方會Java的人比會C/C++的人多。
            (8) 會Java可以使你看起來更像偶蹄類動物(牛)      hoho~


            Chapter II.

            下面說一下ACM-ICPC隊員初用Java編程所遇到的一些問題:

            1. 基本輸入輸出:

            (1)
            JDK 1.5.0 新增的Scanner類為輸入提供了良好的基礎,簡直就是為ACM-ICPC而設的。

            一般用法為:

            import java.io.*
            import java.util.*

            public class Main
            {
                  public static void main(String args[])
                  {
                      Scanner cin = new Scanner(new BufferedInputStream(System.in));
                      ...
                  }
            }

            當然也可以直接 Scanner cin = new Scanner(System.in);
            只是加Buffer可能會快一些

            (2)
            讀一個整數:    int n = cin.nextInt();          相當于    scanf("%d", &n);    或 cin >> n;
            讀一個字符串:String s = cin.next();          相當于    scanf("%s", s);      或 cin >> s;
            讀一個浮點數:double t = cin.nextDouble();    相當于    scanf("%lf", &t); 或 cin >> t;
            讀一整行:      String s = cin.nextLine();      相當于    gets(s);            或 cin.getline(...);
            判斷是否有下一個輸入可以用 cin.hasNext() 或 cin.hasNextInt() 或 cin.hasNextDouble() 等,具體見 TOJ 1001 例程。

            (3)
            輸出一般可以直接用 System.out.print() 和 System.out.println(),前者不輸出換行,而后者輸出。
            比如:System.out.println(n);    // n 為 int 型
            同一行輸出多個整數可以用
                  System.out.println(new Integer(n).toString() + " " + new Integer(m).toString());

            也可重新定義:

            static PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out));
            cout.println(n);

            (4)
            對于輸出浮點數保留幾位小數的問題,可以使用DecimalFormat類,

            import java.text.*;
            DecimalFormat f = new DecimalFormat("#.00#");
            DecimalFormat g = new DecimalFormat("0.000");
            double a = 123.45678, b = 0.12;
            System.out.println(f.format(a));
            System.out.println(f.format(b));
            System.out.println(g.format(b));

            這里0指一位數字,#指除0以外的數字。


            2. 大數字

            BigInteger 和 BigDecimal 是在java.math包中已有的類,前者表示整數,后者表示浮點數

            用法:
            不能直接用符號如+、-來使用大數字,例如:

            (import java.math.*)    // 需要引入 java.math 包

            BigInteger a = BigInteger.valueOf(100);
            BigInteger b = BigInteger.valueOf(50);
            BigInteger c = a.add(b)    // c = a + b;

            主要有以下方法可以使用:
            BigInteger add(BigInteger other)
            BigInteger subtract(BigInteger other)
            BigInteger multiply(BigInteger other)
            BigInteger divide(BigInteger other)
            BigInteger mod(BigInteger other)
            int compareTo(BigInteger other)
            static BigInteger valueOf(long x)

            輸出大數字時直接使用 System.out.println(a) 即可。


            3. 字符串

            String 類用來存儲字符串,可以用charAt方法來取出其中某一字節,計數從0開始:

            String a = "Hello";      // a.charAt(1) = ’e’

            用substring方法可得到子串,如上例

            System.out.println(a.substring(0, 4))      // output "Hell"

            注意第2個參數位置上的字符不包括進來。這樣做使得 s.substring(a, b) 總是有 b-a個字符。

            字符串連接可以直接用 + 號,如

            String a = "Hello";
            String b = "world";
            System.out.println(a + ", " + b + "!");      // output "Hello, world!"

            如想直接將字符串中的某字節改變,可以使用另外的StringBuffer類。


            4. 調用遞歸(或其他動態方法)

            在主類中 main 方法必須是 public static void 的,在 main 中調用非static類時會有警告信息,
            可以先建立對象,然后通過對象調用方法:

            public class Main
            {
                  ...

                  void dfs(int a)
                  {
                      if (...) return;
                      ...
                      dfs(a+1);
                  }
                 
                  public static void main(String args[])
                  {
                      ...
                      Main e = new Main();
                      e.dfs(0);
                      ...
                  }
            }

            5. 其他注意的事項

            (1) Java 是面向對象的語言,思考方法需要變換一下,里面的函數統稱為方法,不要搞錯。

            (2) Java 里的數組有些變動,多維數組的內部其實都是指針,所以Java不支持fill多維數組。
                  數組定義后必須初始化,如 int[] a = new int[100];

            (3) 布爾類型為 boolean,只有true和false二值,在 if (...) / while (...) 等語句的條件中必須為boolean類型。
                  在C/C++中的 if (n % 2) ... 在Java中無法編譯通過。

            (4) 下面在java.util包里Arrays類的幾個方法可替代C/C++里的memset、qsort/sort 和 bsearch:

            Arrays.fill()
            Arrays.sort()
            Arrays.binarySearch()  

            轉自:http://hi.baidu.com/oak_wesley/blog/item/35839200fd9dc10e1d9583de.html


            Java進制轉換~集錦

            由于Unicode兼容ASCII(0~255),因此,上面得到的Unicode就是ASCII。

            java中進行二進制,八進制,十六進制,十進制間進行相互轉換      
            Integer.toHexString(int i)
            十進制轉成十六進制
            Integer.toOctalString(int i)
            十進制轉成八進制
            Integer.toBinaryString(int i)
            十進制轉成二進制
            Integer.valueOf("FFFF",16).toString()
            十六進制轉成十進制
            Integer.valueOf("876",8).toString()
            八進制轉成十進制
            Integer.valueOf("0101",2).toString()
            二進制轉十進制

            至于轉換成二進制或其他進制,Java API提供了方便函數,你可以查Java的API手冊。
            以字符a的ASCII為例:
            int i = 'a';
            String iBin = Integer.toBinaryString(i);//二進制
            String iHex = Integer.toHexString(i);//十六進制
            String iOct = Integer.toOctalString(i);//八進制
            String iWoKao = Integer.toString(i,3);//三進制或任何你想要的35進制以下的進制
            DEC

            有什么方法可以直接將2,8,16進制直接轉換為10進制的嗎?
            java.lang.Integer類
            parseInt(String s, int radix)
            使用第二個參數指定的基數,將字符串參數解析為有符號的整數。
            examples from jdk:
            parseInt("0", 10) returns 0
            parseInt("473", 10) returns 473
            parseInt("-0", 10) returns 0
            parseInt("-FF", 16) returns -255
            parseInt("1100110", 2) returns 102
            parseInt("2147483647", 10) returns 2147483647
            parseInt("-2147483648", 10) returns -2147483648
            parseInt("2147483648", 10) throws a NumberFormatException
            parseInt("99", 8) throws a NumberFormatException
            parseInt("Kona", 10) throws a NumberFormatException
            parseInt("Kona", 27) returns 411787

            進制轉換如何寫(二,八,十六)不用算法
            Integer.toBinaryString
            Integer.toOctalString
            Integer.toHexString

            例一:

            public class Test{
            public static void main(String args[]){

               int i=100;
               String binStr=Integer.toBinaryString(i);
               String otcStr=Integer.toOctalString(i);
               String hexStr=Integer.toHexString(i);
               System.out.println(binStr);

            例二:

            public class TestStringFormat {
            public static void main(String[] args) {
               if (args.length == 0) {
                  System.out.println("usage: java TestStringFormat <a number>");
                  System.exit(0);
               }

               Integer factor = Integer.valueOf(args[0]);

               String s;

               s = String.format("%d", factor);
               System.out.println(s);
               s = String.format("%x", factor);
               System.out.println(s);
               s = String.format("%o", factor);
               System.out.println(s);
            }
            }


            各種數字類型轉換成字符串型:

            String s = String.valueOf( value); // 其中 value 為任意一種數字類型。

            字符串型轉換成各種數字類型:

            String s = "169";
            byte b = Byte.parseByte( s );
            short t = Short.parseShort( s );
            int i = Integer.parseInt( s );
            long l = Long.parseLong( s );
            Float f = Float.parseFloat( s );
            Double d = Double.parseDouble( s );

            數字類型與數字類對象之間的轉換:

            byte b = 169;
            Byte bo = new Byte( b );
            b = bo.byteValue();

            short t = 169;
            Short to = new Short( t );
            t = to.shortValue();

            int i = 169;
            b = bo.byteValue();

            short t = 169;
            Short to = new Short( t );
            t = to.shortValue();

            int i = 169;
            Integer io = new Integer( i );
            i = io.intValue();

            long l = 169;
            Long lo = new Long( l );
            l = lo.longValue();

            float f = 169f;
            Float fo = new Float( f );
            f = fo.floatValue();

            double d = 169f;
            Double dObj = new Double( d );
            d = dObj.doubleValue();

            posted @ 2009-10-16 19:13 abilitytao 閱讀(1280) | 評論 (4)編輯 收藏

            [ACM必學]文件輸入輸出技巧(freopen函數) (轉)

            昨天發了一篇《C語言 使用文件輸入/輸出數據》,使用的是最普通的文件輸入/輸出方法,Felix大牛隨后給了一種更簡單的改進方法,在ACM中應用很廣,而且超贊,現在來介紹一下。

            這次用到的文件打開函數不再是fopen,而是stdio.h中包含的另一個函數freopen

            FILE * freopen ( const char * filename, const char * mode, FILE * stream );

            【參數說明】

            filename: 要打開的文件名

            mode: 文件打開的模式,和fopen中的模式(r/w)相同

            stream: 文件指針,通常使用標準流文件(stdin/stdout/stderr)

            【使用方法】

            因為文件指針使用的是標準流文件,因此我們可以不定義文件指針。

            接下來我們使用freopen()函數以只讀方式r(read)打開輸入文件slyar.in

            freopen("slyar.in", "r", stdin);

            然后使用freopen()函數以寫入方式w(write)打開輸出文件slyar.out

            freopen("slyar.out", "w", stdout);

            接下來的事情就是使用freopen()函數的優點了,我們不再需要修改scanf和printf,而是維持代碼的原樣就可以了。因為freopen()函數重定向了標準流,使其指向前面指定的文件,省時省力啊,贊...

            最后只要使用fclose關閉輸入文件和輸出文件即可。

            fclose(stdin);
            fclose(stdout);

            若要恢復句柄,可以重新打開標準控制臺設備文件,只是這個設備文件的名字是與操作系統相關的。

            DOS/Win:

            freopen("CON", "r", stdin);

            Linux:

            freopen("/dev/console", "r", stdin);

            也附加一個代碼模版:

            #include <stdio.h>
                         
            int main()
            {
                 freopen(
            "slyar.in""r", stdin);
                 freopen(
            "slyar.out""w", stdout);
                         
                 
            /* 中間按原樣寫代碼,什么都不用修改 */
                         
                 fclose(stdin);
                 fclose(stdout);
                 
            return 0;
            }

            PS.剛才發現一個問題,就是在用C-free編譯含有文件操作的源碼時,必須要將fopen或者freopen放到所有變量定義的下面,否則會編譯錯誤...囧

            轉自:http://www.slyar.com/blog/c-freopen-stdin-stdout.html

            posted @ 2009-10-13 22:43 abilitytao 閱讀(2262) | 評論 (3)編輯 收藏

            Hello ,Java world

            第一個 JAVA程序 ,紀念一下 ^_^

            /**
             * @(#)Welcome.java
             * Coded by abilitytao 
             * 
            @version 1.00 2009/9/30
             
            */


            public class Welcome 
            {
                   
                
            public static void main(String args[])
                
            {
                     String a
            ="Hello , JAVA World";
                     System.out.println(a);
                       
                }

            }


            posted @ 2009-09-30 13:30 abilitytao 閱讀(249) | 評論 (0)編輯 收藏

            POJ 2186 Popular Cows(圖的連通性問題——有向圖的強聯通分量+縮點)

                 摘要: 也許你能寫出一段精致的文字,但你卻未必能寫出一段精辟的代碼。這是我最近研究連通性問題的一個體驗,有的人的書寫了好幾頁紙,可是最終能用的卻只有1,2句話而已,我覺得在計算機的世界,沒有什么比代碼更能直接地表達出這個算法的本質了!所以我以后要多讀代碼,少看那些空洞的文字。言歸正傳,來看題,這是我寫的第一個強連通分量的題目,其實求強連通分量的步驟非常簡單,正反兩次使用dfs,就能得到聯通分量的一切信息。...  閱讀全文

            posted @ 2009-09-26 17:40 abilitytao 閱讀(2290) | 評論 (6)編輯 收藏

            ZOJ 2588-Burning Bridges(圖的連通性問題——求割邊)

                 摘要: 今天想看看關于橋的求法,搜到了這樣一個題,由與網上缺少相關的資料,花了很多時間在找資料上,最后發現lrj的書上有這個問題的描述:無向圖的割點和橋(見lrj書) 算法基于以下原理:(1)如果根頂點的兒子數大于1,則根頂點是割點(2)如果一個點和這個點的子孫都不存在指向這個點祖先的后向邊,則這個點為割點。實現時需要對每個結點記錄一個low值,表示該結點的子孫能夠到達的最小的深度,如果這個值小于或等于該...  閱讀全文

            posted @ 2009-09-26 01:35 abilitytao 閱讀(1505) | 評論 (0)編輯 收藏

            位運算簡介及實用技巧

                 摘要: 位運算簡介(By matrix67)  閱讀全文

            posted @ 2009-09-24 13:22 abilitytao 閱讀(2077) | 評論 (2)編輯 收藏

            內存池技術(轉)

                 摘要: 內存池技術 ACM的只要看前面的一部分就好了^_^  閱讀全文

            posted @ 2009-09-22 00:44 abilitytao 閱讀(596) | 評論 (0)編輯 收藏

            POJ 1330 Nearest Common Ancestors(tarjan LCA 算法)

                 摘要:  關于LCA和RMQ問題 一、最近公共祖先(Least Common Ancestors) 對于有根樹T的兩個結點u、v,最近公共祖先LCA(T,u,v)表示一個結點x,滿足x是u、v的祖先且x的深度盡可能大。另一種理解方式是把T理解為一個無...  閱讀全文

            posted @ 2009-09-21 22:24 abilitytao 閱讀(3546) | 評論 (1)編輯 收藏

            9.20上海東華賽區網絡賽H題 health (Dp+Dfs+Bit Operation)

            Health

             

            Unfortunately YY gets ill, but he does not want to go to hospital. His girlfriend LMY gives him N kinds of medicine, which may be helpful. It is not a good idea to take all of them, since taking several different kinds of medicine may cause undesirable side effects. Formally speaking, for each subset S of the N kinds of medicine (excluding the empty set), it has a health value v(S). If YY chooses to take a combination T of the medicines, the final effect to his illness is the sum of health values of all non-empty subsets of T.

            YY wants to get healthy as quickly as possible, so the final effect of the medicines he takes should be as great as possible. Of course, YY may choose to take nothing, thus having a zero final effect, if he is too unlucky that all other alternatives he can get are negative…

             

            Input

             Input contains multiple test cases.

            For each test case, the first line contains a positive integer N (N16), the number of different kinds of medicine YY received from LMY.

            The second line contains a single integer M (0M2N).

            M lines follow, representing a list of health values.

            Each of the M lines contains 2 integers, s (1s<2N) and v (-10000≤v≤10000), indicating a subset of the N kinds of medicine and its health value. Write s in binary representation and add leading zeros if needed to make it exactly N binary digits. If the ith binary digit of s is 1, then the subset it represents includes the ith kind of medicine; otherwise it does not.

            It is guaranteed that no two lines of the list describe the same subset. All non-empty subsets that do not appear in the list have health value 0.

            Input ends with N=0.

             

            Output

             

            For each test case, output one line with only one integer, the maximum final effect that can be achieved.

             

            Sample Input

            2

            3

            1 10

            2 -1

            3 100

            0

             Sample Output

             109





            比賽的時候,看到這道題過的人很多,但是自己卻沒什么思路,非常郁悶,一看題就知道肯定是個DP,可是究竟怎么動態規劃呢?想了半天也想不出來,一直卡在這個題上,后來有個同學提示了我方法,這才明白過來,原來這里面還有位運算,看來我平時缺少位運算方面的訓練了。。。哎 我還是太水。。。
            分析:這個題的DP思想是把所有1 to 2^n-1的狀態所對應的健康值都算出來,然后再其中取一個最大值。我們看看他是如何狀態轉移的:
                        假設 n=3  現在考慮 1 1 1(7,從左到右分別是第3,2,1種藥品) 這種狀態,如果第三種藥品不使用,那么相當于0 1 1 產生的 總健康值;
                        這個健康值已經由它的子結構得到。
                        如果使用第三種藥品,那么我們進行一次DFS,將他對應的所有的有效狀態(無效狀態對應為0即可)對應的健康值加起來,這樣就得到了
                        使用第三種藥物的總健康值。
                        我們將上述子結構和DFS得到的結果相加,便得到了當前狀態下的總健康值。
            我們做一個循環,i from 1to 2^n-1 ,把所有情況對應的健康值算出來,然后再其中取一個最大值即可。(位運算很重要!)

            #include<iostream>
            using namespace std;
            #define MAX (1<<17)

            int value[MAX];
            int dp[MAX];
            int bin[20];
            int n,m;
            int sum;
            int ans;

            void dfs(int i,int p)
            {

                
            if(i<0)
                
            {
                    sum
            +=value[p];
                    
            return ;
                }

                
            else if(bin[i]==1)
                    dfs(i
            -1,p+(1<<i));
                dfs(i
            -1,p);
            }



            void solve(int n)
            {
                
            int now=( (1<<n)-1 );
                
            int i;
                
            int j=1;
                
            int k=-1;
                
            for(i=1;i<=now;i++)
                
            {
                    sum
            =0;
                    memset(bin,
            0,sizeof(bin));
                    j
            =1;
                    k
            =-1;
                    
            while(j*2<=i)
                    
            {

                        
            if(j&&i)
                        
            {
                            k
            ++;
                            bin[k]
            =1;
                        }

                        
            else 
                        
            {
                            k
            ++;
                            bin[k]
            =0;
                        }

                        j
            *=2;

                    }

                    dfs(k,j);
                    dp[i]
            =dp[i-j]+sum;
                    
            if(dp[i]>ans)
                    ans
            =dp[i];
                }



            }



            int main()
            {
                
            int i;
                
            while(scanf("%d",&n))
                
            {
                    ans
            =0;
                    memset(value,
            0,sizeof(value));
                    
            if(n==0)
                        
            break;
                    scanf(
            "%d",&m);
                    
            for(i=1;i<=m;i++)
                    
            {

                        
            int a,b;
                        scanf(
            "%d%d",&a,&b);
                        value[a]
            =b;
                    }

                    solve(n);
                    printf(
            "%d\n",ans);

                }

                
            return 0;
            }






            做了這個題,突然想起了將10進制轉化成2進制的方法,不斷模除2,取余數,但是一直沒有深究,今天終于明白了,原來如果把這個十進制數考慮成2進制,右移一位相當于除以2,模除2就是把最后那一位給取出來了,不斷的模除2,就把這個2進制數一位一位的取出。
            PS :感謝那位提示我思路的同學

            posted @ 2009-09-21 14:28 abilitytao 閱讀(1303) | 評論 (4)編輯 收藏

            STL map常用操作簡介(轉)

            1。目錄

            map簡介
            map的功能
            使用map
            在map中插入元素
            查找并獲取map中的元素
            從map中刪除元素
            2。map簡介

            map是一類關聯式容器 。它的特點是增加和刪除節點對迭代器的影響很小 ,除了那個操作節點,對其他的節點都沒有什么影響。對于迭代器來說,可以修改實值,而不能修改key。

            3。map的功能

            自動建立Key - value的對應。key 和 value可以是任意你需要的類型。
            根據key值快速查找記錄,查找的復雜度基本是Log(N),如果有1000個記錄,最多查找10次,1,000,000個記錄,最多查找20次。
            快速插入Key - Value 記錄。
            快速刪除記錄
            根據Key 修改value記錄。
            遍歷所有記錄。
            4。使用map

            使用map得包含map類所在的頭文件
            #include <map> //注意,STL頭文件沒有擴展名.h

            map對象是模板類,需要關鍵字和存儲對象兩個模板參數:
            std:map<int, string> personnel;
            這樣就定義了一個用int作為索引,并擁有相關聯的指向string的指針.

            為了使用方便,可以對模板類進行一下類型定義,

            typedef map<int, CString> UDT_MAP_INT_CSTRING;
            UDT_MAP_INT_CSTRING enumMap;

            5。在map中插入元素

            改變map中的條目非常簡單,因為map類已經對[]操作符進行了重載

            enumMap[1] = "One";
            enumMap[2] = "Two";
            .....

            這樣非常直觀,但存在一個性能的問題。插入2時,先在enumMap中查找主鍵為2的項,沒發現,然后將一個新的對象插入enumMap,鍵是2,值是一個空字符串,插入完成后,將字符串賦為"Two" ; 該方法會將每個值都賦為缺省值,然后再賦為顯示的值,如果元素是類對象,則開銷比較大。我們可以用以下方法來避免開銷:

            enumMap.insert (map<int, CString> :: value_type(2, "Two"))

            6。查找并獲取map中的元素

            下標操作符給出了獲得一個值的最簡單方法:

            CString tmp = enumMap[2];

            但是,只有當map中有這個鍵的實例時才對 ,否則會自動插入一個實例,值為初始化值 。

            我們可以使用Find()和Count()方法來發現一個鍵是否存在。

            查找map中是否包含某個關鍵字條目用find() 方法,傳入的參數是要查找的key,在這里需要提到的是begin()和end()兩個成員,分別代表map對象中第一個條目和最后一個條目,這兩個數據的類型是iterator.

            int nFindKey = 2;            //要查找的Key
            //定義一個條目變量(實際是指針)
            UDT_MAP_INT_CSTRING::iterator it= enumMap.find(nFindKey);
            if(it == enumMap.end()) {
                //沒找到
            }
            else {
                //找到
            }

            通過map對象的方法獲取的iterator數據類型是一個std::pair對象,包括兩個數據 iterator->first 和 iterator->second 分別代表關鍵字和存儲的數據

            7。從map中刪除元素

            移除某個map中某個條目用erase()

            該成員方法的定義如下

            iterator erase(iterator it); //通過一個條目對象刪除
            iterator erase(iterator first, iterator last);        //刪除一個范圍
            size_type erase(const Key& key); //通過關鍵字刪除
            clear() 就相當于 enumMap.erase(enumMap.begin(), enumMap.end());

             

            轉自:http://blog.csdn.net/logic_nut/archive/2009/08/30/4498990.aspx

            posted @ 2009-09-17 18:23 abilitytao 閱讀(372) | 評論 (0)編輯 收藏

            僅列出標題
            共42頁: First 24 25 26 27 28 29 30 31 32 Last 
            亚洲欧洲久久久精品| 99久久精品国产一区二区蜜芽| 久久国产精品偷99| 久久九九久精品国产| 久久久久国产一区二区三区| 伊人伊成久久人综合网777| 欧美一区二区三区久久综合 | 一本久久久久久久| 色偷偷88欧美精品久久久| 久久久久免费看成人影片| 久久久噜噜噜久久| 国产精品禁18久久久夂久| 亚洲国产精品无码久久青草| avtt天堂网久久精品| 久久精品国产男包| 久久综合综合久久狠狠狠97色88| 久久亚洲天堂| 狠狠干狠狠久久| 久久久久亚洲av成人网人人软件| 国产美女久久久| 午夜精品久久久久久99热| 色综合久久中文色婷婷| 亚洲国产精品18久久久久久| 亚洲中文字幕伊人久久无码| 久久精品成人免费网站| 欧美一区二区三区久久综| 久久人人爽人人爽人人爽| 精品欧美一区二区三区久久久| 久久成人小视频| 热久久最新网站获取| 伊人丁香狠狠色综合久久| 狠狠色婷婷久久综合频道日韩| 久久综合亚洲色HEZYO国产| 国产亚洲精午夜久久久久久| 国产精品一久久香蕉国产线看| 久久久WWW成人| 久久毛片免费看一区二区三区| 久久九九久精品国产免费直播| 久久青青草原精品国产不卡| 午夜精品久久久久9999高清| 久久人人爽人爽人人爽av|