Description
A rectangular polygon is a polygon whose edges are all parallel to the coordinate. The polygon must have a single, non-intersecting boundary. No two adjacent sides must be parallel.
Johnny has several sticks of various lengths. He would like to construct a rectangular polygon. It planning to use sticks as horizontal edges of the polygon, and draw vertical and a pen.
Now Johnny wonders and many sticks he can use. Help him, find the maximal number of sticks this Johnny can use. He would use the sticks only as horizontal edges.
Input the ' the ' input file contains n-the number of sticks (1≤n≤100). The second line contains n integers numbers-the lengths of the sticks he has. The length of each stick doesn ' t exceed 200.
Output Print l-the number of sticks Johnny can use at the the "the" Output file. The following 2l lines must contain the vertices of the rectangular polygon Johnny can construct. Vertices must is listed in the order of traversal. The two vertices must is the ends of a horizontal edge. If There are several solution, output any one. Vertex coordinates must not exceed 10 9
. If No polygon can be constructed, output L = 0.
Sample Input
4
1 2 3, 5 4 1 2 4 8 4 1 1 1 1
Sample Output
3
0 0 1 0 1 1 3 1 3 2 0 2 0 4 0-0 1 0 1 1 2 1
2-2
1-2
1-1
0-1
Hint
Single set of data
In the the ' the ' example Johnny uses a stick of length 1 for (0, 0) − (1, 0) edge, a stick of length 2 for (1, 1) − (3, 1)-Edge an D A stick of length 3 for (3, 2) − (0, 2) edge. There is no way to use all four sticks.
dp+ Record Path
#include <cstdio> #include <algorithm> #include <cstring> using namespace std;
const int MAXN = 40005;
int F[105][MAXN], P[105][MAXN], A[MAXN], L, R, N, B[2][MAXN];
int main () {int i, J;
while (~SCANF ("%d", &n)) {memset (f, 0, sizeof (f));
memset (p, 0, sizeof (p));
F[0][l = r = 20000] = 1;
for (i = 1; I <= n; i++) {scanf ("%d", &a[i]);
for (j = l; J <= R; j +) {if (F[i][j]<f[i-1][j]) {f[i][j] = f[i-1][j];
P[I][J] = P[i-1][j];
} if (F[i-1][j]) {if (f[i][j + a[i]]<f[i-1][j] + 1) {f[i][j + a[i]] = f[i-1][j] + 1;
P[i][j + a[i]] =-I;
} if (F[i][j-a[i]]<f[i-1][j] + 1) {F[i][j-a[i]] = f[i-1][j] + 1;
P[i][j-a[i]] = i;
L = min (l, j-a[i]);
r = Max (R, J + A[i]);
} printf ("%d\n", f[n][20000]-1);
if (f[n][20000]>1) {int k1 = 0, k2 = 0;
for (i = p[n][20000], j = 20000; i;) {if(i>0)
{b[1][k2++] = A[i];
J + + A[i];
i = p[i-1][j];
else {i =-I;
b[0][k1++] = A[i];
J-= A[i];
i = p[i-1][j];
int x = 0, y = 0;
for (i = 0; i<k1; i++) {printf ("%d%d\n", x, y);
printf ("%d%d\n", (x + = B[0][i]), y++);
for (i = 0; i<k2; i++) {printf ("%d%d\n", x, y);
printf ("%d%d\n", (X-= B[1][i]), y++);
}} return 0; }