A1234 | [COCI-2011_2012-contest4]#5 OGRADA
来源COCI
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
In Mirko’s village all fences are made of exactly N boards with different heights. Mirko doesn’t have his own fence yet, so he decided to build one.
Each board is represented by a positive integer less than 109 - it’s height. We define the niceness of the fence as a sum of height differences between adjacent boards.
Mirko already bought the boards, but he doesn’t know how to order them into a fence. He would like his fence to be similar to Slavko’s fence, but also to be as nice as possible.
We say that two fences are similar if ordering of adjacent boards is the same in both fences, i.e. if i-th board of one fence is smaller (or larger) than (i+1)-st, than the same must hold for that boards of the other fence.
Given Slavko’s fence configuration, and heights of boards that Mirko bought, put Mirko’s fence together so that it is similar to Slavko’s but also as nice as possible. If there is more than one solution, output any of them.
Each board is represented by a positive integer less than 109 - it’s height. We define the niceness of the fence as a sum of height differences between adjacent boards.
Mirko already bought the boards, but he doesn’t know how to order them into a fence. He would like his fence to be similar to Slavko’s fence, but also to be as nice as possible.
We say that two fences are similar if ordering of adjacent boards is the same in both fences, i.e. if i-th board of one fence is smaller (or larger) than (i+1)-st, than the same must hold for that boards of the other fence.
Given Slavko’s fence configuration, and heights of boards that Mirko bought, put Mirko’s fence together so that it is similar to Slavko’s but also as nice as possible. If there is more than one solution, output any of them.
输入格式
The first line of input contains integer N (2 ≤ N ≤ 300 000), number of boards in each fence.
The following line conatins N different positive integers representing Slavko’s fence.
The following line contains N different positive integers representing heights of boards Mirko bought for his fence.
The following line conatins N different positive integers representing Slavko’s fence.
The following line contains N different positive integers representing heights of boards Mirko bought for his fence.
输出格式
The first line of output should contain the maximum possible niceness of Mirko’s fence.
The following line should contain N integers, Mirko’s boards in optimal order as described.
The following line should contain N integers, Mirko’s boards in optimal order as described.
输入输出样例
输入 #1
4 5 7 4 9 1 2 3 4
输出 #1
7 2 4 1 3
输入 #2
10 9 5 1 2 6 7 4 18 20 12 10 40 20 30 50 70 80 100 1000 500
输出 #2
3010 100 80 10 40 50 1000 20 70 500 30
50% of the points for test case will be awarded if niceness is correct, but second line is incorrect or
not outputed at all.
First sample description: Mirko bought boards of heights 1, 2, 3 and 4. Fences similar to Slavko’s
that he can build are:
{1,3,2,4} - niceness 2+1+2=5
{1,4,2,3} - niceness 3+2+1=6
{2,3,1,4} - niceness 1+2+3=6
{2,4,1,3} - niceness 2+3+2=7
{3,4,1,2} - niceness 1+3+1=5
not outputed at all.
First sample description: Mirko bought boards of heights 1, 2, 3 and 4. Fences similar to Slavko’s
that he can build are:
{1,3,2,4} - niceness 2+1+2=5
{1,4,2,3} - niceness 3+2+1=6
{2,3,1,4} - niceness 1+2+3=6
{2,4,1,3} - niceness 2+3+2=7
{3,4,1,2} - niceness 1+3+1=5
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted