BridgeOverARoughRiver(POJ-3404)【AdHoc】
题目链接:https://vjudge.net/problem/POJ-3404
题意:n个极限速度不同的人要过桥,一开始所有人在桥的一边,每次最多两个人同时过桥,过桥时需要用一把火炬并且全场只有一把火炬,问所有人到达桥的另外一边所需最短时间。
思路:凭直觉很多人都会立刻想到每次都让最快的人在桥两边来回陪同其他人过桥,但是当遇到类似于1 2 10 11,这种,最优解为 1 2,1,10 11,2,1 2,并不是1 2,1,1 10,1,1 11.
设a最快,b次快,d次慢,c最慢,每次有两种策略可供选择(剩余人数大于等于4,其他情况很容易计算):
1.a b,a,c d,b,
2.a d,a,a c,a.
每次对两种方案进行比较即可。
代码如下:
#include<cstdio>
#include<list>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
list<int> lt;
int n,val[];
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&val[i]);
}
sort(val+,val++n);
for(int i=;i<=n;i++)
lt.push_back(val[i]);
list<int>::iterator it;
int ans=;
while(lt.size()>){
if(lt.size()>=){
int a=*lt.begin(),b,c,d;
it=lt.begin();it++;
b=*it;
it=lt.end();
it--;d=*it;
it--;c=*it;
int t1=b+a+d+b,t2=d+a+c+a;
ans+=min(t1,t2);
it=lt.end();
it--;lt.erase(it);
it=lt.end();it--;lt.erase(it);
}
else if(lt.size()==){
int a,b,c;
it=lt.begin();
a=*it;it++;
b=*it;it++;
c=*it;
int t=b+a+c;
ans+=t;
break;
}
else if(lt.size()==){
it=lt.begin();it++;
ans+=*it;break;
}
else {
ans+=*lt.begin();
break;
}
}
printf("%d",ans);
return ;
}
BridgeOverARoughRiver(POJ-3404)【AdHoc】的更多相关文章
- 【简●解】POJ 1845 【Sumdiv】
POJ 1845 [Sumdiv] [题目大意] 给定\(A\)和\(B\),求\(A^B\)的所有约数之和,对\(9901\)取模. (对于全部数据,\(0<= A <= B <= ...
- POJ 2154 【POLYA】【欧拉】
前记: TM终于决定以后干啥了.这几天睡的有点多.困饿交加之间喝了好多水.可能是灌脑了. 切记两件事: 1.安心当单身狗 2.顺心码代码 题意: 给你N种颜色的珠子,串一串长度问N的项链,要求旋转之后 ...
- POJ 3230 【DP】
题意: 某货旅行,在n个城市呆m天. 给出从第i个城市到第j个城市的路费,或者留在某个城市的生活费. 给出在第i天在第j个城市的收益. 可以在城市之间任意穿梭逗留没有其他特殊要求. 求收益最大是多少. ...
- POJ 3187【permutation】
POJ 3187 给定N值,从而确定了数据的范围及长度,暴力枚举数列,接下来类似杨辉三角的递推计算.注permutation从递增有序数列开始枚举,枚举到符合sum值时退出即可 #include &l ...
- POJ 2718【permutation】
POJ 2718 问题描述: 给一串数,求划分后一个子集以某种排列构成一个数,余下数以某种排列构成另一个数,求这两个数最小的差,注意0开头的处理. 超时问题:一开始是得到一个数列的组合之后再从中间进行 ...
- Tiling POJ 2506 【大数】
id=2506">http://poj.org/problem?id=2506 Description In how many ways can you tile a 2xn rect ...
- poj 2431 【优先队列】
poj 2431 Description A group of cows grabbed a truck and ventured on an expedition deep into the jun ...
- poj 2385【动态规划】
poj 2385 Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14007 Accepte ...
- POJ 1286 【POLYA】
题意: 给你三种颜色的珠子,每次给你N,问在旋转,翻转之后视作相同的情况下,能组成多少种不同的项链. 思路: 让我们借这道题拯救一下我对POLYA定理的理解... sigma(m^(gcd(i,n)) ...
随机推荐
- Subsequence (POJ - 3061)(尺取思想)
Problem A sequence of N positive integers (10 < N < 100 000), each of them less than or equal ...
- zabbix之自定义告警
zabbix支持内置的告警类型.email,sms,等 有时候需要自定义类型的. [其他微信,钉钉都差不多方式,只是脚本不一样] 自定义告警类型[自定义邮件] 编写自定义脚本,并测试成功. [脚本需要 ...
- mysql 查询整个数据库所有表的行数
>use information_schema; >select sum(table_rows) from tables where TABLE_SCHEMA = "test&q ...
- 在树莓派4b上安装 ROS MELODIC 源码安装
按照以下步骤照做就可以了,很简单的,就是浪费一点点时间罢了.也可以退而求其次,买个树莓派3B+来玩,哈哈. Step 1: Install Dependecies and Download the P ...
- Ryu控制器编程开发——packet_in和packet_out简易交换机实现
Ryu控制器二次开发,实现一个简单的只能够简单地广播数据包的交换机. from ryu.base import app_manager from ryu.controller import ofp_e ...
- nginx使用certbot配置https
一般现在的网站都要支持https,即安全的http. 机器:阿里云Ubuntu 16.04.3 LTS 方案一:自己申请证书 配置时需要确保有ssl模块, 之后域名解析下, 之后时申请证书,可以去阿里 ...
- RAFT选举算法-分布式数据库困惑
在做HIS研发工作的时候一直想完善其数据组件,想做一个分布式的数据库支持系统.但一直以来都不清楚这个选举算法应怎么做,原来有一个叫raft的算法https://www.cnblogs.com/just ...
- Chrome浏览器报错:ERR_UNSAFE_PORT
今天用Chrome浏览器打开一个页面发现报错了:ERR_UNSAFE_PORT. 所以,去搜了一下发现Chrome浏览器是默认一些端口号为非安全端口的. 遇到这个问题建议更换端口号或者更换浏览器打开. ...
- LeetCode —— 单词接龙(Python)
使用字典,降低查找的复杂度.使用list会超时. class Solution: def nextWordsList(self, word, wordDict): res_list = [] for ...
- uWSGI Apache 处理 惊群效应的方式 现代的内核
Serializing accept(), AKA Thundering Herd, AKA the Zeeg Problem — uWSGI 2.0 documentationhttps://uws ...