posts - 403, comments - 310, trackbacks - 0, articles - 7
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          USACO上的簡(jiǎn)單介紹,都快忘了各個(gè)術(shù)語(yǔ)的中文名了
          graph
          vertex 頂點(diǎn) (pl. vertexes / vertices)
          edge
          edge-weighted 帶權(quán)圖(貌似中文是這么叫的吧)
          weight
          self-loop 自環(huán)
          simple graph 簡(jiǎn)單圖,不存在自環(huán)或兩條(及以上)連接相同兩點(diǎn)的邊。multigraph 與之相對(duì)
          degree
          adjacent (to)
          sparse graph 稀疏圖,邊數(shù)少于最大值(n*(n-1)/2)的圖。與之相對(duì)的是dense graph。
          (un)directed graph (有)無(wú)向圖
          out-degree in-degree 有向圖頂點(diǎn)的出度/入度
          path
          cycle 回路

          圖的表示:
          edge list
          adjecency matrix
          adjacency list
          implict

          連通性:
          connected
          component 連通分量
          strongly connected component 強(qiáng)連通分量

          subgraph 子圖. The subgraph of G induced by V' is the graph (V', E')
          bipartite 二分圖
          complete 任意兩點(diǎn)間都有邊

          posted @ 2007-06-03 20:06 ZelluX 閱讀(719) | 評(píng)論 (0)編輯 收藏

          一開(kāi)始什么優(yōu)化都沒(méi)有,過(guò)不了了(記得以前是可以的啊)
          然后用了三個(gè)hash數(shù)組記錄各列、對(duì)角線棋子的放置情況,再加上左右對(duì)稱(chēng)解的優(yōu)化,總算在0.828秒里過(guò)了
          /*
          PROG: checker
          ID: 06301031
          LANG: C++
          */


          #include 
          <iostream>
          #include 
          <fstream>

          using namespace std;

          int g[13];
          bool column[13], diag1[25], diag2[25];
          int n;
          int countResult = 0;
          int mid = 0, firstHalf = 0;
          ifstream fin(
          "checker.in");
          ofstream fout(
          "checker.out");

          void DFS(int t) {
              
          int i;
              
          if (t == n) {
                  countResult
          ++;
                  
          if (countResult <= 3{
                      fout 
          << g[0+ 1;
                      
          for (i = 1; i < n; i++{
                          fout 
          << " " << g[i] + 1;
                      }

                      fout 
          << endl;
                  }

                  
          return;
              }


              
          for (g[t] = 0; g[t] < n; g[t]++{
                  
          if ((countResult > 3&& (t == 0)) {
                      
          if (g[t] == n / 2{
                          firstHalf 
          = countResult;
                          
          if (n % 2 == 0{
                              fout 
          << firstHalf * 2 << endl;
                              exit(
          0);
                          }

                      }

                      
          if ((g[t] == n / 2 + 1&& (n % 2 == 1)) {
                          mid 
          = countResult - firstHalf;
                          fout 
          << firstHalf * 2 + mid << endl;
                          exit(
          0);
                      }

                  }

                  
          if (column[g[t]]) {
                      
          continue;
                  }

                  
          if (diag1[g[t] + t]) {
                      
          continue;
                  }

                  
          if (diag2[g[t] - t + n]) {
                      
          continue;
                  }


                  diag1[g[t] 
          + t] = true;
                  diag2[g[t] 
          - t + n] = true;
                  column[g[t]] 
          = true;
                  DFS(t 
          + 1);
                  diag1[g[t] 
          + t] = false;
                  diag2[g[t] 
          - t + n] = false;
                  column[g[t]] 
          = false;

          nextPosition:
                  ;
              }

          }


          int main() {
              fin 
          >> n;
              
          int i;
              
          for (i = 0; i < n; i++{
                  column[i] 
          = false;
              }

              
          for (i = 0; i < n * 2 -1; i++{
                  diag1[i] 
          = false;
                  diag2[i] 
          = false;
              }

              DFS(
          0);
              fout 
          << countResult << endl;
              fout.close();
              
          return 0;
          }

          posted @ 2007-06-03 19:22 ZelluX 閱讀(602) | 評(píng)論 (0)編輯 收藏

          1. 關(guān)于函數(shù)指針
          The C++ Programming Language上的一段示例代碼:
          map<string, int> histogram;

          void record(const string& s)
          {
          histogram[s]++;
          }

          void print(const pair<const string, int>& r)
          {
          cout << r.first << ' ' << r.second << '\n\;
          }

          int main()
          {
          istream_iterator<string> ii(cin);
          istream_iterator<string> eos;

          for_each(ii, eos, record);
          for_each(histogram.begin(), histogram.end(), print);
          }

          其中record和print是以函數(shù)指針的形式傳遞給for_each的。感覺(jué)這種方法最清晰、直接。
          Java似乎更多的是用接口來(lái)達(dá)到類(lèi)似的效果的,比如Collections.sort(Comparable),通常通過(guò)匿名內(nèi)部類(lèi)來(lái)進(jìn)行自定義元素的比較,從而排序。但是這在語(yǔ)義上已經(jīng)不是函數(shù)了,而是將被排序?qū)ο蠼忉尀閷?shí)現(xiàn)了Comparable接口的對(duì)象。
          另外Java反射機(jī)制中也有Mehod方法,覺(jué)得也可以通過(guò)傳遞Method類(lèi),然后在sort方法中調(diào)用這個(gè)Method的invoke方法,這應(yīng)該更接近函數(shù)指針的語(yǔ)義。但沒(méi)看到過(guò)這樣的實(shí)例。
          C#則引入了委托的概念,通過(guò)delegate關(guān)鍵字聲明該方法。多一個(gè)關(guān)鍵字感覺(jué)就是啰唆了點(diǎn)。 -,-
          現(xiàn)在開(kāi)始對(duì)第一次在Thinking in Java中看到的Callback回調(diào)機(jī)制有了一點(diǎn)感覺(jué)。當(dāng)時(shí)看的時(shí)候很難理解。
          看來(lái)在學(xué)習(xí)某一門(mén)語(yǔ)言的時(shí)候有一定其他語(yǔ)言的背景進(jìn)行比較,很容易加深理解。

          2. 使用地址傳遞結(jié)構(gòu),減少開(kāi)銷(xiāo)
          學(xué)C++最不適應(yīng)的就是指針的應(yīng)用,因?yàn)闆](méi)有C的基礎(chǔ),盡管高中競(jìng)賽用的是Pascal,也用指針實(shí)現(xiàn)了trie、圖的鏈?zhǔn)奖硎镜缺容^復(fù)雜的數(shù)據(jù)結(jié)構(gòu),但是也沒(méi)有像C++這樣指針穿插在整個(gè)程序中的,當(dāng)然C更多。
          C++傳遞結(jié)構(gòu)時(shí)默認(rèn)是先復(fù)制一份拷貝,然后函數(shù)操作的是該拷貝,而不是Java中的傳遞引用(當(dāng)然Java沒(méi)有結(jié)構(gòu)這一類(lèi)型)。
          C++ Primer Plus中指出要
          1) 調(diào)用函數(shù)時(shí)傳遞地址&myStruct
          2) 形參聲明為指針MyStruct *
          3) 訪問(wèn)成員使用操作符 ->

          3. 將引用用于結(jié)構(gòu)
          同樣,為了節(jié)省時(shí)空開(kāi)銷(xiāo),在函數(shù)返回值時(shí)盡量使用引用。
          const MyStruct & use(MyStruct & mystruct)
          注意最好將返回的引用聲明為const,否則可以使用這樣的代碼:
          use(myStruct).used = 10;
          容易產(chǎn)生語(yǔ)義混亂。
          但在某些時(shí)候必須去掉const關(guān)鍵字。

          posted @ 2007-06-03 18:58 ZelluX 閱讀(372) | 評(píng)論 (0)編輯 收藏

          算法:
          進(jìn)入大學(xué)后,對(duì)算法這些基礎(chǔ)的看法改變了好幾次。
          高中里認(rèn)為搞ACM沒(méi)什么意義,覺(jué)得和應(yīng)試差不多。
          但是大學(xué)里有意無(wú)意的一些接觸后,發(fā)現(xiàn)它涉及了大量應(yīng)用數(shù)學(xué)的知識(shí),對(duì)基礎(chǔ)的提高十分有用。
          保送的時(shí)候志愿填了數(shù)學(xué)系,不就是為了打好數(shù)學(xué)基礎(chǔ)嗎?
          從功利的角度講,有ACM的經(jīng)歷以后進(jìn)IT公司自然可以方便不少。

          英語(yǔ):
          從小學(xué)到高中,英語(yǔ)一直是強(qiáng)項(xiàng),考試不需要突擊,課本單詞不需要刻意背誦,語(yǔ)言點(diǎn)很少?gòu)?fù)習(xí),高中的英語(yǔ)考試讓我對(duì)自己的英語(yǔ)過(guò)于自信了。
          然后進(jìn)了大學(xué),處處受到強(qiáng)人的打擊,上學(xué)期拿了B+覺(jué)得已經(jīng)不錯(cuò)了。到現(xiàn)在這個(gè)英語(yǔ)班已經(jīng)成為中等偏下的學(xué)生了,無(wú)論是詞匯量還是聽(tīng)說(shuō)能力。
          sigh,暑假要報(bào)個(gè)補(bǔ)習(xí)班了。

          至于其他的應(yīng)用,asp.net ruby 之類(lèi)的東西,權(quán)且當(dāng)玩具學(xué)學(xué)吧,等需要應(yīng)用的時(shí)候再好好看未必來(lái)不及,關(guān)鍵要有扎實(shí)的基礎(chǔ)。

          希望這個(gè)想法在放假前不會(huì)改變。

          posted @ 2007-06-03 18:28 ZelluX 閱讀(341) | 評(píng)論 (0)編輯 收藏

          開(kāi)始的方法太慢了,Superprime要從一位數(shù)開(kāi)始生成,逐漸增加位數(shù)。


          /*
          PROG: sprime
          ID: 06301031
          LANG: C++
          */


          #include 
          <iostream>
          #include 
          <fstream>
          #include 
          <cmath>
          #include 
          <vector>
          #include 
          <algorithm>

          using namespace std;

          bool isPrime(long n) {
              
          if (n == 1{
                  
          return false;
              }

              
          for (int i = 2; i <= sqrt(n); i++{
                  
          if (n % i == 0{
                      
          return false;
                  }

              }

              
          return true;
          }


          int main() {
              ifstream fin(
          "sprime.in");
              ofstream fout(
          "sprime.out");
              
          int n;
              fin 
          >> n;
              
          long i, j;
              
          long base = 1;
              vector
          <long> prev, now;
              vector
          <long>::iterator iter;
              prev.push_back(
          0);
              
          for (i = 1; i <= n; i++{
                  
          for (j = 1; j <= 9; j++{

                      
          for (iter = prev.begin(); iter != prev.end(); iter++{
                          
          long number = *iter * 10 + j;
                          
          if (isPrime(number)) {
                              now.push_back(number);
                          }

                      }

                  }

                  prev 
          = now;
                  now.clear();
                  
          base *= 10;
              }


              sort(prev.begin(), prev.end());
              
          for (iter = prev.begin(); iter != prev.end(); iter++{
                  fout 
          << *iter << endl;
              }

          }

          posted @ 2007-06-01 22:32 ZelluX 閱讀(256) | 評(píng)論 (0)編輯 收藏

          最簡(jiǎn)單的DP,于是只用了一維數(shù)組稍微提高下難度,不過(guò)還是一次編譯一次提交成功了,恩

          /*
          PROG: numtri
          ID: 06301031
          LANG: C++
          */


          #include 
          <iostream>
          #include 
          <fstream>

          using namespace std;

          int main() {
              ifstream fin(
          "numtri.in");
              ofstream fout(
          "numtri.out");
              
          int n;
              fin 
          >> n;
              
          int i, j;
              
          int f[1001];
              
          for (i = 0; i <= n; i++{
                  f[i] 
          = 0;
              }

              
          for (i = 0; i < n; i++{
                  
          for (j = 0; j <= i; j++{
                      
          int x;
                      fin 
          >> x;
                      f[j] 
          += x;
                  }

                  
          int temp[1001];
                  
          for (j = 0; j <= i; j++{
                      temp[j] 
          = f[j];
                  }

                  temp[i 
          + 1= f[i];
                  
          for (j = 0; j <= i; j++{
                      
          if (f[j] > temp[j + 1]) {
                          temp[j 
          + 1= f[j];
                      }

                  }

                  
          for (j = 0; j <= i + 1; j++{
                      f[j] 
          = temp[j];
                  }

              }


              
          int max = 0;
              
          for (i = 0; i <= n; i++{
                  
          if (f[i] > max) {
                      max 
          = f[i];
                  }

              }

              fout 
          << max << endl;
              
          return 0;
          }

          posted @ 2007-06-01 21:51 ZelluX 閱讀(266) | 評(píng)論 (0)編輯 收藏

          Packing Rectangles先cheat了,下星期再回來(lái)做。

          先用篩法做了一張hash表,記錄是否為素?cái)?shù),然后找各個(gè)素?cái)?shù)判斷是否為回文數(shù),超內(nèi)存了。。。
          于是改為生成回文數(shù)后判斷是否為素?cái)?shù),過(guò)了。
          貌似現(xiàn)在寫(xiě)這種程序的速度比高中快不少了,到底是什么進(jìn)步了呢?
          /*
          PROG: pprime
          ID: 060301031
          LANG: C++
          */


          #include 
          <iostream>
          #include 
          <fstream>
          #include 
          <bitset>
          #include 
          <cmath>

          using namespace std;

          bool isPrime(const long num) {
              
          int i;
              
          for (i = 2; i <= sqrt(num); i++{
                  
          if (num % i == 0{
                      
          return false;
                  }

              }

              
          return true;
          }


          int main() {
              ifstream fin(
          "pprime.in");
              ofstream fout(
          "pprime.out");
              
          long from, to;
              
          long i = 0, j;
              fin 
          >> from >> to;
              
              
          long beginNum = 1;
              
          while (true{
                  
          int number;
                  i
          ++;  // i indicates the digits of palindromes to be generated
                  for (j = beginNum; j < beginNum * 10; j++{
                      
          long num1 = j, num2 = 0, temp = j;
                      
          if (i % 2 == 1{
                          temp 
          /= 10;
                      }

                      
          while (temp > 0{
                          num1 
          *= 10;
                          num2 
          = num2 * 10 + (temp % 10);
                          temp 
          /= 10;
                      }

                      number 
          = num1 + num2;
                      
          if (number > to) {
                          
          break;
                      }

                      
          if (number < from) {
                          
          continue;
                      }

                      
          if (isPrime(number)) {
                          fout 
          << number << endl;
                      }

                  }

                  
          if (number > to) {
                      
          break;
                  }

                  
          if (i % 2 == 0{
                      beginNum 
          *= 10;
                  }

              }

              
          return 0;
          }

          posted @ 2007-06-01 21:28 ZelluX 閱讀(463) | 評(píng)論 (0)編輯 收藏

          使用SQL登錄驗(yàn)證:

          1. 在SQL Server Management Studio Express中右擊數(shù)據(jù)庫(kù),Properties -> Security,Server authentication選為SQL Server and Windows Authentication mode

          2. 返回主界面,在Security的Login中增加用戶,分配權(quán)限。

          posted @ 2007-05-31 21:49 ZelluX 閱讀(371) | 評(píng)論 (0)編輯 收藏

          BFS,一次通過(guò)
          /*
          PROG: milk3
          ID: 06301031
          LANG: C++
          */


          #include 
          <iostream>
          #include 
          <fstream>
          #include 
          <deque>
          #include 
          <set>

          using namespace std;

          struct Status {
              
          int a;
              
          int b;
              
          int c;
          }
          ;

          void pour(const int from, const int to, const int maxTo, int& newFrom, int& newTo) {
              newTo 
          = from + to;
              newFrom 
          = 0;
              
          if (newTo > maxTo) {
                  newFrom 
          = newTo - maxTo;
                  newTo 
          = maxTo;
              }

          }


          int main() {
              
          int i, j, k;
              ifstream fin(
          "milk3.in");
              ofstream fout(
          "milk3.out");
              Status begin;
              
          int maxA, maxB, maxC;
              fin 
          >> maxA >> maxB >> maxC;
              begin.a 
          = 0;
              begin.b 
          = 0;
              begin.c 
          = maxC;
              
              
          bool hash[21][21][21];
              
          for (i = 0; i <= maxA; i++{
                  
          for (j = 0; j <= maxB; j++{
                      
          for (k = 0; k <= maxC; k++{
                          hash[i][j][k] 
          = false;
                      }

                  }

              }

              hash[
          0][0][maxC] = true;
              
          set<int> result;

              deque
          <Status> queue;
              queue.push_back(begin);
              
          while (!queue.empty()) {
                  deque
          <Status>::iterator now = queue.begin();
                  
          if (now->== 0{
                      result.insert(now
          ->c);
                  }

                  Status newStat;
                  newStat 
          = *now;
                  pour(now
          ->a, now->b, maxB, newStat.a, newStat.b);
                  
          if (!hash[newStat.a][newStat.b][newStat.c]) {
                      hash[newStat.a][newStat.b][newStat.c] 
          = true;   
                      queue.push_back(newStat);
                  }

                  newStat 
          = *now;
                  pour(now
          ->b, now->a, maxA, newStat.b, newStat.a);
                  
          if (!hash[newStat.a][newStat.b][newStat.c]) {
                      hash[newStat.a][newStat.b][newStat.c] 
          = true;
                      queue.push_back(newStat);
                  }

                  newStat 
          = *now;
                  pour(now
          ->c, now->a, maxA, newStat.c, newStat.a);
                  
          if (!hash[newStat.a][newStat.b][newStat.c]) {
                      hash[newStat.a][newStat.b][newStat.c] 
          = true;
                      queue.push_back(newStat);
                  }

                  newStat 
          = *now;
                  pour(now
          ->a, now->c, maxC, newStat.a, newStat.c);
                  
          if (!hash[newStat.a][newStat.b][newStat.c]) {
                      hash[newStat.a][newStat.b][newStat.c] 
          = true;
                      queue.push_back(newStat);
                  }

                  newStat 
          = *now;
                  pour(now
          ->b, now->c, maxC, newStat.b, newStat.c);
                  
          if (!hash[newStat.a][newStat.b][newStat.c]) {
                      hash[newStat.a][newStat.b][newStat.c] 
          = true;
                      queue.push_back(newStat);
                  }

                  newStat 
          = *now;
                  pour(now
          ->c, now->b, maxB, newStat.c, newStat.b);
                  
          if (!hash[newStat.a][newStat.b][newStat.c]) {
                      hash[newStat.a][newStat.b][newStat.c] 
          = true;
                      queue.push_back(newStat);
                  }


                  queue.pop_front();
              }


              
          set<int>::iterator iter = result.begin();
              fout 
          << *iter;
              iter
          ++;
              
          for (; iter != result.end(); iter++{
                  fout 
          << " " << *iter;
              }

              fout 
          << endl;
              
          return 0;
          }

          posted @ 2007-05-31 17:17 ZelluX 閱讀(702) | 評(píng)論 (0)編輯 收藏

          第一次做是用一張hash表記錄的所有的數(shù)是否為所謂的bisquare,然后先枚舉公差,再枚舉首項(xiàng),本來(lái)想這樣做就不用再對(duì)結(jié)果排序了,沒(méi)想到效率很低。
          改為先枚舉首項(xiàng),再枚舉第二項(xiàng),然后計(jì)算公差后,速度提高不少。
          另外第一次使用sort方法。
          /*
          PROG: ariprog
          ID: 06301031
          LANG: C++
          */


          #include 
          <iostream>
          #include 
          <fstream>
          #include 
          <vector>
          #include 
          <algorithm>

          using namespace std;

          struct Ans {
              
          int start;
              
          int step;
          }
          ;

          bool compareOp(const Ans& ans1, const Ans& ans2);

          int main() {
              ifstream fin(
          "ariprog.in");
              ofstream fout(
          "ariprog.out");
              
          int n, m, i, j, k;
              
          bool solved = false;
              fin 
          >> n >> m;

              
          // generate Arithmetic Progressions sequence
              bool seq[250*250*2 + 1];    
              
          for (i = 0; i <= m * m * 2; i++{
                  seq[i] 
          = false;
              }

              
          for (i = 0; i <= m; i++{
                  
          for (int j = 0; j <= i; j++{
                      seq[i
          *+ j*j] = true;
                  }

              }


              vector
          <Ans> result;
              
          int step;
              
          for (i = 0; i <= m * m * 2; i++{
                  
          if (!seq[i]) {
                      
          continue;
                  }

                  
          for (j = i + 1; j <= m * m * 2; j++{
                      
          if (!seq[j]) {
                          
          continue;
                      }

                      step 
          = j - i;
                      
          if (i + step * (n - 1> m * m * 2{
                          
          break;
                      }


                      
          bool flag = true;
                      
          for (k = 1; k < n; k++{
                          
          if (!seq[i + step * k]) {
                              flag 
          = false;
                              
          break;
                          }

                      }

                      
          if (flag) {
                          Ans ans;
                          ans.start 
          = i;
                          ans.step 
          = step;
                          result.push_back(ans);
                          solved 
          = true;
                      }

                  }

              }

              
          if (!solved) {
                  fout 
          << "NONE" << endl;
              }


              sort(result.begin(), result.end(), compareOp);
              vector
          <Ans>::iterator iter;
              
          for (iter = result.begin(); iter != result.end(); iter++{
                  fout 
          << iter->start << " " << iter->step << endl;
              }

              fout.close();
              
          return 0;
          }


          bool compareOp(const Ans& ans1, const Ans& ans2) {
              
          return ans1.step < ans2.step;
          }

          posted @ 2007-05-30 22:18 ZelluX 閱讀(632) | 評(píng)論 (0)編輯 收藏

          僅列出標(biāo)題
          共39頁(yè): First 上一頁(yè) 23 24 25 26 27 28 29 30 31 下一頁(yè) Last 
          主站蜘蛛池模板: 林口县| 昌黎县| 隆昌县| 兴仁县| 舒城县| 瑞丽市| 雅安市| 永登县| 重庆市| 拉孜县| 大荔县| 雷山县| 泸西县| 盐源县| 枞阳县| 湾仔区| 宣城市| 北川| 筠连县| 廊坊市| 兴业县| 吉林省| 大新县| 山东| 涟水县| 长宁县| 普安县| 德江县| 色达县| 略阳县| 白水县| 佛学| 南投市| 繁峙县| 辽阳市| 兰坪| 香格里拉县| 亚东县| 古蔺县| 石城县| 突泉县|