ACdream 1429 Rectangular Polygon
来源:互联网 发布:数据库常用设计工具 编辑:程序博客网 时间:2024/05/02 09:48
Description
A rectangular polygon is a polygon whose edges are all parallel to the coordinate axes. 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. He is planning to use sticks as horizontal edges of the polygon, and draw vertical edges with a pen.
Now Johnny wonders, how many sticks he can use. Help him, find the maximal number of sticks that Johnny can use. He will use sticks only as horizontal edges.
Input
Output
. If no polygon can be constructed, output l = 0.
Sample Input
41 2 3 541 2 4 841 1 1 1
Sample Output
30 01 01 13 13 20 2040 01 01 12 12 -21 -21 -10 -1
Hint
单组数据
In the first 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 and a stick of length 3 for (3, 2) − (0, 2) edge. There is no way to use all four sticks.
dp+记录路径
#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;}
- ACdream 1429 Rectangular Polygon
- ACdream 1429 Rectangular Polygon
- ACdream 1429 Rectangular Polygon (DP)
- acdream 1429 Rectangular Polygon【dp】
- acdream1429 rectangular polygon
- Andrew Stankevich Contest 23 G - Rectangular Polygon
- Problem G. Rectangular Polygon(dp + 转移重现)
- polygon
- Polygon
- ACdream
- Acdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- centos 6.5下安装R 3.2.0
- 自定义taglib实例代码分析
- 面试题40 数组中只出现一次的数字
- hdu1796(容斥原理)
- nf_conntrack: table full, dropping packet解决方法
- ACdream 1429 Rectangular Polygon
- MapReduce源码分析总结
- Ubuntu下ns2中打补丁与生成补丁
- USB开发—自上而下(一)
- JavaBean
- hdu1085(母函数)
- 分布式流式处理框架:Storm
- hdoj1711-- Number Sequence(KMP模板)
- Mysql学习之路——day3