Problem G. Rectangular Polygon(dp + 转移重现)

『杭电1888』Rectangular Polygons Submit 阅读详情
2006-2007 Winter Petrozavodsk Camp, Andrew Stankevich Contest 23 (ASC 23)

Problem G. Rectangular Polygon
Input file: polygon.in
Output file: polygon.out
Time limit: 1 second
Memory limit: 64 megabytes
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
The first line of the input file contains n — the number of sticks (1 ≤ n ≤ 100). The second line contains
n integer 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 first line of the output file. The following 2l lines
must contain the vertices of the rectangular polygon Johnny can construct. Vertices must be listed in
order of traversal. The first two vertices must be the ends of a horizontal edge. If there are several
solution, output any one. Vertex coordinates must not exceed 109
.
If no polygon can be constructed, output l = 0.
Example
polygon.in polygon.out
4
1 2 3 5
3
0 0
1 0
1 1
3 1
3 2
0 2
4
1 2 4 8
0
4
1 1 1 1
4
0 0
1 0
1 1
2 1
2 -2
1 -2
1 -1
0 -1
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.

题意等价于:给你n个砝码,第i个砝码的重量为wi,问:最多选多少个砝码使天平平衡。
#include<bits/stdc++.h>
using namespace std;

const int maxn = 4e4 + 5;


struct X{
    int pre;     当前的状态时由上一个哪个状态转移来的
    int sum;
}dp[105][maxn];  dp[i][j].sum : 前 i 个砝码,偏移度为 j 最多拿多少个砝码。

                 若左边秤更重时 j(向左偏移 j 重量)就为负数,若右边秤更重时j(向左偏移 j 重量)为正数,
                 若天平平衡时 j 为0。

                 因为数组的 j 不能为负数,所以就整体向右平移20000,天平平衡时 j 为20000。

                 因为n最大为100,每个砝码最大为200,如果都放在左边 j 就为-20000。

                 所以最后答案为:dp[n][20000];

int w[105];      砝码的重量。
int l[105];      放左边砝码的重量。
int r[105];      放右边砝码的重量。

void Init(int n){初始化
    for(int i = 0;i <= n;i++){
        for(int j = 0;j <= 40000;j++){
            dp[i][j].sum = -100000000;
        }
    }
    dp[0][20000].sum = 0;
}

int main(){
    freopen("polygon.in","r",stdin);
    freopen("polygon.out","w",stdout);
    int n;
    scanf("%d",&n);
    for(int i = 1;i <= n;i++) scanf("%d",&w[i]);
    Init(n);
    for(int i = 1;i <= n;i++){
        for(int j = 0;j <= 40000;j++){
            if(j >= w[i] && j + w[i] <= 40000){
                int a,b,c,M;
                a = dp[i - 1][j - w[i]].sum + 1;//第 i 个砝码放右边。
                b = dp[i - 1][j + w[i]].sum + 1;//第 i 个砝码放左边。
                c = dp[i - 1][j].sum;           //第 i 个砝码不放。
                M = max(a,max(b,c));
                if(M == a) dp[i][j].pre = j - w[i];
                else if(M == b) dp[i][j].pre = j + w[i];
                else if(M == c) dp[i][j].pre = j;
                dp[i][j].sum = M;
            }
            else if(j >= w[i]){
                int a,b,M;
                a = dp[i - 1][j - w[i]].sum + 1;//第 i 个砝码放右边。
                b = dp[i - 1][j].sum;           //第 i 个砝码不放。
                M = max(a,b);
                if(M == a) dp[i][j].pre = j - w[i];
                else if(M == b) dp[i][j].pre = j;
                dp[i][j].sum = M;
            }
            else if(j + w[i] <= 40000){
                int a,b,M;
                a = dp[i - 1][j + w[i]].sum + 1;//第 i 个砝码放右边。
                b = dp[i - 1][j].sum;           //第 i 个砝码不放。
                M = max(a,b);
                if(M == a) dp[i][j].pre = j + w[i];
                else if(M == b) dp[i][j].pre = j;
                dp[i][j].sum = M;
            }
            else{
                dp[i][j].sum = dp[i - 1][j].sum;//第 i 个砝码不放。
                dp[i][j].pre = j;
            }
        }
    }

    int ll = 0,rr = 0,tot = 20000;//把左右边秤的砝码挑出来。
    for(int i = n;i >= 1;i--){
        int ans = dp[i][tot].pre - tot;
        if(ans > 0) l[++ll] = ans; 
        else if(ans < 0) r[++rr] = -ans;
        tot = dp[i][tot].pre;
    }

    printf("%d\n",dp[n][20000].sum);
    if(dp[n][20000].sum != 0){//选的砝码的个数不为0就输出。
        int x,y;
        x = l[1],y = 0;
        printf("0 0\n");
        printf("%d %d\n",x,y);
        for(int i = 2;i <= ll;i++){
            y++;
            printf("%d %d\n",x,y);
            x += l[i];
            printf("%d %d\n",x,y);
        }
        y++;
        printf("%d %d\n",x,y);
        x -= r[1];
        printf("%d %d\n",x,y);
        for(int i = 2;i <= rr;i++){
            y++;
            printf("%d %d\n",x,y);
            x -= r[i];
            printf("%d %d\n",x,y);
        }
    }
}
AI赚钱新思路 利用RPA+KIMI实现小红书全自动制作发布(详细攻略) 我已经事先准备好了文案素材,现在就利用Kimi的智能,根据我们已有的文案来创作出新的文案吧。之前我写过一篇关于【:一个戴着用百元大钞制成的简约头饰的少女,她坐在小车里,手里拿着一叠港元,整个画面洋溢着现实主义动漫风格和治愈系的温暖气息。通过这篇文章,我们已经一步步地完成了小红书治愈系文案的创作、治愈系图片的生成,以及笔记的发布,实现了整个流程的自动化。不慌不忙,快乐常在 在繁忙的日常中,给自己的心灵留一片净土,感受内心深处的平静与安宁。接下来,我将展示如何使用影刀来编写自动化脚本,以生成治愈系的图文图片。 阅读详情

