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 ...
随机推荐
- UMLUnified Modeling Language (UML)又称统一建模语言或标准建模语言
1.用例图(use case diagram) 2.活动图(activity diagram) 3.静态结构图 4.顺序图(Sequence Diagram):时序图 5.交互纵览图(Interact ...
- hydra爆破用法
-R 根据上一次进度继续破解 -S 使用SSL协议连接 -s 指定端口 -l 指定用户名 -L 指定用户名字典(文件) -p 指定密码破解 -P 指定密码字典(文件) -e 空密码探测和指定用户密码探 ...
- Android Studio系列教程五--Gradle命令详解与导入第三方包
Android Studio系列教程五--Gradle命令详解与导入第三方包 2015 年 01 月 05 日 DevTools 本文为个人原创,欢迎转载,但请务必在明显位置注明出处!http://s ...
- 用CSS3实现上下左右箭头
225deg 向上箭头 135deg向下箭头45deg向右箭头 -45deg向左箭头
- Java 数据类型和变量
1.1 基本类型与引用类型的区别 1.基本类型代表简单的数据类型,比如整数和字符,引用类型所引用的实例能表示任意一种复杂的数据类型. 2.基本类型仅表示数据类型,而引用类型所引用的实例除了表示复杂数据 ...
- 我的WCF摸爬滚打之路(2)
昨天抽空写了一个wcf的创建和宿主程序的创建文章,下面也有很多园友给了评论,在此谢谢大家给了我继续记录我的摸爬滚打之路信心……抱拳! 上次的文章<我的WCF摸爬滚打之路(1)>中写到,在测 ...
- Easyui Combotree问题及其相关
Easyui的setValue方法无效 今天在开发项目的时候,遇到第一个很奇怪的问题:EasyUI的setValue方法无效. $('#department_parent').combotree('s ...
- AS2.0大步更新 Google强势逆天
New Features in Android Studio 2.0Instant Run: Faster Build & Deploy逆天吗?你还在羡慕iOS的playground吗?And ...
- JAVA NIO是什么(zz)
JAVA NIO是什么? 1. 基本 概念 IO 是主存和外部设备 ( 硬盘.终端和网络等 ) 拷贝数据的过程. IO 是操作系统的底层功能实现,底层通过 I/O 指令进行完成. 所有语言运行时系 ...
- LeetCode:Populating Next Right Pointers in Each Node I II
LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...