Find the Hamiltonian given $\dot p$ and $\dot q$ I have these equations:
$$\dot p=ap+bq,$$ 
$$\dot q=cp+dq,$$
and I have to find the conditions such as the equations are canonical. Then, I have to find the Hamiltonian $H$. 
To answer to the first question, I have imposed that
$$\frac{\partial}{\partial q}(\frac{\partial H}{\partial p})+\frac{\partial }{\partial p}(-\frac{\partial H}{\partial q})=0$$
$$\frac{\partial}{\partial q}(cp+dq)+\frac{\partial }{\partial p}(ap+bq)=0$$  $$\Rightarrow d+a=0.$$
And so I have that the canonical equations are in the form: 
$$\dot p=ap+bq,$$ $$\dot q=cp-aq.$$
But how can I find the Hamiltonian? The result must be $H=-apq-\frac{1}{2}bq^2+\frac{1}{2}cp^2$. The general expression $H=\sum p_i \dot q_i-L$ doesn't help me.
 A: I) OP is given a problem of the form
$$\tag{1} \dot{q}~=~f(q,p), \qquad   \dot{p}~=~g(q,p), $$
where $f$ and $g$ are two given smooth functions. OP is asked to derive the integrability condition for the eqs. (1) to be Hamilton's eqs.
$$\tag{2}  \dot{q}~=~\frac{\partial H}{\partial p}, \qquad   
\dot{p}~=~-\frac{\partial H}{\partial q}.$$ 
OP correctly deduces that 
$$\tag{3}  f~=~\frac{\partial H}{\partial p}, \qquad   
g~=~-\frac{\partial H}{\partial q},$$ 
and that the integrability condition is the Maxwell-type relation
$$\tag{4} \frac{\partial f}{\partial q}+\frac{\partial g}{\partial p}~=~0.$$
Michael Brown in a comment then explains that if 
$$\tag{5} F(q,p)~=~\int^{p}\!dp^{\prime}~f(q,p^{\prime})$$ 
is some antiderivative/primitive integral/indefinite integral of the given $f$ function, so that 
$$\tag{6} \frac{\partial F}{\partial p}~=~f,$$
then eqs. (3a) and (6) imply that
$$\tag{7} \frac{\partial (H-F)}{\partial p}~=~0.$$
In other words, the difference $H-F$ cannot depend on the $p$ variable. It could be an arbitrary function $G(q)$ of the $q$ variable only. So the Hamiltonian is of the form
$$\tag{8} H(q,p)~=~F(q,p)+G(q). $$
Finally, one gets restrictions on the $G$ function by plugging eq. (8) into eq. (3b). This should answer OP's question (v4), and we are in principle done.
II) However, we cannot resist making the following general point about the existence of a Hamiltonian formulation. OP's title question is a bit academic if one a priori insists that the variables $q$ and $p$ should play the role of canonical variables. Why would one insist on that? The goal is, after all, just to get a Hamiltonian formulation, whatever it takes. So a more realistic question is the following more general problem.

Suppose that we are given a two-dimensional first-order problem
$$\tag{9} \dot{x}~=~f(x,y), \qquad   \dot{y}~=~g(x,y), $$
where $f$ and $g$ are two given smooth functions. Is eq. (9) a Hamiltonian system 
  $$\tag{10}  \dot{x}~=~\{x,H\}, \qquad   \dot{y}~=~\{y,H\}, $$
  with a symplectic structure $\{\cdot,\cdot\}$ and Hamiltonian $H(x,y)$? 

The answer is, perhaps surprisingly: Yes, always, at least locally,
cf. e.g. my Phys.SE answer here.
