杂题_POJ上的过桥问题
本文出自:http://blog.csdn.net/svitter
过桥问题解释:一条船能够坐两个人,可是有非常多人要过河,所以送过一个人去,还有一个人还要回来接。使全部人过河之后时间最短,怎样求?
此问题曾作为阿里巴巴题目
初看此题想的太过简单,直接让跑的最快的送过去,自己再跑回来就可以。事实上不然。
函数g(a,b)表示过河,b(a)表示回来。假设过河时间分别为1,2,5,10
那么两种过河方案:
1.初想方案:
g(1,2)=2
g (1, 10) = 10
g (1, 5 ) = 5
b(1) * 2 = 2
sum =19
2.还有一种方案:. g(1,2) =2
b(1) =1
g(5,10)=10
b(2)=2
g(1,2)=2
sum = 17
还有一种方案就是让两个跑的快的先跑过去,然后一个带回来船,然后两个慢的跑过去,还有一个跑的快的带回来船。
如此两种方案的速度分别为:
设跑的快的分别为a, b,跑的慢的为c, d
那么c, d过河须要的时间分别为:
1.a + a + c + d
2.a +b+b+d
如此一来,求得b+b与a+c的大小关系,则知道取哪种方案最佳。
题目有poj2573, poj1700
1700解题代码:(也有dfs,dp写法,可是我没理解)
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm> using namespace std; int main()
{
int n, sec;
int time[1010];
int t;
int temp;
int i, j, k;
//freopen("test", "r", stdin);
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
memset(time, 0, sizeof(time)); for(i = 0; i < n; i++)
scanf("%d", &time[i]); if(n == 1)
printf("%d\n", time[0]); else if(2 == n)
printf("%d\n", time[0] > time[1] ? time[0] : time[1]); else if(3 == n)
printf("%d\n", time[0] + time[1] + time[2]); else
{
sec = 0;
sort(time ,time + n);
temp = time[1]*2; while(n >=4)
{
sec += time[0];
sec += time[n-1];
i = time[1] * 2;
j = time[0] + time[n-2];
if(i > j)
sec += j;
else
sec += i;
n -= 2;
}
if(2 == n)
printf("%d\n", sec + time[1]); else if(3 == n)
printf("%d\n", sec + time[0] + time[1] + time[2]);
}
} return 0;
}
2573:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue> using namespace std; struct outPut
{
int front, end;
outPut(int a, int b):front(a), end(b){}
outPut(int a):front(a), end(-1){}
}; void output(outPut *a)
{
if(a->end != -1)
printf("%d %d\n", a->front, a->end);
else
printf("%d\n", a->front);
} int main()
{
int temp;
int pe[1010];
int i, j;
int n;
// cost time
int sec;
//freopen("test", "r", stdin);
queue <outPut> Queue;
while(~scanf("%d", &n))
{
//printf("\nCase: \n");
for(i = 0; i < n; i++)
scanf("%d", &pe[i]); if(1 == n)
{
printf("%d\n", pe[0]);
printf("%d\n", pe[0]);
}
//..
else if(2 == n)
{
if(pe[0] > pe[1])
{
i = pe[0];
j = pe[1];
}
else
{
i = pe[1];
j = pe[0];
}
printf("%d\n", i); printf("%d %d\n", j, i);
}
else if(3 == n)
{
sort(pe, pe+3);
printf("%d\n", pe[0]+pe[1]+pe[2]); printf("%d %d\n", pe[0], pe[2]);
printf("%d\n", pe[0]);
printf("%d %d\n", pe[0], pe[1]);
}
else
{
sort(pe, pe+n);
temp = pe[1] * 2;
sec = 0; while(n >= 4)
{
sec += pe[n-1];
sec += pe[0];
if(temp < pe[0] + pe[n-2])
{
Queue.push(outPut(pe[0], pe[1]));
Queue.push(outPut(pe[0]));
Queue.push(outPut(pe[n-2], pe[n-1]));
Queue.push(outPut(pe[1]));
sec += temp;
}
else
{
Queue.push(outPut(pe[0], pe[n-1]));
Queue.push(outPut(pe[0]));
Queue.push(outPut(pe[0], pe[n-2]));
Queue.push(outPut(pe[0]));
sec += pe[0] + pe[n-2];
}
n -= 2;
}
if(2 == n)
{
outPut *tmp;
printf("%d\n", sec + pe[1]);
while(!Queue.empty())
{
tmp = &Queue.front();
output(tmp);
Queue.pop();
}
printf("%d %d\n", pe[0], pe[1]);
}
else if(3 == n)
{
outPut *tmp;
printf("%d\n", sec + pe[0] + pe[1] + pe[2]);
while(!Queue.empty())
{
tmp = &Queue.front();
output(tmp);
Queue.pop();
}
printf("%d %d\n", pe[0], pe[2]);
printf("%d\n", pe[0]);
printf("%d %d\n", pe[0], pe[1]);
} }
}
return 0;
}
杂题_POJ上的过桥问题的更多相关文章
- 正睿OI DAY3 杂题选讲
正睿OI DAY3 杂题选讲 CodeChef MSTONES n个点,可以构造7条直线使得每个点都在直线上,找到一条直线使得上面的点最多 随机化算法,check到答案的概率为\(1/49\) \(n ...
- dp杂题(根据个人进度选更)
----19.7.30 今天又开了一个新专题,dp杂题,我依旧按照之前一样,这一个专题更在一起,根据个人进度选更题目; dp就是动态规划,本人认为,动态规划的核心就是dp状态的设立以及dp转移方程的推 ...
- Codeforces 杂题集 2.0
记录一些没有写在其他随笔中的 Codeforces 杂题, 以 Problemset 题号排序 1326D2 - Prefix-Suffix Palindrome (Hard version) ...
- 【Java面试】-- 杂题
杂题 2019-11-03 21:09:37 by冲冲 1.类加载器的双亲委派机制 类加载器:把类通过类加载器加载到JVM中,然后转换成class对象(通过类的全路径来找到这个类). 双亲委派机制 ...
- 贪心/构造/DP 杂题选做Ⅱ
由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...
- 贪心/构造/DP 杂题选做Ⅲ
颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...
- Project Euler18题 从上往下邻接和
题目:By starting at the top of the triangle below and moving to adjacent numbers on the row below, the ...
- wangkoala杂题总集(根据个人进度选更)
CQOI2014 数三角形 首先一看题,先容斥一波,求出网格内选三个点所有的情况,也就是C(n*m,3);然后抛出行里三点共线的方案数:C(n,3)*m; 同理就有列中三点共线的方案数:n*C(m,3 ...
- 2019暑期金华集训 Day6 杂题选讲
自闭集训 Day6 杂题选讲 CF round 469 E 发现一个数不可能取两次,因为1,1不如1,2. 发现不可能选一个数的正负,因为1,-1不如1,-2. hihoCoder挑战赛29 D 设\ ...
随机推荐
- 基于visual Studio2013解决C语言竞赛题之0405阶乘求和
题目 解决代码及点评 这道题和上一道题类似,第n个累加项 = n-1累加项的n倍 由于有这个规律,我们可以用一个for循环实现 但是例子代码并没有这么做,大家可以回去修改下代码,使得代码更 ...
- IT第三天 - 数据类型、转换、Scanner使用
IT第三天 上午 变量类型 1.6种数值类型:byte.short.int.long.float.double:其中byte是8个字节,short是16字节,int是32字节.long是64字节:日常 ...
- 什么是CALayer
一.什么是CALayer * 在iOS系统中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. * 其实UIView之所以 ...
- Keil4 每次选build 编译(F7)都全部编译的解决办法
Keil4 每次选build 编译(F7)都全部编译的解决办法 http://blog.csdn.net/wchengshen/article/details/50440079 Keil4 每次选bu ...
- 一个库搞定各种分享--ShareSDK
ShareSDK是为iOS.Android.WindowsPhone提供社会功能的一个组件,开发者只需10分钟即可集成到自己的APP中,它不仅支持分享给QQ好友.微信好友.微信朋友圈.新浪微博.腾迅微 ...
- js调试工具console详解
#console基本输出方法,占位符:字符(%s).整数(%d).浮点数(%f)和对象(%o) console.log('日志'); console.info('信息'); console.error ...
- 基于visual Studio2013解决算法导论之018栈实现(基于链表)
题目 用链表实现栈 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <time.h> #in ...
- vb和vb.net事件机制
学习java事件前,回顾了下vb6和vb.net的事件机制,总结在这里,供对比用. 事件是面对对象中对象间通信的方法.事件发生者(又叫事件源)发生一个事件时,通过发送一条消息,给事件接受者(事件处理者 ...
- iOS:(接口适配器3)--iPhone适应不同型号 6/6plus 前
对于不同的苹果设备.检查每个参数<iOS:机型參数.sdk.xcode各版本号>. 机型变化 坐标:表示屏幕物理尺寸大小,坐标变大了.表示机器屏幕尺寸变大了: 像素:表示屏幕 ...
- ACM 做题过程中的一些小技巧。
ACM做题过程中的一些小技巧. 1.一般用C语言节约空间,要用C++库函数或STL时才用C++; cout.cin和printf.scanf最好不要混用. 2.有时候int型不够用,可以用long l ...