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

解法:容易看出有一个多阶段决策的过程,对于每个棍子,我们有 可以不选,或是选在第一个集合,或是选在第二个集合 这三种决策。因为两个集合最后的和要相等,那么令一个集合为正,另一个为负,那么最后和为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[][],path[][];
int a[]; int main()
{
freopen("polygon.in","r",stdin);
freopen("polygon.out","w",stdout);
int n,i,j;
while(scanf("%d",&n)!=EOF)
{
memset(dp,-,sizeof(dp));
int sum = ;
for(i=;i<=n;i++) {
scanf("%d",&a[i]);
sum += a[i];
}
int low = N-sum, high = N+sum;
dp[][N] = ;
for(i=;i<=n;i++) {
for(j=low;j<=high;j++) {
if(dp[i-][j] != - && dp[i][j] < dp[i-][j]) {
dp[i][j] = dp[i-][j];
path[i][j] = j;
}
if(dp[i-][j-a[i]] != - && dp[i][j] < dp[i-][j-a[i]]+) {
dp[i][j] = dp[i-][j-a[i]]+;
path[i][j] = j-a[i];
}
if(dp[i-][j+a[i]] != - && dp[i][j] < dp[i-][j+a[i]]+) {
dp[i][j] = dp[i-][j+a[i]]+;
path[i][j] = j+a[i];
}
}
}
printf("%d\n",dp[n][N]);
int now = N,pre;
vector<int> UP,DOWN;
for(i=n;i>=;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 = , y = ;
for(i=;i<UP.size();i++) {
printf("%d %d\n",x,y);
x += UP[i];
printf("%d %d\n",x,y);
y++;
}
for(i=;i<DOWN.size();i++) {
printf("%d %d\n",x,y);
x -= DOWN[i];
printf("%d %d\n",x,y);
y++;
}
}
return ;
}

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

  1. ACdream 1429 Rectangular Polygon

    Rectangular Polygon Time Limit: 1000MS   Memory Limit: 256000KB   64bit IO Format: %lld & %llu D ...

  2. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  3. 【POJ 3140】 Contestants Division(树型dp)

    id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  4. 【POJ 2486】 Apple Tree(树型dp)

    [POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Acce ...

  5. 【49.23%】【hdu 1828】Picture

    Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  6. 【设计模式 - 23】之模版方法模式(Template)

    1      模式简介 模版方法模式的定义: 模版方法模式在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模版方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤. 模版方法模 ...

  7. 【20.23%】【codeforces 740A】Alyona and copybooks

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 【30.23%】【codeforces 552C】Vanya and Scales

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 【16.23%】【codeforces 586C】Gennady the Dentist

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. angularjs SyntaxError: Unexpected token  in JSON at position 0

    使用NodeJs读取json格式的文件,转换成对象时报错 :SyntaxError: Unexpected token in JSON at position 0,这个问题查了两三个小时,记录一下解决 ...

  2. php语言实现的7种基本的排序方法

    今天总结了一下常用的7种排序方法,并用php语言实现. 直接插入排序 /* * 直接插入排序,插入排序的思想是:当前插入位置之前的元素有序, * 若插入当前位置的元素比有序元素最后一个元素大,则什么也 ...

  3. git 常用指令

    下载项目 git clone https://git.oschina.net/jianqingwang/jianblog.git 注意,clone跟的是项目地址 查看分支(也就是版本) git bra ...

  4. 《Ext JS模板与组件基本知识框架图----模板》

    最近在整理Ext JS的模板和组件,在参考<Ext JS权威指南>,<Ext JS Web应用程序开发指南>,<Ext JS API>等相关书籍后才写下这篇< ...

  5. PHP 命名空间(namespace)

    PHP 命名空间(namespace) PHP 命名空间(namespace)是在PHP 5.3中加入的,如果你学过C#和Java,那命名空间就不算什么新事物. 不过在PHP当中还是有着相当重要的意义 ...

  6. jsp读取properties文件

    jsp读取properties文件 jsp中读取properties文件,并把值设到js变量中: mpi.properties文件内容: MerchantID=00000820 CustomerEMa ...

  7. SQL Queries from Transactional Plugin Pipeline

    Sometimes the LINQ, Query Expressions or Fetch just doesn't give you the ability to quickly query yo ...

  8. GitHub学习心得之 简单操作

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 前言 本文对Github的基本操作进行了总结, 主要基于以下文章: http://gitre ...

  9. Atitit。数据库 安全性 重要敏感数据加密存储解决方案

    Atitit.数据库 安全性 重要敏感数据加密存储解决方案 1.1. 加密存储的重要性1 1.2. 使用的加密算法aes1 1.3. 数据加密以后会有一些问题.1 1.3.1. 一个是统计,比如统计资 ...

  10. java中文件的I/O操作

    java中文件的读写操作 (一) (1)java中文件的字节转成字符读操作 FileInputStream fStream = new FileInputStream("test.txt&q ...