CF82D Two out of Three
题目描述
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 n n people, at that each of them is characterized by a positive integer ai a_{i} ai — 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 ai a_{i} ai and aj a_{j} aj of time to be served, the time needed to work with both of them customers is equal to max(ai,aj) max(a_{i},a_{j}) max(ai,aj) . 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 i i , then he goes to the cashier, and is served within ai a_{i} ai of time. Note that the total number of phases of serving a customer will always be equal to ⌈n/2⌉ ⌈n/2⌉ ⌈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 n n ( 1<=n<=1000 1<=n<=1000 1<=n<=1000 ), which is the number of people in the sequence. The second line contains space-separated integers a1,a2,...,an a_{1},a_{2},...,a_{n} a1,a2,...,an ( 1<=ai<=106 1<=a_{i}<=10^{6} 1<=ai<=106 ). 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 n n people. Then on ⌈n/2⌉ ⌈n/2⌉ ⌈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 n 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 1 .
题意翻译
一队顾客排在一位收银员前面。他采取这样一个策略:每次,假如队伍有至少两人,就会从前面的前三人(如果有)中选取两位一起收银,所花费的时间为这两人单独收银所需时间的最大值。如果只有两人,那么一起收银;如果只有一人,那么单独收银。请问所需的总时间最少是多少?
输入输出样例
4
1 2 3 4
6
1 2
3 4
5
2 4 3 1 4
8
1 3
2 5
4
考虑dp
发现不好搞,在不同的情况下选择不同的数会造成不同的影响
考虑状态的设计,发现对于不同的情况,不一样的其实是当前3个数和选到了第几个数
可以在状态中记录一下目前3个数,发现只用记录前面留下的即可
设f[i][j]表示选到了第i个数,前面留下的是第j个数时的最优解
然后就没什么了
转移应该是很显而易见的
code
//¼ÓÓÍ
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int f[1001][1001],n,a[1001],out[1001][1001][3];
inline ll read()
{
char c=getchar();ll a=0,b=1;
for(;c<'0'||c>'9';c=getchar())if(c=='-')b=-1;
for(;c>='0'&&c<='9';c=getchar())a=a*10+c-48;
return a*b;
}
inline void az(int i,int j,int x,int y,int z){out[i][j][0]=x;out[i][j][1]=y;out[i][j][2]=z;}
void prout(int x,int y)
{
if(y==0)return;
prout(x-1,out[x][y][2]);
if(out[x][y][0]<=n)
{
cout<<out[x][y][0]<<' ';
}
if(out[x][y][1]<=n)
{
cout<<out[x][y][1]<<' ';
}
cout<<endl;
}
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
n=read();
for(int i=1;i<=n;i++)
{
a[i]=read();
}
memset(f,0x3f,sizeof(f));
f[1][1]=max(a[2],a[3]);f[1][2]=max(a[1],a[3]);f[1][3]=max(a[1],a[2]);
az(1,1,2,3,0);
az(1,2,1,3,0);
az(1,3,1,2,0);
int m=(n&1)?n/2+1:n/2;
for(int i=2;i<=m;i++)
{
int x=i<<1,y=i<<1|1;
for(int j=1;j<x;j++)
{
if(f[i][x]>f[i-1][j]+max(a[j],a[y]))
{
f[i][x]=f[i-1][j]+max(a[j],a[y]);
az(i,x,j,y,j);
}
if(f[i][y]>f[i-1][j]+max(a[j],a[x]))
{
f[i][y]=f[i-1][j]+max(a[j],a[x]);
az(i,y,j,x,j);
}
if(f[i][j]>f[i-1][j]+max(a[x],a[y]))
{
f[i][j]=f[i-1][j]+max(a[x],a[y]);
az(i,j,x,y,j);
}
}
}
cout<<f[m][n+1]<<endl;
prout(m,n+1);
return 0;
}
随机推荐
- Java并发(十二)----线程应用之多线程解决烧水泡茶问题
1.背景 统筹方法,是一种安排工作进程的数学方法.它的实用范围极广泛,在企业管理和基本建设中,以及关系复杂的科研项目的组织与管理中,都可以应用. 怎样应用呢?主要是把工序安排好. 比如,想泡壶茶喝.当 ...
- VisionPro学习笔记(2)——图像转换工具ImageCovertTool
众所周知,VisionPro是一款功能强大的机器视觉软件,用于开发和部署机器视觉应用程序.其中ImageConvertTool是其中一个重要的工具,用于图像转换和处理.本文将介绍如何使用ImageCo ...
- JAVA获取字符串内的括号对;获取括号对的内容;按指定规则返回括号对位置;
先看结果:处理字符串 "这个是一条测试用的字符串[ ( 5 ( 4( 3 [(1) (2)] ))(7))][(6)]" 结果 解决思路:参考正则表达式里面出入站部分 代码实现如下 ...
- 【SpringBoot】定时任务
SpringBoot实现定时任务 SpringBoot创建定时任务,目前主要有以下三种实现方式: 基于注解(@Scheduled): 基于注解@Scheduled默认为单线程,开启多个任务时,任务的执 ...
- 2023-07-16:讲一讲Kafka与RocketMQ中零拷贝技术的运用?
2023-07-16:讲一讲Kafka与RocketMQ中零拷贝技术的运用? 答案2023-07-16: 什么是零拷贝? 零拷贝(英语: Zero-copy) 技术是指计算机执行操作时,CPU不需要先 ...
- 行行AI人才直播第13期:刘红林律师《AIGC创业者4大法律问题需注意》
行行AI人才(海南行行智能科技有限公司)是博客园和顺顺智慧共同运营的AI行业人才全生命周期服务平台. AIGC爆火至今,商业落地已成为各行各业焦点的问题.它的广泛应用也带来了一系列的法律风险和挑战.一 ...
- 关于Linux下服务器MySQL的安装和搭建
一.检测是否已经安装Mysql 检测 # yum list installed | grep mysql //检查安装 # yum -y remove mysql-libs.x86_64 //卸载 / ...
- Redis从入门到放弃(9):集群模式
前面文章我们介绍了Redis的主从模式是一种在Redis中实现高可用性的方式,但也存在一些缺点. 1.主从模式缺点 写入单点故障:在主从模式中,写入操作只能在主节点进行,如果主节点宕机,写入将无法执行 ...
- 使用 Vue 实现页面访问拦截
目录 1 Vue 路由与导航守卫 1.1 Vue 路由简介 1.2 导航守卫概述 2 实现访问拦截的核心概念 2.1 路由守卫介绍 2.1.1 前置守卫(beforeEach) 2.1.2 后置钩子( ...
- 美团面试拷打:ConcurrentHashMap 为何不能插入 null?HashMap 为何可以?
周末的时候,有一位小伙伴提了一些关于 ConcurrentHashMap 的问题,都是他最近面试遇到的.原提问如下: 整个提问看着非常复杂,其实归纳来说就是两个问题: ConcurrentHashMa ...