# 【ASC 23】G. ACdream 1429 Rectangular Polygon --DP

dp[i][j]表示前 i 个 偏移量为 j 的最大棍子数，因为每根棍最长为200，所以偏移量最多为+-20000，所以在+-20000之间枚举，最多100*40000

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
#define N 30007

int dp[105][60004],path[105][60004];
int a[105];

int main()
{
freopen("polygon.in","r",stdin);
freopen("polygon.out","w",stdout);
int n,i,j;
while(scanf("%d",&n)!=EOF)
{
memset(dp,-1,sizeof(dp));
int sum = 0;
for(i=1;i<=n;i++) {
scanf("%d",&a[i]);
sum += a[i];
}
int low = N-sum, high = N+sum;
dp[0][N] = 0;
for(i=1;i<=n;i++) {
for(j=low;j<=high;j++) {
if(dp[i-1][j] != -1 && dp[i][j] < dp[i-1][j]) {
dp[i][j] = dp[i-1][j];
path[i][j] = j;
}
if(dp[i-1][j-a[i]] != -1 && dp[i][j] < dp[i-1][j-a[i]]+1) {
dp[i][j] = dp[i-1][j-a[i]]+1;
path[i][j] = j-a[i];
}
if(dp[i-1][j+a[i]] != -1 && dp[i][j] < dp[i-1][j+a[i]]+1) {
dp[i][j] = dp[i-1][j+a[i]]+1;
path[i][j] = j+a[i];
}
}
}
printf("%d\n",dp[n][N]);
int now = N,pre;
vector<int> UP,DOWN;
for(i=n;i>=1;i--) {
pre = path[i][now];
if(now > pre) UP.push_back(now-pre);
if(pre > now) DOWN.push_back(pre-now);
now = pre;
}
int x = 0, y = 0;
for(i=0;i<UP.size();i++) {
printf("%d %d\n",x,y);
x += UP[i];
printf("%d %d\n",x,y);
y++;
}
for(i=0;i<DOWN.size();i++) {
printf("%d %d\n",x,y);
x -= DOWN[i];
printf("%d %d\n",x,y);
y++;
}
}
return 0;
}
View Code

posted @ 2015-03-05 23:08 whatbeg 阅读(...) 评论(...) 编辑 收藏