poj 1700 poj 3404
来源:互联网 发布:windows iot 安装软件 编辑:程序博客网 时间:2024/05/16 12:37
经典的过桥问题。只要这两道题一起做,就可以做对了(之前老是考虑少一种情况。。。思维能力有待提高)。
具体解法如下:
n==1时,直接输出答案
n==2时,输出最大值
n==3时,输出三者和
n>=4时,两种策略,均转化成4人时的情形求最优解。设4人过河速度为A<B<C<D,那么有两种策略:
先AB过,A回,CD过,B回,即temp=A+2*B+D
先AD过,A回,再AC过,A回,即temp=2*A+C+D(忘了这种情况)
然后取其中的最小值即可。也就是说,在2*B<A+C时选方案1,否则选方案2.
多做dp和贪心题是锻炼思维的好方法,以后要多多训练思维才行。
以下是代码:(poj 1700)
- #include<cstdio>
#include<algorithm>
using namespace std;
const int M=1100;
int a[M];
int n; - int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int i;
bool flag=false;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
sort(a+1,a+n+1);
int ans=0;
while(n)
{
if(n==1)
{
ans+=a[1];
n=0;
}
else if(n==2)
{
ans+=a[2];
n=0;
}
else if(n==3)
{
ans+=a[1]+a[2]+a[n];
n=0;
}
else
{
if(2*a[2]<a[1]+a[n-1])
{
ans+=a[1]+2*a[2]+a[n];
n-=2;
}
else
{
ans+=2*a[1]+a[n-1]+a[n];
n-=2;
}
}
}
printf("%d/n",ans);
}
return 0;
}
poj 3404 的只要把case那个去掉即可,看了题目就懂了。
- poj 1700 poj 3404
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 记号: WPF ListView 自定义 Header
- 第一期十篇在新浪博客上关于苹果开发的博文
- JAVA swing JLabel JButton标签文本文字内容的换行
- Android中联系人数据结构及逻辑
- 何修改Visual Studio 2005 的默认导入导出路径?
- poj 1700 poj 3404
- 笨笨数据压缩教程--第六章 聪明的以色列人(下):LZ78 和 LZW
- 当ERP邂逅SOA 谁哭谁笑谁知道?
- protocol buffers 简介
- linux socket 中判断对方关闭的两种方式
- Ext中JsonStore重新设置url然后加载数据的方法
- eclipse3.6+tomcatPluginV33+VisualEditor1.5安装,打包,下载
- ARM存储管理
- build