相关推荐

保姆级教程:在RV1126开发板上跑通YOLOv5口罩检测(从数据集到摄像头实时识别)

本文提供了一份详细的RV1126开发板部署YOLOv5口罩检测系统的全流程指南,涵盖数据集准备、模型训练优化、边缘部署及实时摄像头识别等关键步骤。重点解析了RV1126开发板的性能特征、YOLOv5模型训练调优技巧、RKNN模型转换量化策略,以及部署过程中的性能优化方案,帮助开发者高效实现边缘端目标检测应用。

weixin_27310985的博客 116

POJ 3135 Polygons on the Grid 笔记

已知 n 根木棍的长度,求取多任意木棍连接成凸多边形的最大面积。

woniupengpeng的专栏 256

ORB-SLAM3复现的详细过程——配置安装及ROS和脚本运行---Ubuntu20.04

所提供的图像具有高动态范围,事先进行了光度标定,用20Hz的帧率采集,分辨率为1024像素*1024像素;需要下载数据集dataset-room1_512_16和rgbd_dataset_freiburg1_desk。在一键安装ROS后,只需要再安装Pangolin即可,其他所有的需要都已经存在。用微型飞行器采集的视觉惯性数据集,包含双目相机和IMU同步测量数据及位姿的真值。第二条命令中的${ROS_PACKAGE_PATH}是自己文件的路径。其采用的相机和IMU传感器,在硬件上进行了时间同步。

琉璃轻纱的博客 1万+

allegro 17.4 中挖铜的操作

allegro 17.4 中挖铜的操作

wtlh1的博客 2395

tanner2019安装教程

tanner2019安装教程 tanner安装包 软件由老师提供,但由于老师给的方法不知怎么回事无法使用就自己找了一些方法,本教程仅提供参考 链接:https://pan.baidu.com/s/1A2O6JuEF8YFbe8bPnD3J6w 提取码:ysif 复制这段内容后打开百度网盘手机App,操作更方便哦 安装过程 1.选择TannerTools_v2019.2-Win64.exe,进行安装。 2.选择安装产品 3.选择目标路径 4.确认安装 5.安装成功,退出。 获取许可过程 1.打开key压缩

weixin_47730560的博客 1万+

相机光学(三十八)——VCM(Voice Coil Motor)音圈马达

VCM(Voice Coil Motor)音圈马达全称Voice Coil Montor,电子学里面的音圈电机,是马达的一种。因为原理和扬声器类似,所以叫音圈电机,具有高频响、高精度的特点。其主要原理是在一个永久磁场内,通过改变马达内线圈的直流电流大小,来控制弹簧片的拉伸位置,从而带动上下运动。手机摄像头广泛的使用VCM实现自动对焦功能,通过VCM可以调节镜头的位置,呈现清晰的图像。弹片式结构滚珠式结构摩擦式结构Open loop开马达Close loop闭环马达。

图像学习之旅 7260

Andrew Stankevich Contest 23 G - Rectangular Polygon

G - Rectangular Polygon Time Limit: 4000/2000MS (Java/Others) Memory Limit: 512000/256000KB (Java/Others)     Special Judge SubmitStatus Problem Description       A rectangular polygon i

smz436487的专栏 787

如何使用MCP2518FD外部CAN FD控制器实现速速CAN通信

1、SPI时序MCP2518FD外部CAN FD控制器具有小尺寸和SPI接口,可实现轻松连接。可将CAN FD通道轻松添加到缺少CAN FD外设或没有所需CAN FD通道的微控制器上。MCP2518FD支持经典格式 (CAN 2.0B) 和CAN灵活数据速率 (CAN FD) 格式中的CAN帧格式,符合ISO11898-1:2015标准。框图:本文实验板MCU:CAN FD控制器:MCP2518FDCAN FD收发器:ATA6560开发环境:想了解开发板更详细信息,请点击。

qq_57798018的博客 1万+

ACdream 1429 Rectangular Polygon

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

Occult 499

Tessy集成测试,看完这篇就够了

