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

题意:有很多棍子,从棍子中选出两个棍子集合,使他们的和相等,求能取得的最多棍子数。

解法:容易看出有一个多阶段决策的过程,对于每个棍子,我们有 可以不选,或是选在第一个集合,或是选在第二个集合 这三种决策。因为两个集合最后的和要相等,那么令一个集合为正,另一个为负,那么最后和为0,我们用偏移0的量来作为状态之一。

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;
}

时间: 2024-10-25 07:48:53

【ASC 23】G. ACdream 1429 Rectangular Polygon --DP的相关文章

acdream 1429 Rectangular Polygon【dp】

题目:acdream 1429 Rectangular Polygon 题意:给出n个数,是边长,然后问你能不能组成多边形,其边必须是平行于x轴或者平行于y轴的.如果可以的话输出任意一种方案. 分析:分析发现,就是给你一堆数,然后让你尽可能挑出多的数,分成两部分的和相等,我们可以用dp来解决这个问题. 定义dp[i][j] 前 i 个数中挑出一些书其两部分的差值是 j 的最大个数. 转移方程: dp[i][j] = max(dp[i][j],dp[i-1][j]); //不放 dp[i][j-a

ACdream 1429 Rectangular Polygon

Rectangular Polygon Time Limit: 1000MS   Memory Limit: 256000KB   64bit IO Format: %lld & %llu Description A rectangular polygon is a polygon whose edges are all parallel to the coordinate axes. The polygon must have a single, non-intersecting bounda

【例2-3】围圈报数

[例2-3]围圈报数 链接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1334时间限制: 1000 ms         内存限制: 65536 KB [题目描述] 有n个人依次围成一圈,从第1个人开始报数,数到第m个人出列,然后从出列的下一个人开始报数,数到第m个人又出列,-,如此反复到所有的人全部出列为止.设n个人的编号分别为1,2,-,n,打印出列的顺序. [输入] n和m. [输出] 出列的顺序. [输入样例] 4 17 [输出样例]

1334:【例2-3】围圈报数

1334:[例2-3]围圈报数 时间限制: 1000 ms         内存限制: 65536 KB提交数: 2253     通过数: 1482 [题目描述] 有nn个人依次围成一圈,从第11个人开始报数,数到第mm个人出列,然后从出列的下一个人开始报数,数到第mm个人又出列,…,如此反复到所有的人全部出列为止.设nn个人的编号分别为1,2,…,n1,2,…,n,打印出列的顺序. [输入] nn和mm. [输出] 出列的顺序. [输入样例] 4 17 [输出样例] 1 3 4 2 [提示]

【POJ 3254】 Corn Fields(状压DP)

[POJ 3254] Corn Fields(状压DP) Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10891   Accepted: 5705 Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parce

【POJ 2029】 Get Many Persimmon Trees(DP)

[POJ 2029] Get Many Persimmon Trees(DP) Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4024   Accepted: 2628 Description Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aizu for a long time in the 18

【HDU 4352】 XHXJ&#39;s LIS (数位DP+状态压缩+LIS)

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2422    Accepted Submission(s): 990 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

【HDU 5647】DZY Loves Connecting(树DP)

pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 332    Accepted Submission(s): 112 Problem Description DZY has an unroote

【POJ 2486】 Apple Tree(树型dp)

[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Accepted: 2990 Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each