ACdream 1429 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 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
4
1 2 3 5
4
1 2 4 8
4
1 1 1 1
Sample Output
3
0 0
1 0
1 1
3 1
3 2
0 2
0
4
0 0
1 0
1 1
2 1
2 -2
1 -2
1 -1
0 -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.
Source
第一问:求最多使用木棍数量。竖边可以无视,问题简化成从所有木棍中挑出一部分,分成总长度相等的两组,求最大总长度。动规可解。
第二问:具体摆法。由于是多解任意输出一解,竖边依旧可以无视。在之前的动规中保存一下每次用的木棍长度(我保存的是端点间相对位置),最后输出就行。
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int q=;
const int mxn=;
const int bas=;
int le,ri;
int a[mxn];
int dp[mxn][bas*],p[mxn][bas*];
int n;
int st1[mxn],tp1;
int st2[mxn],tp2; int main(){
while(scanf("%d",&n)!=EOF){
memset(dp,-,sizeof(dp));
memset(p,,sizeof(p));
int i,j;
le=ri=bas;
for(i=;i<=n;i++)scanf("%d",&a[i]);
dp[][bas]=;
p[][bas]=bas;
for(i=;i<=n;i++){
for(j=le;j<=ri;j++){//枚举长度差
if(dp[i-][j]<=-)continue;
if(dp[i][j]<dp[i-][j]){
dp[i][j]=dp[i-][j];//不选
p[i][j]=j;
}
if(dp[i][j+a[i]]<dp[i-][j]+)//放在上面
{
dp[i][j+a[i]]=dp[i-][j]+;
p[i][j+a[i]]=j;
}
if(dp[i][j-a[i]]<dp[i-][j]+)//放在下面
{
dp[i][j-a[i]]=dp[i-][j]+;
p[i][j-a[i]]=j;
}
}
le-=a[i];//扩展规划范围
ri+=a[i];
}
printf("%d\n",dp[n][bas]);//回答第一问:最多使用木棍数量
int tnow=bas;//最终端点位置
int tmp;
tp1=;tp2=;
for(i=n;i>=;i--){//倒着找
tmp=p[i][tnow];//上次木棍端点位置
// printf("test: %d %d\n",tmp,tnow);
if(tmp>tnow)st2[++tp2]=tmp-tnow;
if(tmp<tnow)st1[++tp1]=tnow-tmp;
tnow=tmp;//更新位置
}
int x=,y=;
while(tp2)
{
y++;
printf("%d %d\n",x,y);
x+=st2[tp2];
printf("%d %d\n",x,y);
tp2--;
}
while(tp1)
{
y++;
printf("%d %d\n",x,y);
x-=st1[tp1];
printf("%d %d\n",x,y);
tp1--;
}
}
return ;
}
ACdream 1429 Rectangular Polygon的更多相关文章
- 【ASC 23】G. ACdream 1429 Rectangular Polygon --DP
题意:有很多棍子,从棍子中选出两个棍子集合,使他们的和相等,求能取得的最多棍子数. 解法:容易看出有一个多阶段决策的过程,对于每个棍子,我们有 可以不选,或是选在第一个集合,或是选在第二个集合 这三种 ...
- HDU 5452——Minimum Cut——————【树链剖分+差分前缀和】ACdream 1429——Diversion——————【树链剖分】
Minimum Cut Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Tota ...
- UVALive 3959 Rectangular Polygons (排序贪心)
Rectangular Polygons 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/G Description In thi ...
- Shapely中的几何图形操作
Geometric Objects object.area Returns the area (float) of the object. object.bounds Returns a (minx, ...
- postgis几何操作函数集
管理操作函数 AddGeometryColumn - Adds a geometry column to an existing table of attributes. By default use ...
- Recover Polygon (easy)
Recover Polygon (easy) The zombies are gathering in their secret lair! Heidi will strike hard to des ...
- [LeetCode] Convex Polygon 凸多边形
Given a list of points that form a polygon when joined sequentially, find if this polygon is convex ...
- hdu 1429
http://acm.hdu.edu.cn/showproblem.php?pid=1429 一个广搜的简单题吧,不过有意思的事这个题目用到了位运算,还有就是很恶心的MLE #include < ...
- 结合谷歌地图多边形(polygon)与Sql Server 2008的空间数据类型计算某个点是否在多边形内的注意事项
首先在利用 GEOGRAPHY::STPolyFromText(@GeoStr, 4326) 这样的函数把字符串转换为Geography类型时,字符串里经纬度的顺序是 “经度[空格]纬度”,即“lon ...
随机推荐
- esc安装数据库 sqlserver mssql
在服务器里面设置虚拟内存为:2G以上就可以安装了
- iOS下使用SHA1WithRSA算法加签源码
首先了解一下几个相关概念,以方便后面遇到的问题的解决: RSA算法:1977年由Ron Rivest.Adi Shamirh和LenAdleman发明的,RSA就是取自他们三个人的名字.算法基于一个数 ...
- 了解了这些才能开始发挥jQuery的威力(转)
链接:http://www.cnblogs.com/dolphinX/archive/2013/10/08/3347677.html 由于当前jQuery如此的如雷贯耳,相信不用介绍什么是jQuery ...
- main函数中argc和argv含义
在main函数中经常可以看到int main(int argc, char ** argv)的函数头,这里的形参int argc, char ** argv究竟是啥含义呢? &1 int ar ...
- LeetCode:Search in Rotated Sorted Array I II
LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...
- Win7下手动卸载oracle 11G
由于安装过程中遇到的之前提到的那个问题,http://www.cnblogs.com/shenliang123/p/3141886.html 知道解决方法后,也只能将oracle重新卸载后再进行安装 ...
- brew-cask 之本地更新 node
本文同步自我的个人博客:http://www.52cik.com/2015/11/04/brew-cask-local.html 今天 Node v4.2.2 (LTS) 发布,什么是 LTS 呢,百 ...
- 运用Java对微信公众平台二次开发技术——开发者模式接入
当初我在这碰到了很多问题,市面上以及网络上的资料特别少,所以当初碰了很多壁,所以现在跟大家分享一下,如何用Java,对微信公众平台进行二次开发. 一.开发预备知识: 最基本的JavaSE与JavaWe ...
- 风清杨之Oracle的安装与说明
1.Oracle官网与下载地址 Oracle中文官网:http://www.oracle.com/cn/index.html Oracle中文官网下载:http://www.oracle.com/te ...
- State Pattern -- 状态模式原理及实现(C++)
主要参考<大话设计模式>和<设计模式:可复用面向对象软件的基础>两本书.本文介绍命令模式的实现. 问题出发点 在实际开发中,我们经常会遇到这种情况:一个对象有多种状态,在每一个 ...