UVa Problem 10049 Self-describing Sequence (自描述序列)

來源:互聯網
上載者:User
// Self-describing Sequence (自描述序列)// PC/UVa IDs: 110607/10049, Popularity: C, Success rate: high Level: 2// Verdict: Accepted// Submission Date: 2011-06-07// UVa Run Time: 0.048s//// 著作權(C)2011,邱秋。metaphysis # yeah dot net//// 顯式的產生這個序列顯然是超出記憶體限制也是沒有必要的。有兩種方法,一種是得出遞推關係,一// 種是根據序列特點得到某數的f(n)值。//// 遞推方法:設 G(n) 表示滿足 f(k) > f(k - 1) 時的自變數值,觀察序列可以知道 G(1)// = 2,G(2) = 4,G(3) = 6,G(4) = 9,... ,由序列的定義可以知道區間 // [G(n - 1),G(n)) 有 f(n) 個 n,則可以知道://// G(n) = f(1) + f(2) + f(3) + ... + f(n) + 1//// 則有://// G(n) = G(n - 1) + f(n)//// 由於已經知道 [G(n - 1),G(n)) 之間的數函數值都是 n。則可以通過以下方式計算// G(n) 的值:// G(1) = 2,G(2) = 4,k = 1,對於[G(k),G(k + 1))之間的數,其 f(n) =// k + 1,則有 G(n) = G(n - 1) + k + 1。//// 非遞推方法:根據以下事實,從 n = 3 開始對於序列中的每一對 f(n) = k,則必將在序列// 中添加 f(n) * k 個元素。可以使用一個隊列來記錄添加的元素,計算將要添加的元素總數,// 直到該隊列將要產生的元素量超過指定數量。根據情況判定要求的 n 值的函數值。具體方法如下:// // 1. n = 1 或者 n = 2,特殊情況,予以特殊處理。// 2. 設 sequence 為一個隊列,記錄該序列的元素,初始情況下,隊列中儲存了 n = 3 時的函// 數值即 f(3) = 2。變數 total 根據 f(n) * k 的關聯性記錄當前隊列中儲存元素所能產生// 的元素總量,初始值為 0。變數 n 為當前的自變數值。// 3. 從隊列中取出第一個元素,設其值為 fn,可知 f(n) = fn。則隊列所能產生元素個數// total += fn * n。比較當前 n 與輸入中要求函數值的數 k,若相等則表明 f(k) = fn。// 否則繼續計算 total 直到上限值。// 4. 當前序列已經產生了足夠的元素,最後能產生指定上限的元素,則繼續從隊列首位取出一個元// 素,按前述步驟,只不過這時不需要實際在隊列的最後添加元素,而是計算 total 的變化範圍,// 在 [total, total + fn) 之間數的函數值都是剛彈出的元素所對應的 n 值。同時仍應比對隊// 列首的 n 值,若有值對應,則該數的函數值就是彈出的隊列元素值 fn。// 5. 繼續處理直到找到所有輸入數的函數值,然後輸出結果。#include <iostream>#include <vector>#include <algorithm>#include <queue>using namespace std;struct data{int value;int index;int fn;};// 遞推方式解法。void self_describing_by_recurrence(){int self[700000];const int max = 2000000000;self[0] = 1;self[1] = 2;self[2] = 4;int index;while (self[self[index] - 1] < max){for (int j = self[index]; j < self[index + 1]; j++)self[j] = self[j - 1] + index + 1;index++;}int capacity = self[index] - 1;int n;while (cin >> n, n)cout << upper_bound(self, self + capacity, n) - self << endl;}bool cmp(data x, data y){return x.value < y.value;}// 非遞推方式解法。void self_describing_by_iterate(){int counter = 0, tmp;vector < data > input;while (cin >> tmp, tmp){data t;t.value = tmp;t.index = counter++;t.fn = 0;input.push_back(t);}// 排序以便輸出。sort(input.begin(), input.end(), cmp);// 記錄當前已添加的元素的隊列。queue < int > sequence;// 初始時函數自變數 n 為 3,當前已有兩個元素,即 total 所指定的值。int n = 3, total = 2, index = 0, maxn = input[input.size() - 1].value;// 輸入中尚未設定 f(n) 值的元素個數。int unsetted = input.size();// 處理 n == 1 和 n == 2 的情況。while (input[index].value <= 2){input[index].fn = input[index].value;index++;unsetted--;}if (!unsetted)goto out;// 壓入初始元素。sequence.push(2);// 當將要添加的元素總數少於輸入的最大給定值時,繼續計算添加元素的數量。while (total < maxn){// 根據序列定義,將 fn 個 n 壓入隊列。int fn = sequence.front();for (int c = 1; c <= fn; c++)sequence.push(n);// 計算隊列所能產生元素的總數。total += fn * n;// 比較當前 n 值是否和要求的數相等,若相等,則表明要求的數的函數值// 就是隊列首的元素 fn。if (input[index].value == n){input[index++].fn = fn;unsetted--;if (!unsetted)goto out;}// 彈出隊列首元素。sequence.pop();n++;}total = n + sequence.size();while (sequence.size()){// 比較隊列前端和輸入 n 值是否匹配。int fn = sequence.front();if (input[index].value == n){input[index++].fn = fn;unsetted--;}// 確定是否有元素在區間 [total, total + number)。for (int i = index; i < input.size(); i++)if (total <= input[i].value && input[i].value <= (total + fn - 1)){input[i].fn = n;unsetted--;}// 所有數都已設定,列印退出。if (!unsetted)goto out;// 計數要添加的元素個數,並不需要實際添加。total += fn;sequence.pop();n++;}out:for (int i = 0; i < input.size(); i++)for (int j = 0; j < input.size(); j++)if (input[j].index == i)cout << input[j].fn << endl;}int main(int ac, char *av[]){self_describing_by_recurrence();// self_describing_by_iterate();return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.