A8094 | Two out of Three
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Vasya has recently developed a new algorithm to optimize the reception of customer flow and he considered the following problem.
Let the queue to the cashier contain $n$ people, at that each of them is characterized by a positive integer $a_{i}$ — that is the time needed to work with this customer. What is special about this very cashier is that it can serve two customers simultaneously. However, if two customers need $a_{i}$ and $a_{j}$ of time to be served, the time needed to work with both of them customers is equal to $max(a_{i},a_{j})$ . Please note that working with customers is an uninterruptable process, and therefore, if two people simultaneously come to the cashier, it means that they begin to be served simultaneously, and will both finish simultaneously (it is possible that one of them will have to wait).
Vasya used in his algorithm an ingenious heuristic — as long as the queue has more than one person waiting, then some two people of the first three standing in front of the queue are sent simultaneously. If the queue has only one customer number $i$ , then he goes to the cashier, and is served within $a_{i}$ of time. Note that the total number of phases of serving a customer will always be equal to $⌈n/2⌉$ .
Vasya thinks that this method will help to cope with the queues we all hate. That's why he asked you to work out a program that will determine the minimum time during which the whole queue will be served using this algorithm.
Let the queue to the cashier contain $n$ people, at that each of them is characterized by a positive integer $a_{i}$ — that is the time needed to work with this customer. What is special about this very cashier is that it can serve two customers simultaneously. However, if two customers need $a_{i}$ and $a_{j}$ of time to be served, the time needed to work with both of them customers is equal to $max(a_{i},a_{j})$ . Please note that working with customers is an uninterruptable process, and therefore, if two people simultaneously come to the cashier, it means that they begin to be served simultaneously, and will both finish simultaneously (it is possible that one of them will have to wait).
Vasya used in his algorithm an ingenious heuristic — as long as the queue has more than one person waiting, then some two people of the first three standing in front of the queue are sent simultaneously. If the queue has only one customer number $i$ , then he goes to the cashier, and is served within $a_{i}$ of time. Note that the total number of phases of serving a customer will always be equal to $⌈n/2⌉$ .
Vasya thinks that this method will help to cope with the queues we all hate. That's why he asked you to work out a program that will determine the minimum time during which the whole queue will be served using this algorithm.
输入格式
The first line of the input file contains a single number $n$ ( $1<=n<=1000$ ), which is the number of people in the sequence. The second line contains space-separated integers $a_{1},a_{2},...,a_{n}$ ( $1<=a_{i}<=10^{6}$ ). The people are numbered starting from the cashier to the end of the queue.
输出格式
Print on the first line a single number — the minimum time needed to process all $n$ people. Then on $⌈n/2⌉$ lines print the order in which customers will be served. Each line (probably, except for the last one) must contain two numbers separated by a space — the numbers of customers who will be served at the current stage of processing. If $n$ is odd, then the last line must contain a single number — the number of the last served customer in the queue. The customers are numbered starting from $1$ .
输入输出样例
输入 #1
4 1 2 3 4
输出 #1
6 1 2 3 4
输入 #2
5 2 4 3 1 4
输出 #2
8 1 3 2 5 4
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted