• <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>
            題目+我的解答打包下載
            http://www.shnenglu.com/Files/zuroc/kof_rule.zip變態比賽規則

            為了促進各部門員工的交流,百度舉辦了一場全公司范圍內的“拳皇”(百度內部最流行的格斗游戲)友誼賽,負責組織這場比賽的是百度的超級“拳皇”迷W.Z。W.Z不想用傳統的淘汰賽或者循環賽的方式,而是自己制定了一個比賽規則。

            由于一些員工(比如同部門或者相鄰部門員工)平時接觸的機會比較多,為了促進不同部門之間的交流,W.Z希望員工自由分組。不同組之間的每兩個人都會進行一場友誼賽而同一組內的人之間不會打任何比賽。

            比如4個人,編號為1~4,如果分為兩個組并且1,2一個組,3,4一個組,那么一共需要打四場比賽:1 vs 3,1 vs 4,2 vs 3,2 vs 4。 而如

            果是1,2,3一組,4單獨一組,那么一共需要打三場比賽 1 vs 4,2 vs 4,3 vs 4。


            很快W.Z意識到,這樣的比賽規則可能會讓比賽的場數非常多。W.Z想知道如果有N個人,通過上面這種比賽規則,總比賽場數有可能為K場嗎?

            比如3個人,如果只分到一組則不需要比賽,如果分到兩組則需要2場比賽,如果分為三組則需要3場比賽。但是無論怎么分都不可能恰需要1場比賽。


            相信作為編程高手的你一定知道該怎么回答這個問題了吧? 那么現在請你幫助W.Z吧。


            輸入要求:
            每行為一組數據,包含兩個數字 N, K(0N=500, K=0)。例:
            2 0
            2 1
            3 1
            3 2
            樣例:in.txt



            輸出要求:
            對輸入的N,K 如果N個員工通過一定的分組方式可以使比賽場數恰好為K,則輸出YES,否則輸出NO(請全部使用大寫字母),每組數據占一行。例:
            YES
            YES
            NO
            YES
            樣例:out.txt


            評分規則:
            1.程序將運行在一臺Linux機器上(內存使用不作嚴格限制),在每一測試數據集上運行不能超過10秒,否則該用例不得分;
            2.要求程序能按照輸入樣例的格式讀取數據文件,按照輸出樣例的格式將運行結果輸出到標準輸出上。如果不能正確讀入數據和輸出數據,該題將不得分;
            3.該題目共有3個測試數據集,每個測試數據集為一個輸入文件。各測試數據集占該題目分數的比例分別為30%,30%,40%;
            4.該題目20分。
            /*
            我的思路
            1:
                0
            2:
                0
                1x1+0=1
            3:
                0
                1x2+0=2
                1x2+1=3
            4:
                0
                1x3+0=3
                1x3+2=5
                1x3+3=6
                2x2+0=4    //最小含2
            5:
                1x4+0=4
                1x4+3=7
                1x4+5=9
                1x4+6=10
                2x3+0=6 //最小含2

            算法
                數為N
                    0
                    1x(N-1)+(N-1)可能組合
                    2x(N-2)+(N-2)不含1的所有可能組合
                    3x(N-3)+(N-2)不含1,2的所有可能組合
                    ............................................
                    至
                    N/2+(N-N/2)+(N-2)不含1,2的所有可能組合
            我想的算法啟動速度比較慢,但是用了緩沖,一旦計算過大的計算小的就很快了.
            應該有更好的算法,但是我沒想到...........

            張沈鵬 zsp007@gmail.com
            2007-5-13
            */

            #include <fstream>
            #include <sstream>
            #include <iostream>

            #include <vector>
            #include <map>
            #include <list>

            #include <string>

            #include <algorithm>
            #include <functional>

            using namespace std;

            #define BEG_END(c)        (c.begin()),(c.end())

            typedef unsigned int uint;



            template <class T>
            vector<T>    possible_NO(T x)
            {
                vector<vector<T> >    temp=impl_possible_NO(x);
                vector<T>    res;
                for( vector<vector<T> >::iterator i=temp.begin(),end=temp.end();i!=end;++i){
                    for( vector<T>::iterator j=i->begin(),end=i->end();j!=end;++j){
                        res.push_back(*j);    
                    }
                }
                return res;
            }

            template <class T>
            vector<vector<T> >    impl_possible_NO(T x)
            {
                vector<vector<T> >    res;
                vector<T> temp;

                if (2>x)
                {
                    temp.push_back(0);
                    res.push_back(temp);
                    return res;
                }
                if (2==x)
                {
                    temp.push_back(1);
                    res.push_back(temp);
                    return res;
                }
                static map<T,vector<vector<T>>    > buffer;
                map<T,vector<vector<T> >    >::iterator a=buffer.find(x);
                if (a!= buffer.end() )
                    return a->second;

                for (T i=1;x/2>=i;++i)
                {

                    vector<vector<T> >    N_i=impl_possible_NO(x-i);

                    temp.clear();
                    
                    T base=i*(x-i);
                   
                    temp.push_back(base);

                    if (i<=N_i.size())
                    {
                        for (vector<T>::iterator j=N_i[i-1].begin(),end=N_i[i-1].end();j!=end;++j)
                        {
                            temp.push_back(base+*j);
                        }
                    }
                    res.push_back(temp);
                }

                buffer[x]=res;

                return res;
            }

            template <class T>
            bool possible(T n,T k)
            {
                if(k==0)return true;
                if(k<n-1 || k>n*n-1/2 )return false;

                vector<T> possibleNo=possible_NO(n);
                if(find(possibleNo.begin(),possibleNo.end(),k)!=possibleNo.end())    return true;
               
                return false;
              
            }


            int main()
            {
                string infile_name="in.txt" , outfile_name="out.txt";

                //ofstream outfile(outfile_name.c_str());
                ostream& outfile = cout;

                ifstream infile(infile_name.c_str());
                if (!infile)
                {
                    cerr<<"Error : can't open input file "<<infile_name<<" .\n";
                    return -1;
                }

                string line;

                while (getline(infile,line))
                {
                    uint n,k;

                    istringstream(line)>>n>>k;

                    outfile<< (possible(n,k)? "YES":"NO")  <<'\n';
                }

                return 0;
            }
             



            posted on 2007-05-15 10:54 張沈鵬 閱讀(1442) 評論(3)  編輯 收藏 引用 所屬分類: C++
            Comments
            • # re: 2006百度之星程序設計大賽試題-變態比賽規則(解答)
              sundrop
              Posted @ 2008-05-22 04:37
              按照你的思路寫的程序 好像是動態規劃
              不過程序的輸入輸出和題目要求稍有出入
              程序里用了 set 和 hash_map
              并且緩存結果集以提高性能
              函數返回值用指針以減少拷貝
              可以算到 n=120 的情況
              再打的話 內存就不夠用了
              僅供參考

                回復  更多評論   
            • # re: 2006百度之星程序設計大賽試題-變態比賽規則(解答)
              sundrop
              Posted @ 2008-05-22 04:39
              /*
              * Combat Game
              *
              * n players are divided into g groups.
              * Players in one group do not combat with each other.
              * Every two players from different groups should combat with each other.
              * How many combats there will be between these g groups?
              *
              * n is the total player number, n belong-to N,
              * g is the total group number, 1 <= g <= n.
              *
              * k(n, g) is the combat number if
              * n players are divided into g groups.
              *
              * k(n, g) = K(n, g, 1)
              *
              * K(n, g, r) is the combat number if
              * n players are divided into g groups,
              * and at least, r players should be in each group.
              *
              * K(n, g, r) = {
              * set-item:
              * m*(n-m) + K(n-m, g-1, m)
              * condition:
              * |-> m belong-to N,
              * |-> r <= m <= n/g
              * }
              * K(n, g, r) = { null-set |-> r > [n/g] }
              * K(n, 1, r) = { 0 |-> r <= n }
              */
              vector_ptr calc_k(size_t n, size_t g, size_t r) {
              #ifdef DEBUG
              cout << "\tcalc_k: " << n << ", " << g << ", " << r << endl;
              #endif
              if (r > n/g) {
              return 0;
              }
              vector_ptr v = alloc_vector(n, g, r);
              if (v->size() != 0) {
              return v;
              }
              if (g == 1) {
              PUSH_(*v, 0);
              return v;
              }
              vector_ptr t;
              for (size_t m = r, tm; m <= n/g; ++m) {
              #ifdef DEBUG
              cout << "\tm = " << m << endl;
              #endif
              tm = m*(n-m);
              t = calc_k(n-m, g-1, m);
              if (t != 0) {
              #ifdef DEBUG
              cout << "\t(" << (n-m) << ", " << (g-1) << ", " << m << ") => { ";
              copy(t->begin(), t->end(), ostream_iterator<size_t>(cout, " "));
              cout << " }" << endl;
              #endif
              typedef vector_t::iterator IterT;
              for (IterT it = t->begin(); it != t->end(); ++it) {
              PUSH_(*v, tm + *it);
              }
              } else {
              PUSH_(*v, tm);
              }
              }
              return v;
              }

              /*
              * Read number n, as the total player number.
              * Output combat numbers if
              * these n players is divided into 1, 2, ..., n groups.
              *
              * The program will terminate if read an zero.
              */
              int main() {
              #ifdef DEBUG
              cout << "Program is running in DEBUG mode.\n\n";
              #endif
              #ifdef DEBUG_MEMORY_USAGE
              cout << "Program is running in DEBUG_MEMORY_USAGE flag mode.\n\n";
              #endif
              size_t n;
              vector_ptr v;
              while(true) {
              cout << "Please input total player number (type 0 to quit) : ";
              cin >> n;
              cout << endl;
              if (n == 0) {
              release();
              break;
              }
              for (size_t g = 1; g <= n; ++g) {
              #ifdef DEBUG
              cout << "n = " << n << ", g = " << g << endl;
              #endif
              v = calc_k(n, g, 1);
              if (v != 0) {
              cout << "(" << n << ", " << g << ") => { ";
              #ifndef DEBUG_MEMORY_USAGE
              copy(v->begin(), v->end(), ostream_iterator<size_t>(cout, " "));
              #else
              cout << "set with size = " << v->size();
              #endif
              cout << " }" << endl;
              } else {
              cout << "(" << n << ", " << g << ", " << 1 << ") => null-set" << endl;
              }
              }
              cout << endl;
              }
              system("pause");
              return 0;
              }  回復  更多評論   
            • # re: 2006百度之星程序設計大賽試題-變態比賽規則(解答)
              sundrop
              Posted @ 2008-05-22 04:40
              上個程序 之前的部分

              #include <cstdlib>
              #include <iostream>
              #include <iterator>

              #include <deque>
              #include <set>
              #include <map>
              #include <ext/hash_map>

              using namespace std;
              using namespace __gnu_cxx;

              /*
              * define DEBUG macro to enable debug message
              *
              *
              #define DEBUG
              //*/

              /*
              * define DEBUG_MEMORY_USAGE macro to show memory usage info
              *
              *
              #define DEBUG_MEMORY_USAGE
              //*/

              /*
              * define USE_SET to use std::set as default container
              * undefine USE_SET to use std::deque as default container
              */
              #define USE_SET
              //*/

              #ifdef USE_SET
              typedef set<size_t> vector_t;
              #define PUSH_(CONT, VALUE) (CONT).insert(VALUE)
              #else
              typedef deque<size_t> vector_t;
              #define PUSH_(CONT, VALUE) (CONT).push_back(VALUE)
              #endif

              typedef vector_t * vector_ptr;

              typedef struct key_type {
              size_t n, g, r;
              key_type(size_t pn, size_t pg, size_t pr) :
              n(pn), g(pg), r(pr) {
              }
              bool operator <(key_type const & that) const {
              return (n < that.n && g < that.g && r < that.r);
              }
              bool operator ==(key_type const & that) const {
              return (n == that.n && g == that.g && r == that.r);
              }
              } key_t;

              namespace __gnu_cxx {
              template<> struct hash<key_t>
              { size_t operator()(key_t const & __x) const { return __x.n*10000+__x.g*100+__x.r; } };
              }

              typedef hash_map<key_t,vector_ptr> map_t;

              map_t storage;

              #ifdef DEBUG_MEMORY_USAGE
              size_t created = 0, deleted = 0;
              #endif

              vector_ptr alloc_vector(size_t n, size_t g, size_t r) {
              typedef map_t::iterator mIterT;
              key_t const ky(n, g, r);
              mIterT it = storage.find(ky);
              vector_ptr ptr;
              if (it == storage.end() || !(it->first == ky)) {
              ptr = new vector_t();
              storage.insert(make_pair(ky, ptr));
              #ifdef DEBUG
              cout << "\tcreate (" << n << ", " << g << ", " << r << ")" << endl;
              #endif
              #ifdef DEBUG_MEMORY_USAGE
              created += 1;
              #endif
              } else {
              ptr = it->second;
              }
              return ptr;
              }

              void release() {
              #ifdef DEBUG_MEMORY_USAGE
              map<size_t, size_t> statistic;
              #endif
              typedef map_t::iterator mIterT;
              for (mIterT it = storage.begin(); it != storage.end(); ++it) {
              delete it->second;
              #ifdef DEBUG_MEMORY_USAGE
              deleted += 1;
              if (statistic.find(it->first.n) == statistic.end()) {
              statistic[it->first.n] = 0;
              }
              statistic[it->first.n] += 1;
              #endif
              }
              #ifdef DEBUG_MEMORY_USAGE
              cout << "created " << created << endl;
              for (map<size_t, size_t>::iterator its = statistic.begin();
              its != statistic.end(); ++its) {
              cout << "\tn = " << its->first << ", object number = " << its->second << endl;
              }
              cout << "deleted " << deleted << endl;
              #endif
              }
                回復  更多評論   
             
            久久亚洲精品无码aⅴ大香| 精品国产综合区久久久久久| 久久精品国产免费观看 | 久久夜色精品国产噜噜噜亚洲AV | 欧美激情精品久久久久久久九九九 | 婷婷久久五月天| 国产亚洲精品美女久久久| 91精品国产高清久久久久久国产嫩草 | 91精品国产91久久久久福利| 狠狠色伊人久久精品综合网| 国产精品99久久久精品无码| 青青草国产精品久久久久| 久久人人爽人人爽人人av东京热| 国产精品久久网| 久久午夜福利无码1000合集| 久久综合欧美成人| 久久精品国产亚洲av高清漫画| 久久久久人妻一区精品| 97精品国产91久久久久久| 无码八A片人妻少妇久久| 久久九九全国免费| 亚洲AV成人无码久久精品老人| 久久精品国产99国产精品| 精品无码久久久久久午夜| 思思久久精品在热线热| 久久久久婷婷| 久久免费观看视频| 久久国产精品二国产精品| 99久久亚洲综合精品网站| 久久国产高潮流白浆免费观看| 久久久久人妻一区二区三区| 久久成人永久免费播放| 精品水蜜桃久久久久久久| 国内精品久久久久久久coent| 久久青青草原综合伊人| 亚洲一本综合久久| 天天久久狠狠色综合| 久久最近最新中文字幕大全| 日韩亚洲欧美久久久www综合网 | 久久久久国产亚洲AV麻豆| AA级片免费看视频久久|