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

      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 10 9
.      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

Andrew Stankevich Contest 23

  第一问:求最多使用木棍数量。竖边可以无视,问题简化成从所有木棍中挑出一部分,分成总长度相等的两组,求最大总长度。动规可解。

  第二问:具体摆法。由于是多解任意输出一解,竖边依旧可以无视。在之前的动规中保存一下每次用的木棍长度(我保存的是端点间相对位置),最后输出就行。

 #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的更多相关文章

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

    题意:有很多棍子,从棍子中选出两个棍子集合,使他们的和相等,求能取得的最多棍子数. 解法:容易看出有一个多阶段决策的过程,对于每个棍子,我们有 可以不选,或是选在第一个集合,或是选在第二个集合 这三种 ...

  2. HDU 5452——Minimum Cut——————【树链剖分+差分前缀和】ACdream 1429——Diversion——————【树链剖分】

    Minimum Cut Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Tota ...

  3. UVALive 3959 Rectangular Polygons (排序贪心)

    Rectangular Polygons 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/G Description In thi ...

  4. Shapely中的几何图形操作

    Geometric Objects object.area Returns the area (float) of the object. object.bounds Returns a (minx, ...

  5. postgis几何操作函数集

    管理操作函数 AddGeometryColumn - Adds a geometry column to an existing table of attributes. By default use ...

  6. Recover Polygon (easy)

    Recover Polygon (easy) The zombies are gathering in their secret lair! Heidi will strike hard to des ...

  7. [LeetCode] Convex Polygon 凸多边形

    Given a list of points that form a polygon when joined sequentially, find if this polygon is convex ...

  8. hdu 1429

    http://acm.hdu.edu.cn/showproblem.php?pid=1429 一个广搜的简单题吧,不过有意思的事这个题目用到了位运算,还有就是很恶心的MLE #include < ...

  9. 结合谷歌地图多边形(polygon)与Sql Server 2008的空间数据类型计算某个点是否在多边形内的注意事项

    首先在利用 GEOGRAPHY::STPolyFromText(@GeoStr, 4326) 这样的函数把字符串转换为Geography类型时,字符串里经纬度的顺序是 “经度[空格]纬度”,即“lon ...

随机推荐

  1. TestLink学习七:TestLink测试用例Excel转换XML工具

    TestLink对于测试用例的管理来说,是蛮强大的,但是在导入导出这块,功能有点弱,本文针对测试用例的导入,转载了一个Excel转换成xml工具. 1.根据到处的测试用例xml,定义一下我的Excel ...

  2. java 21 - 13 IO流之 合并流

    SequenceInputStream :表示其他输入流的逻辑串联. 构造方法摘要 SequenceInputStream(Enumeration<? extends InputStream&g ...

  3. IO流的练习1 —— 随机获取文本中的信息

    需求:一个文本中有几个名字,随机从中获取一个名字 分析: A:首先把文本中的数据读出 B:再把数据存储到集合中 C:产生一个随机的索引 D:打印出这个索引对应的值 public static void ...

  4. 内联(行级)元素不能设置margin-top

    内联(行级)元素 不能设置宽高,但padding属性可以设置,需要注意的是行级元素不能设置margin-top和margin-bottom属性,但可以设置margin-left和margin-righ ...

  5. BIO、NIO与NIO.2的区别与联系

    BIO.NIO.NIO.2之间的区别主要是通过同步/异步.阻塞/非阻塞来进行区分的 同步: 程序与操作系统进行交互的时候采取的是问答的形式 异步: 程序与操作系统取得连接后,操作系统会主动通知程序消息 ...

  6. 动态调用webservice,不需要添加Web References

    using System; using System.Collections.Generic; using System.Web; using System.Net; using System.IO; ...

  7. DWM 窗体玻璃效果实现

    我一直盼望着 Windows 新版本的发布.令人感兴趣的事情莫过于浏览 MSDN® 和 SDK 文档,查找一些可以利用和依赖的最新创新,然后让朋友和同事以及您的老板(如果幸运的话)大开眼界.Windo ...

  8. Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  9. textfield控制光标开始位置

    //    UIView *paddingView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.wi ...

  10. iOS适配HTTPS,创建一个自签名的SSL证书(x509)具体步骤

    引言(创建生成的证书只能用于测试使用.如果想使用自签名证书就只能以自己为 CA机构颁发证书,进行双向认证才能使用) 使用HTTP(超文本传输)协议访问互联网上的数据是没有经过加密的.也就是说,任何人都 ...