本文详细介绍了集成测试模块的创建与测试流程。首先,通过TESSY模块进行组件测试管理,设置测试环境并配置相关函数,包括外部函数和组件函数的处理。接着,设计测试用例,包括设置工作任务、增加测试用例、为测试场景添加函数及赋值,并执行测试用例。此外,文章还介绍了集成测试覆盖率的概念,包括CPC(调用对覆盖率)和FC(函数覆盖率)的定义与作用,强调了它们在确保测试充分性和发现潜在问题中的重要性。最后,提到了数据共享的注意事项,指出在测试过程中全局变量的共享特性及桩函数调用的处理方式。

qq_37282365的博客 1105

ZYNQ系列PS侧和PL侧DDR3或DDR4在AD(Altium Designer 25)中的layout布线规则约束

DDR3或DDR4的PCB布线规则通常有相对固定的要求,对应的,在PCB设计时有固定的约束设置。

FPGA艺术家的博客 803

平常水题 - Sheldon Numbers(逆向思维,位运算)

Sheldon Numbers According to Sheldon Cooper, the best number is 73. In his own words, “The best number is 73. 73 is the 21st prime number. Its mirror, 37, is the 12th, and its mirror, 21, is the pr

dreams___的博客 1181

使用 MATLAB/Simulink 对风光互补发电系统进行建模

随着全球对清洁能源的需求日益增长,风力发电和光伏发电作为两种主要的可再生能源形式,在电力供应中的占比越来越高。然而,由于风电和光伏的间歇性和不确定性,单独依赖其中任何一种能源都会给电网带来挑战。因此,风光互补发电系统(Wind-PV Hybrid System)应运而生,通过合理配置风力发电机和光伏电池板的数量,可以有效提高系统的稳定性和可靠性。此外,为了确保整个电力系统的安全稳定运行,还需要研究风光互补发电系统与主网之间的协调调度策略。本教程将详细介绍如何使用。

amy_mhd的博客 124

AT - Grand - 004 - B - Colorful Slimes(思维理解)

B - Colorful Slimes Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Statement Snuke lives in another world, where slimes are real creatures and kept by some people. Slimes come...

dreams___的博客 936

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

平常水题 - 蚂蚁(栈的使用)

蚂蚁 Time limit per test: 0.5 seconds Time limit all tests: 5.0 seconds Memory limit: 256 megabytes 水平线上有 N 只蚂蚁,每只蚂蚁的位置及大小均不同。他们沿着 X 轴爬行,有的向左,有的向右,爬行的速度是一样的,两只蚂蚁相遇大一点的会吃掉小的。 现在从左到右给出每只蚂蚁的大小和爬行的

dreams___的博客 891

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

平常水题 - J. Architect of Your Own Fortune (二分图匹配)

J. Architect of Your Own Fortune Vasya is a schoolboy who rides a trolley bus and then a bus to get to school. He’s always very happy to get a “lucky ticket,” which means the total of the first thre

dreams___的博客 880

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

寒武纪重现(HDU)- 1008(6261) - Rikka with Mutex(二分 + 错误总结)

Rikka with Mutex Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 32 Accepted Submission(s): 12 Problem Description Sometimes, technica...

dreams___的博客 857

平常水题 - Atcoder 058 - C - 怪文書 / Dubious Document(字符串的处理)

C - 怪文書 / Dubious Document                                                       Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Snuke loves "paper cutting": he c

dreams___的博客 808

CF - div2 - 459 - C - The Monster

C. The Monster time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output As Will is stuck in the Upside Down, he can still communicate with his mom,

dreams___的博客 794

D. Almost Identity Permutations(dp)

D. Almost Identity Permutations time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output A permutation p of size n is an array such that every inte

dreams___的博客 773

2017中国大学生程序设计竞赛-哈尔滨站 (B.尺取 + 二分)

Problem Description Alice are given an array A[1..N] with N numbers.Now Alice want to build an array B by a parameter K as following rules:Initially, the array B is empty. Consider each interval in ar

dreams___的博客 740

AtCoder - Petrozavodsk - Contest - 001 - D (缩点 + 贪心 + 错误总结)

D - Forest Time limit : 2sec / Memory limit : 256MB Score : 600 points Problem Statement You are given a forest with N vertices and M edges. The vertices are numbered 0 through N−1. The edges are

dreams___的博客 694

B. Preparing for Merge Sort(二分)

2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)B. Preparing for Merge Sort time limit per test2 seconds memory limit per

dreams___的博客 665

平常水题 - atcoder 062 - D - 3N Numbers

D - 3N Numbers Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement Let N be a positive integer. There is a numerical sequence of length 3N, a=(a1,a2,…,a3N). Snuke

dreams___的博客 639

平常水题 - CodeForces - 703C(二分 + 几何)

C. Chris and Road time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output And while Mishka is enjoying her trip...

dreams___的博客 627

平常水题 - CodeForces - 222B

The Free Meteor Association (FMA) has got a problem: as meteors are moving, the Universal Cosmic Descriptive Humorous Program (UCDHP) needs to add a special module that would analyze this movement.

dreams___的博客 600
上一篇: 内存分析
下一篇: dfs序
dwenhu___
博客等级 码龄10年 8粉丝 62原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值