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

            為生存而奔跑

               :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

            留言簿(5)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            積分與排名

            • 積分 - 326992
            • 排名 - 74

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            SRM 144 DIV1 550

            Problem Statement

                

            In most states, gamblers can choose from a wide variety of different lottery games. The rules of a lottery are defined by two integers (choices and blanks) and two boolean variables (sorted and unique). choices represents the highest valid number that you may use on your lottery ticket. (All integers between 1 and choices, inclusive, are valid and can appear on your ticket.) blanks represents the number of spots on your ticket where numbers can be written.

            The sorted and unique variables indicate restrictions on the tickets you can create. If sorted is set to true, then the numbers on your ticket must be written in non-descending order. If sorted is set to false, then the numbers may be written in any order. Likewise, if unique is set to true, then each number you write on your ticket must be distinct. If unique is set to false, then repeats are allowed.

            Here are some example lottery tickets, where choices = 15 and blanks = 4:

            • {3, 7, 12, 14} -- this ticket is unconditionally valid.
            • {13, 4, 1, 9} -- because the numbers are not in nondescending order, this ticket is valid only if sorted = false.
            • {8, 8, 8, 15} -- because there are repeated numbers, this ticket is valid only if unique = false.
            • {11, 6, 2, 6} -- this ticket is valid only if sorted = false and unique = false.

            Given a list of lotteries and their corresponding rules, return a list of lottery names sorted by how easy they are to win. The probability that you will win a lottery is equal to (1 / (number of valid lottery tickets for that game)). The easiest lottery to win should appear at the front of the list. Ties should be broken alphabetically (see example 1).

            Definition

                
            Class: Lottery
            Method: sortByOdds
            Parameters: vector <string>
            Returns: vector <string>
            Method signature: vector <string> sortByOdds(vector <string> rules)
            (be sure your method is public)
                

            Constraints

            - rules will contain between 0 and 50 elements, inclusive.
            - Each element of rules will contain between 11 and 50 characters, inclusive.
            - Each element of rules will be in the format "<NAME>:_<CHOICES>_<BLANKS>_<SORTED>_<UNIQUE>" (quotes for clarity). The underscore character represents exactly one space. The string will have no leading or trailing spaces.
            - <NAME> will contain between 1 and 40 characters, inclusive, and will consist of only uppercase letters ('A'-'Z') and spaces (' '), with no leading or trailing spaces.
            - <CHOICES> will be an integer between 10 and 100, inclusive, with no leading zeroes.
            - <BLANKS> will be an integer between 1 and 8, inclusive, with no leading zeroes.
            - <SORTED> will be either 'T' (true) or 'F' (false).
            - <UNIQUE> will be either 'T' (true) or 'F' (false).
            - No two elements in rules will have the same name.

            主要是運(yùn)用了排列組合的知識(shí),其中,最難的是,從n個(gè)數(shù)中找出m個(gè)數(shù),要求這m個(gè)數(shù)必須是排好序的,并且可以重復(fù),可以把它轉(zhuǎn)化為從n+m-1個(gè)數(shù)中選出m個(gè)數(shù),且是排好序的,答案就是C(n+m-1,m)
            // BEGIN CUT HERE

            // END CUT HERE
            #line 5 "Lottery.cpp"
            #include 
            <algorithm>
            #include 
            <vector>
            #include 
            <string>
            #include 
            <iostream>
            #include 
            <math.h>
            #include 
            <sstream>
            using namespace std;
            typedef 
            long long int64;
            class TICKET
            {
            public:
                
            string name;
                int64 rate;
            }
            ;
            bool operator < (const TICKET & t1,const TICKET & t2)
            {
                
            if(t1.rate!=t2.rate)
                    
            return t1.rate<t2.rate;
                
            else return t1.name<t2.name;
            }

            vector
            <TICKET>tickets;
            class Lottery
            {
            private:
                
            int choices,blanks;    
                
            bool sorted,unique;

            public:
                vector 
            <string> sortByOdds(vector <string> rules)
                
            {
                    vector
            <string>::iterator it;
                    
            for(it=rules.begin();it!=rules.end();it++)
                    
            {
                        TICKET ticket;
                        ticket.name
            =split(*it);
                        ticket.rate
            =process();
                        tickets.push_back(ticket);
                    }

                    sort(tickets.begin(),tickets.end());
                    vector
            <string> ans;
                    
            for(vector<TICKET>::iterator it=tickets.begin();it!=tickets.end();it++)
                        ans.push_back(it
            ->name);
                    
            return ans;
                }


                
            string split(string rule)
                
            {
                    
            string::size_type pos,pos2;
                    pos 
            = rule.find_first_of(':');
                    
            string name=rule.substr(0,pos);

                    
            char ch1,ch2;
                    istringstream ist(rule.substr(pos
            +2,rule.size()));
                    ist
            >>choices>>blanks>>ch1>>ch2;
                    sorted
            =(ch1=='T'?1:0);
                    unique
            =(ch2=='T'?1:0);
                    
            //pos+=2; //去掉一個(gè)空格

                    
            //pos2 = rule.find_first_of(' ',pos);
                    
            //choices=0;
                    
            //for(string::size_type i=pos;i<pos2;i++)
                    
            //    choices=choices*10+(rule[i]-'0');
                    
            //
                    
            //pos2++;
                    
            //blanks=rule[pos2]-'0';

                    
            //pos2+=2;
                    
            //sorted = (rule[pos2]=='T'?1:0);

                    
            //pos2+=2;
                    
            //unique = (rule[pos2]=='T'?1:0);

                    
            return name;
                }


                int64 process()
                
            {
                    
            if(sorted && unique)
                        
            return C(choices,blanks);
                    
            else if(!sorted && !unique)
                        
            return pow((double)choices,blanks);
                    
            else if(!sorted && unique)
                        
            return A(choices,blanks);
                    
            else
                    
            {
                        
            return C(choices-1+blanks,blanks);
                    }

                }


                int64 A(
            int m,int n)
                
            {
                    int64 res
            =1;
                    
            for(int i=m;i>=m-n+1;i--)
                        res
            *=i;
                    
            return res;
                }


                int64 C(
            int m,int n)
                
            {
                    
            //return A(m,n)/A(n,n);
                    int64 res=1;
                    
            for(int i=m;i>=m-n+1;i--)
                        res
            *=i;
                    
            for(int i=2;i<=n;i++)
                        res
            /=i;
                    
            return res;
                }

            }
            ;
            // BEGIN CUT HERE
            int main()
            {
                Lottery test;
                
            string Arr0[] = {"INDIGO: 93 8 T F",
                                 
            "ORANGE: 29 8 F T",
                                 
            "VIOLET: 76 6 F F",
                                 
            "BLUE: 100 8 T T",
                                 
            "RED: 99 8 T T",
                                 
            "GREEN: 78 6 F T",
                                 
            "YELLOW: 75 6 F F"}
            ;
                vector 
            <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); 
                vector
            <string> ans = test.sortByOdds(Arg0);
                
            for(vector<string>::iterator it=ans.begin();it!=ans.end();it++)
                cout
            <<*it<<endl;
                
            return 0;
            }

            // END CUT HERE

            posted on 2009-08-13 12:41 baby-fly 閱讀(554) 評(píng)論(0)  編輯 收藏 引用 所屬分類: TopCoder

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


            青青草原综合久久大伊人精品| 国产精品免费久久久久电影网| 免费观看成人久久网免费观看| 久久国产亚洲精品| 久久精品无码免费不卡| 久久婷婷久久一区二区三区| 精品久久久久久亚洲精品| 欧美喷潮久久久XXXXx| 色综合久久久久综合体桃花网 | 丰满少妇高潮惨叫久久久| 久久亚洲sm情趣捆绑调教 | 伊人久久大香线蕉综合Av| 香蕉99久久国产综合精品宅男自| 久久久久久久久久免免费精品| 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 久久精品国产72国产精福利| 国产免费久久精品丫丫| 久久久久久毛片免费看| 人妻精品久久久久中文字幕| 狠狠色丁香久久婷婷综合蜜芽五月| 怡红院日本一道日本久久 | 免费精品久久久久久中文字幕| 久久影院久久香蕉国产线看观看| 无码人妻少妇久久中文字幕| 久久强奷乱码老熟女网站| 久久综合九色综合网站| 久久国产热精品波多野结衣AV| 国产精品久久久久天天影视| 丁香久久婷婷国产午夜视频| 亚洲午夜久久久| 久久国产色AV免费看| 久久国产一片免费观看| 少妇人妻综合久久中文字幕| 国产精品无码久久综合| 久久se精品一区二区影院| 三级三级久久三级久久| 久久精品无码专区免费青青| 久久久久人妻一区精品果冻| 无码人妻久久一区二区三区免费 | 超级碰久久免费公开视频| 亚洲午夜精品久久久久久浪潮|