poj 2573 Bridge (过桥问题 贪心)

对于此问题有两种策略

1、最快的带最慢的和次慢的

2、最快和次快带最慢和次慢

此链接有详细解释点击打开链接

<span style="font-size:18px;"><span style="font-size:24px;">#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int s[1050];
int main()
{
    int a;
    scanf("%d",&a);
    for(int i=0;i<a;i++)
        scanf("%d",&s[i]);
    if(a==1)
    {
        printf("%d\n%d\n",s[0],s[0]);
        return 0;
    }
    sort(s,s+a);
    int ans=a,cne,sum=0,d,e;
    while(ans>3)
    {
        int sum1=2*s[0]+s[ans-2]+s[ans-1];
        int sum2=2*s[1]+s[0]+s[ans-1];
        if(sum1>sum2)
            sum+=sum2;
        else
            sum+=sum1;
        ans-=2;
    }
    if(ans==3)
        printf("%d\n",sum+=s[0]+s[1]+s[2]);
    else
        printf("%d\n",sum+=s[1]);
    while(a>3)
    {
        if(s[0]+s[a-2]<2*s[1])
            printf("%d %d\n%d\n%d %d\n%d\n",s[0],s[a-1],s[0],s[0],s[a-2],s[0]);
        else
            printf("%d %d\n%d\n%d %d\n%d\n",s[0],s[1],s[0],s[a-2],s[a-1],s[1]);
        a-=2;
    }
    if(a==3)
        printf("%d %d\n%d\n%d %d\n",s[0],s[2],s[0],s[0],s[1]);
    else
        printf("%d %d\n",s[0],s[1]);
    return 0;
}
</span></span>

poj 2573 Bridge (过桥问题 贪心)

时间: 2024-11-05 19:25:46

poj 2573 Bridge (过桥问题 贪心)的相关文章

POJ 2573 Bridge 【模拟+分治】

题目: n人希望在晚上过河. 最多两人可以组成一组一起过河,每组必须有手电筒. 在n个人中只有一个手电筒,所以必须安排过河顺序,以便及时返回手电筒使得更多的人可以过河.每个人的过河速度都不同; 每一组的速度由较慢成员的速度决定. 你的任务就是确定一个方案,让所有的人在最短的时间内过河并且输出方案. 分析: 此题其实本来是没有思路的,但是 ,通过排序,我们发现:既然每次都得一个人传递手电筒,为什么不让最快的那个人传呢(其实同时需要a[1],a[2]两个人配合,详见方案2)?又因为能者多劳,因此每次

poj 3404 Bridge over a rough river(过桥问题)

题目链接:http://poj.org/problem?id=3404 Bridge over a rough river Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4024   Accepted: 1644 Description A group of N travelers (1 ≤ N ≤ 50) has approached an old and shabby bridge and wishes to cro

POJ 2965-The Pilots Brothers&#39; refrigerator(贪心+枚举)

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19464   Accepted: 7462   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

poj 2586 Y2K Accounting Bug (贪心)

Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9632   Accepted: 4806 Description Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc.

POJ 1328、Radar Installation 贪心

jQuery的属性操作非常简单,下面以一个a元素来说明属性的获取/设置/删除操作 <body> <a>jquery.com</a> </body> 添加属性 $('a').attr('href', 'http://www.jquery.com') 添加多个属性 $('a').attr({'href':'http://www.jquery.com', 'title':'jquery.com'}) 获取属性 $('a').attr('href') class属性

POJ 3069 Saruman&#39;s Army(贪心)

Saruman's Army Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3069 Appoint description:  System Crawler  (2015-04-27) Description Saruman the White must lead his army along a straight path from

poj 3608 Bridge Across Islands 两凸包间最近距离

1 /** 2 旋转卡壳,, 3 **/ 4 #include <iostream> 5 #include <algorithm> 6 #include <cmath> 7 #include <cstdio> 8 using namespace std; 9 10 const double eps = 1e-8; 11 struct point { 12 double x,y; 13 point(double x=0,double y =0):x(x),y(

poj 2393 Yogurt Factory(贪心)

Description The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over the next N (1 <= N <= 10,000) weeks, the price of milk and labor will fluctuate weekly such that it will cost the company C_i (1 <= C_i <= 5,000) c

POJ 2393 Yogurt factory(简单贪心)

http://poj.org/problem?id=2393 题意:任务规定,一个酸奶制造厂,在n个星期内,分别要向外提供y[i]unit的酸奶.已知这个制造厂第i周制造每unit酸奶的费用为c[i],储存室储存每1unit酸奶1星期的费用为s.问要完成这个任务的最小费用是多少. . . . . . . . . . . . . . 思路:简单贪心.维护一个目前最优的代价minc = min(c, minc + s),然后求和. #include <iostream> using namespa