Code Golf - Heapify

void heapify(vector<int>& vec) {
    int n = vec.size();
    for (int a = 0, b = 1; b - 1 < n; a = b, b <<= 1) {
        nth_element(vec.begin() + a, vec.begin() + b, vec.end());
    }
}

You can implement a "heapify" by only four lines of code. And the time complexity is O(n).

more ...


Workflowy full notes

Recently, I'm becoming a crazy fan of workflowy because its ability to build a personal wiki concisely.

However, there's only one thing bothers me that workflowy hide all the notes as the screenshot below, and it annoys me a lot.

Alt text

Luckily we can always find a way to get around …

more ...

用C++实现一个通用的sort函数

问题

用C++实现一个尽可能通用的sort函数

分析

一个通用的sort函数应该包含以下要点:

  • 确实可以排序(LOL)
  • 可以应对C-style array和C++-style container的排序需求
  • 可以应用于任意random access container
  • 可以 …
more ...

ZeroMQ启示录

ØMQ是一个消息系统

ZeroMQ是一个消息系统,也被称为“消息中间件”。它被广泛的用于经济、游戏、嵌入式等领域。

什么是消息系统

打个比方,消息系统就像我们使用的 …

more ...

Codeforces Round #290 (Div. 2) Tutorial

A. Fox And Snake

Implementation

(n, m) = map(int, raw_input().split())

res = []

for i in xrange(n):
    if i % 2 == 0:
        res.append('#' * m)
    elif (i / 2) % 2 == 0:
        res.append('.' * (m - 1) + '#')
    else:
        res.append('#' + '.' * (m - 1))

for line in res:
    print line

B. Fox And Two Dots

DFS …

more ...

Codeforces Round #289 (Div. 2) Tutorial

A. Maximum in Table

Simulation.

n = int(raw_input())
g = [[1 for i in xrange(n)] for j in xrange(n)]

for i in xrange(1, n):
    for j in xrange(1, n):
        g[i][j] = g[i - 1][j] + g[i][j - 1]

print g[n - 1][n - 1]

B …

more ...

Codeforces Round #288 (Div. 2)

A. Pasha and Pixels

Brute force.

There are multiple ways to form a 2*2 square at one single step.

Alt text

So at every step, we have to check the neighbours of pixel that is colored black.

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define …
more ...

Sequence Median

Description

Given a sequence of integer numbers, try to find the median of the sequence.

Extending

  • Make sure your code can get the right answer in any conditions
  • Make sure your code work effectively on some special kinds of sequence. For example, ordered sequence or a nearly ordered one, a …
more ...