您的位置:

CodeJam编程比赛全面详解

一、CodeJam T恤

CodeJam(又称谷歌编程大赛)是谷歌公司举办的一项全球性编程比赛,旨在为全球程序员提供一个交流平台和提高自己编程能力的机会。每年谷歌都会派发一些活动纪念品来丰富比赛参与者的收藏,其中最受欢迎的莫过于CodeJam T恤。这种T恤的设计很特别,上面包含着历届CodeJam的logo或者CodeJam比赛的难题名称,很适合那些富有纪念意义的程序员们收藏。

CodeJam T-Shirt:

#include <iostream>
#include <string>
using namespace std;

int main()
{
  string tShirt;
  getline(cin, tShirt);
  cout << "I love my CodeJam T-shirt with " << tShirt << " printed on it!" << endl;
  return 0;
}

二、CodeJam是什么比赛

CodeJam是一种面向全球程序员的编程比赛,更确切地说,CodeJam是一种算法竞赛。CodeJam比赛不需注册费用,全程使用英语,官方旨在推广计算机科学和编程,鼓励优秀的问题解决能力及创新精神,并吸引人才加入Google。

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
  int testCases;
  cin >> testCases;
  
  for (int i = 1; i <= testCases; i++) {
      double a, b, c;
      cin >> a >> b >> c;
      double s = (a + b + c) / 2;
      double area = sqrt(s*(s-a)*(s-b)*(s-c));
      cout << "Case #" << i << ": " << area << endl;
  }
  
  return 0;
}

三、CodeJam难度

CodeJam比赛的难度层级比较高,涉及到算法、数据结构、编程的全方面技能。难度从Round 1(R1)到Finals,依次递增,同时每个Round附带着若干题目,参赛选手需要根据难度选择自己最为擅长的部分加以解决,操作难度和解题难度都需要一定的技巧。

#include <iostream>
#include <string>
using namespace std;

int main()
{
   int n;
   cin >> n;
   string s;
   getline(cin, s);
   
   for (int i = 0; i < n; i++) {
       getline(cin, s);
       int length = s.length();
       int j = 0;
       bool hasPlusSign = false;
       while (j < length) {
           if (s[j] == '+') {
               hasPlusSign = true;
               break;
           }
           j++;
       }
       if (hasPlusSign) {
           int result = 0;
           int k;
           for (k = 0; k < j; k++) {
               if (s[k] != ' ') {
                   result += (s[k] - '0');
                   result *= 10;
               }
           }
           result /= 10;
           cout << "Case #" << (i + 1) << ": " << result << endl;
       } else {
           cout << "Case #" << (i + 1) << ": " << j << endl;
       }
   }
   return 0;
}

四、CodeJam题目

CodeJam比赛的难题覆盖了众多计算机领域,并且难度层次比较严谨。通常情况下,难度递增的题目分为Round 1 (R1)、Round 2 (R2)、Round 3 (R3)、Online Qualification Round等等。以下是一道CodeJam R1B的例题:A.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    int t, n;
    string s;
    cin >> t;
    for (int i = 1; i <= t; i++) {
        cin >> n >> s;
        vector groups;
        int currGroup = 1;
        for (int j = 1; j < s.size(); j++) {
            if (s[j] == s[j - 1]) {
                currGroup++;
            } else {
                groups.push_back(currGroup);
                currGroup = 1;
            }
        }
        groups.push_back(currGroup); 
        int ans = 0;
        for (int j = 1; j < groups.size(); ++j) {
            ans += min(groups[j], groups[j - 1]);
        }
        cout << "Case #" << i << ": " << ans << endl;
    }
    return 0;
}

  

五、CodeJam 2022

CodeJam每年都会举办比赛,2022年度CodeJam也不例外。根据官网的消息,2022年度的CodeJam已经与2021年一样,分 R1A、R1B、R2、R3 四个轮次。诚邀各位优秀的程序员们踊跃参加,一起切磋编程技艺!

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
  cout << "Welcome to CodeJam 2022!" << endl;
  srand(2022);
  int a = rand() % 100;
  int b = rand() % 100;
  
  cout << "Let's do some math: " << a << " + " << b << " = ?" << endl;
  
  int answer;
  cin >> answer;
  
  if (answer == a + b) {
    cout << "Congratulations, you got the right answer!" << endl;
  } else {
    cout << "Sorry, the correct answer is " << a + b << endl;
  }
  return 0;
}

六、CodeJam Round 2

CodeJam 的Round 2比赛难度要更高,但是也能在很多领域为程序员们提供更多的挑战。以下是一道CodeJam 2020 Round 2的例题:B。

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;

const int MAXN = 1e5 + 10;
long long ans[MAXN], score[MAXN];

struct Q {
    int l, r, id;
    Q() {};
    Q(int l_, int r_, int id_) {l = l_; r = r_; id = id_;}
    bool operator < (const Q &other) const {
        if (l / 320 != other.l / 320) {
            return l / 320 < other.l / 320;
        }
        return r < other.r;
   }
};

vector g[MAXN];
int a[MAXN], start[MAXN], end[MAXN];
bool seen[MAXN];
int t, n, q, cnt, b[MAXN], c[MAXN];

long long dfs(int u, int p) {
    seen[u] = true;
    start[u] = cnt;
    b[cnt] = a[u];
    c[cnt] = u;
    cnt++;
    for (int v : g[u]) {
        if (v != p) {
            dfs(v, u);
            b[cnt] = a[u];
            c[cnt] = u;
            cnt++;
        }
    }
    end[u] = cnt;
    return score[u];
}

int main() {
    cin >> t;
    for (int qq = 1; qq <= t; qq++) {
        cin >> n >> q;
        memset(seen, false, sizeof(seen));
        for (int i = 1; i <= n; i++) {
            g[i].clear();
            cin >> a[i];
        }
        for (int i = 1; i <= n; i++) {
            cin >> score[i];
        }
        for (int i = 1; i <= n - 1; i++) {
            int x, y;
            cin >> x >> y;
            g[x].push_back(y);
            g[y].push_back(x);
        }
        dfs(1, -1);
        vector
    queries;
        for (int i = 1; i <= q; i++) {
            int l, r;
            cin >> l >> r;
            queries.push_back(Q(l, r, i));
        }
        sort(queries.begin(), queries.end());
        int st = 0, ed = 0;
        long long sum = 0;
        for (Q x : queries) {
            int l = x.l, r = x.r, id = x.id;
            while (ed < r) {
                sum += score[c[ed]] - b[ed];
                ed++;
            }
            while (st > l) {
                sum += score[c[st-1]] - b[st-1];
                st--;
            }
            while (st < l) {
                sum -= score[c[st]] - b[st];
                st++;
            }
            while (ed > r) {
                sum -= score[c[ed-1]] - b[ed-1];
                ed--;
            }
            ans[id] = sum;
        }
        cout << "Case #" << qq << ": ";
        for (int i = 1; i <= q; i++) {
            cout << ans[i];
            if (i != q) {
                cout << " ";
            }
        }
        cout << endl;
    }
    return 0;
}