跳到主要內容

[Training] GCJ 2021 Qualification Round - Reversort

 第一次參加 GCJ,之前一直沒時間完成這個夢想,這次勇敢踏出第一步。

Qualification Round 已經晉級,我檢討並總結自己的 Code,將比較好的寫法整理下來。



第一題 Reversort 很簡單,沒太大的問題。

題目要求實現 Reversort 演算法,Pseudo Code 已經給予,直接依照 Pseudo Code 刻程式碼即可:

Reversort(L):

  for i := 1 to length(L) - 1

    j := position with the minimum value in L between i and length(L), inclusive

    Reverse(L[i..j])

這裡要注意,矩陣 reverse 有既有的函式可以用,以及 Case 輸出 index 是從 1 開始計,而非 0 開始起算。

以下是我總結較好的寫法:

// by EdocZec on 2021/04/03
// Google Code Jam 2021-01

#include <bits/stdc++.h>
using namespace std;

template<typename T>
ostream& operator<<(ostream&os, const vector<T>& v)
{
    for(int i=0; i<v.size(); i++)
        os << v[i] << " ";
    return os;
}
int readi()
{
    int i;
    cin >> i;
    return i;
}
int alg(vector<int>& a)
{
    int score = 0;
    for(int i=0; i<(a.size()-1); i++)
    {
       int min_idx = i;
       for(int j=i+1; j<(a.size()); j++) 
       {
            if(a[min_idx] > a[j])    min_idx = j;   
       }
       score += min_idx - i + 1;
       reverse(a.begin()+i, a.begin()+min_idx+1);
    }
    return score;
}
int main(int argc, char** argv)
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t, len;

    cin >> t;

    for(int i=0; i<t; i++)
    {
        cin >> len;
        vector<int> a;
        for(int j=0; j<len; j++)    a.push_back(readi());
        //cout << "[Debug] " << a << endl;

        int score = alg(a);
        cout << "Case #" << i+1 << ": " << score << endl; 
    }
    return 0;
}

下一篇將會檢討 Qualification Round 其他的題目。

留言

這個網誌中的熱門文章

[開箱] Filco Majestouch 2 忍者鍵盤第二代

最近買了一個新款的 Filco 忍者鍵盤第二代,NINJA Majestouch 2: 鍵盤對我來說蠻重要的,以前就很喜歡 Filco 品牌的鍵盤,我大學使用的第一支茶軸鍵盤就是 Filco 的牌子,現在已經充滿了灰塵,使用了至少已有 8 年了吧: 畢業後有買另一個 irock 鍵盤: 這禮拜拆開新鍵盤來看看: 內部包裝 內部包裝 採用按鍵的文字採用側印 鍵帽與拔鍵器 整體外觀 整體而言,美感簡約低調,按鍵的反饋力也很好,是一個適合寫程式的鍵盤。 相較於我的另一個也是德國櫻桃茶軸鍵盤 irock 品牌,我覺得 Filco 的按鍵回饋比較綿密,有漸層的回饋感,且按鍵聲音比較安靜。irock 則比較像機械打字的回饋感,敲字時也都蠻舒壓。

[程式競賽] UVa 572, Oil Deposits,Flood Fill 演算法

原題目簡述如下: 以 m x n 大小的 grid 代表一張地圖,現今要在此地圖內探勘,找出油田。某一區塊如果標示 "@" 代表有油,"*" 代表沒有油。 "@" 相鄰的區域的聯集,可視為一個油田。(所謂相鄰,除了上下左右,斜向的相鄰也算進去) 求任意地圖中,油田的個數。 例如輸入的測資為: *    *   *    *  @ *   @  @  *  @ *   @   *   *  @ @ @  @   * @ @ @   *   *  @ 則油田個數為 2。 想法 採用典型的倒水演算法(Flood Fill),走訪 "@" 出現的區域,從此往下倒水,倒過水的區域標上 id,因此透過 id 的編號,可以得知油田的個數。 實作 先實作倒水演算法的子函式: void floodfill(vector<vector<char> >& map,                vector<vector<int>  >& id_table,                int row, int col, int id) {    if(row < 0 || (row >= map.size()) )   return;    if(col < 0 || (col >= map[0].size())) return;    if(map[row][col] != '@' || id_table[row][col] > 0) return;    id_table[row][col] = id;   ...

[心得] 復古、老派的程式設計之路

最近看到 John Carmack 2018 年的貼文中的幾段話: I’m not a Unix geek.  I get around ok, but I am most comfortable developing in Visual Studio on Windows.  I thought a week of full immersion work in the old school Unix style would be interesting, even if it meant working at a slower pace.  It was sort of an adventure in retro computing — this was fvwm and vi.  Not vim, actual BSD vi. In the end, I didn’t really explore the system all that much, with 95% of my time in just the basic vi / make / gdb operations.  I appreciated the good man pages, as I tried to do everything within the self contained system, without resorting to internet searches.  Seeing references to 30+ year old things like Tektronix terminals was amusing. In the spirit of my retro theme, I had printed out several of Yann LeCun’s old papers and was considering doing everything completely off line, as if I was actually in a mountain cabin somewhere, but I wound up watching a lot of the Stanford CS231N lectur...