• poj2573Bridge(过桥问题)


    链接

    A,B为最快和次快

    有两种方式可以使c,d过桥

    一是a与c一起走,a回来接d再与d一起走,一直到对岸人为0为止

    而是 a与b一起走 a回来送灯 c与d一起走 b回来送灯 重复此过程。

    只剩2人时  直接过桥

    3 人时  A回来送灯 ac走 a回来送灯 ab走

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 #include<vector>
     7 #include<cmath>
     8 #include<queue>
     9 #include<set>
    10 using namespace std;
    11 #define N 1010
    12 #define LL long long
    13 #define INF 0xfffffff
    14 const double eps = 1e-8;
    15 const double pi = acos(-1.0);
    16 const double inf = ~0u>>2;
    17 int a[N];
    18 int main()
    19 {
    20     int i,n;
    21     while(cin>>n)
    22     {
    23         for(i = 1; i <= n;i++)
    24         cin>>a[i];
    25         if(n==1)
    26         {
    27             cout<<a[1]<<endl;
    28             cout<<a[1]<<endl;
    29             continue;
    30         }
    31         sort(a+1,a+n+1);
    32         int g = n;
    33         int ans = 0;
    34         while(g>3)
    35         {
    36             int s = min(a[g]+a[g-1]+2*a[1],a[2]*2+a[g]+a[1]);
    37             ans+=s;
    38             g-=2;
    39         }
    40         if(g==3)
    41             ans+=a[1]+a[2]+a[g];
    42         else if(g==2)
    43             ans+=a[2];
    44         cout<<ans<<endl;
    45         g = n;
    46         while(g>3)
    47         {
    48             int s = a[g]+a[g-1]+2*a[1];
    49             if(s>a[2]*2+a[g]+a[1])
    50             printf("%d %d
    %d
    %d %d
    %d
    ",a[1],a[2],a[1],a[g-1],a[g],a[2]);
    51             else
    52             printf("%d %d
    %d
    %d %d
    %d
    ",a[1],a[g],a[1],a[1],a[g-1],a[1]);
    53             g-=2;
    54         }
    55         if(g==3)
    56         printf("%d %d
    %d
    %d %d
    ",a[1],a[g],a[1],a[1],a[2]);
    57         else if(g==2)
    58         printf("%d %d
    ",a[1],a[2]);
    59     }
    60     return 0;
    61 }
    View Code
  • 相关阅读:
    2020-09-24 刷题记录
    Codeforces Round #672 (Div. 2) A
    力扣部分刷题记录
    CF 1500-1800训练 A
    AtCoder Beginner Contest 179
    Educational Codeforces Round 95 (Rated for Div. 2) A
    AtCoder Beginner Contest 178 A
    springboot-------整合mybatis-plus
    机器学习入门--------numpy学习
    机器学习入门--------matplotlib学习
  • 原文地址:https://www.cnblogs.com/shangyu/p/3620009.html
Copyright © 2020-2023  润新知