SRM 146 2 1000BridgeCrossing


Problem Statement

A well-known riddle goes like this: Four people are crossing an old bridge. The bridge cannot hold more than two people at once. It is dark, so they can't walk without a flashlight, and they only have one flashlight! Furthermore, the time needed to cross the bridge varies among the people in the group. For instance, let's say that the people take 1, 2, 5 and 10 minutes to cross the bridge. When people walk together, they always walk at the speed of the slowest person. It is impossible to toss the flashlight across the bridge, so one person always has to go back with the flashlight to the others. What is the minimum amount of time needed to get all the people across the bridge?

In this instance, the answer is 17. Person number 1 and 2 cross the bridge together, spending 2 minutes. Then person 1 goes back with the flashlight, spending an additional one minute. Then person 3 and 4 cross the bridge together, spending 10 minutes. Person 2 goes back with the flashlight (2 min), and person 1 and 2 cross the bridge together (2 min). This yields a total of 2+1+10+2+2 = 17 minutes spent.

You want to create a computer program to help you solve new instances of this problem. Given an int[] times, where the elements represent the time each person spends on a crossing, your program should return the minimum possible amount of time spent crossing the bridge.

Definition

  • ClassBridgeCrossing
  • MethodminTime
  • Parametersvector<int>
  • Returnsint
  • Method signatureint minTime(vector<int> times)
(be sure your method is public)

Limits

  • Time limit (s)2.000
  • Memory limit (MB)64

Notes

  • In an optimal solution, exactly two people will be sent across the bridge with the flashlight each time (if possible), and exactly one person will be sent back with the flashlight each time. In other words, in an optimal solution, you will never send more than one person back from the far side at a time, and you will never send less than two people across to the far side each time (when possible).

Constraints

  • times will have between 1 and 6 elements, inclusive.
  • Each element of times will be between 1 and 100, inclusive.

Test cases

    1.  
      • times{ 1, 2, 5, 10 }
       

      Returns17

       
      The example from the text.
    2.  
      • times{ 1, 2, 3, 4, 5 }
       

      Returns16

       
      One solution is: 1 and 2 cross together (2min), 1 goes back (1min), 4 and 5 cross together (5min), 2 goes back (2min), 1 and 3 cross together (3min), 1 goes back (1min), 1 and 2 cross together (2min). This yields a total of 2 + 1 + 5 + 2 + 3 + 1 + 2 = 16 minutes spent.
    3.  
      • times{ 100 }
       

      Returns100

       
      Only one person crosses the bridge once.
    4.  
      • times{ 1, 2, 3, 50, 99, 100 }
       

      Returns162

       #include <cstdio>
      #include <cmath>
      #include <cstring>
      #include <ctime>
      #include <iostream>
      #include <algorithm>
      #include <set>
      #include <vector>
      #include <sstream>
      #include <typeinfo>
      #include <fstream> using namespace std;
      int ti[] , tot = ;
      int a[][] ;
      class BridgeCrossing {
      public:
      int minTime(vector<int> tim) {
      tot = ;
      for (int i : tim) ti[tot ++] = i ; tot -- ;
      sort (ti + , ti + tot + ) ;
      // printf ("tot = %d\n" , tot ) ;
      // for (int i = 1 ; i <= tot ; i ++) printf ("%d " , ti[i]) ; puts ("") ;
      if (tot == ) return ti[] ;
      if (tot == ) return ti[] ;
      int sum = ti[] ;
      for (int i = tot ; i >= ; i -= ) {
      if (i == ) {
      sum += ti[] + ti[] ;
      }
      else {
      if (ti[] + ti[i - ] < * ti[]) sum += ti[] + ti[i] + ti[] + ti[i - ] ;
      else sum += ti[] + ti[i] + ti[] + ti[] ;
      }
      }
      return sum ;
      }
      }; // CUT begin
      ifstream data("BridgeCrossing.sample"); string next_line() {
      string s;
      getline(data, s);
      return s;
      } template <typename T> void from_stream(T &t) {
      stringstream ss(next_line());
      ss >> t;
      } void from_stream(string &s) {
      s = next_line();
      } template <typename T> void from_stream(vector<T> &ts) {
      int len;
      from_stream(len);
      ts.clear();
      for (int i = ; i < len; ++i) {
      T t;
      from_stream(t);
      ts.push_back(t);
      }
      } template <typename T>
      string to_string(T t) {
      stringstream s;
      s << t;
      return s.str();
      } string to_string(string t) {
      return "\"" + t + "\"";
      } bool do_test(vector<int> times, int __expected) {
      time_t startClock = clock();
      BridgeCrossing *instance = new BridgeCrossing();
      int __result = instance->minTime(times);
      double elapsed = (double)(clock() - startClock) / CLOCKS_PER_SEC;
      delete instance; if (__result == __expected) {
      cout << "PASSED!" << " (" << elapsed << " seconds)" << endl;
      return true;
      }
      else {
      cout << "FAILED!" << " (" << elapsed << " seconds)" << endl;
      cout << " Expected: " << to_string(__expected) << endl;
      cout << " Received: " << to_string(__result) << endl;
      return false;
      }
      } int run_test(bool mainProcess, const set<int> &case_set, const string command) {
      int cases = , passed = ;
      while (true) {
      if (next_line().find("--") != )
      break;
      vector<int> times;
      from_stream(times);
      next_line();
      int __answer;
      from_stream(__answer); cases++;
      if (case_set.size() > && case_set.find(cases - ) == case_set.end())
      continue; cout << " Testcase #" << cases - << " ... ";
      if ( do_test(times, __answer)) {
      passed++;
      }
      }
      if (mainProcess) {
      cout << endl << "Passed : " << passed << "/" << cases << " cases" << endl;
      int T = time(NULL) - ;
      double PT = T / 60.0, TT = 75.0;
      cout << "Time : " << T / << " minutes " << T % << " secs" << endl;
      cout << "Score : " << * (0.3 + (0.7 * TT * TT) / (10.0 * PT * PT + TT * TT)) << " points" << endl;
      }
      return ;
      } int main(int argc, char *argv[]) {
      cout.setf(ios::fixed, ios::floatfield);
      cout.precision();
      set<int> cases;
      bool mainProcess = true;
      for (int i = ; i < argc; ++i) {
      if ( string(argv[i]) == "-") {
      mainProcess = false;
      } else {
      cases.insert(atoi(argv[i]));
      }
      }
      if (mainProcess) {
      cout << "BridgeCrossing (1000 Points)" << endl << endl;
      }
      return run_test(mainProcess, cases, argv[]);
      }
      // CUT end

      这个问题大家都很熟悉吧,晚上有5个人过河,只有一盏灯,,他们每个人的过河时间为1,2,3,6,18(另一组人1,10,10,10,10)。每次最多两个人过河(过河必须有一个人带着灯),且过河的时间按走得慢的那个人算,求5个人过河所需的最少时间?

      当然这个问题转到程序里自然是变成了n个人过河喽。

      如果你把上面得数据跑了一遍的话,得到29(另一组43)的话。那么心里大概就有最佳方案的笼统概念了:

      现在令n个人走过的时间分别为a1,a2,a3,a4,a5,……an;(从小到大)

      那么一开始肯定先是1,2过河。

      接着考虑花时间最多的那两个人(必须考虑他们,这点很重要),我们怎么送他们呢?

      第一种方案:

        1回来了,让1把n送过去,然后1回来,再让1把n-1送过去:a1 + an + a1 + a(n-1) ;

      第二种方案:

        1回来了,让n,n-1过河,然后2回来,再1,2过河: a1 + an + a2 + a2 ;

      (如果觉得叙述方案时有奇怪的地方,请见怪不怪,总之是为了让每轮送两人,并且每轮结束时保证1,2都在对岸)

      所以这两种方案进行选择时,只要比较:a1 + a(n-1) 和 2× a2 中较小的即可。

      。。。大概就是这样,因为每次送两人,所以总人数为奇数时,还有点细节要考虑。

tc 146 2 BridgeCrossing(n人过桥问题)的更多相关文章

  1. tc 146 2 RectangularGrid(数学推导)

    SRM 146 2 500RectangularGrid Problem Statement Given the width and height of a rectangular grid, ret ...

  2. 蒟蒻修养之tc蓝名计划

    开一个新坑......(听说tc是智商高的人才能玩的QAQ显然我是被屠的... 1 [645DIV2]这个能说是裸模拟吗... 弃坑= =做了一些题感觉没必要放上来了= =等div1先吧....... ...

  3. [转帖][超级少儿不宜]一氧化氮(NO),为什么亚洲人是最硬

    阴茎科学:一氧化氮(NO),为什么亚洲人是最硬 尼堪巴图鲁   ​关注他 2,911 人赞同了该文章   https://zhuanlan.zhihu.com/p/55941740 超级少儿不宜.. ...

  4. Hznu_oj 2340 你敢一个人过桥吗?

    Description 在漆黑的夜里,N位旅行者来到了一座狭窄而且没有护栏的桥边.如果不借助手电筒的话,大家是无论如何也不敢过桥去的.不幸的是,N个人一共只带了一只手电筒,而桥窄得只够让两个人同时过. ...

  5. 【bzoj2073】[POI2004]PRZ

    题目描述 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍在桥上的人都不能超过一定的限制. 所以这只队伍过桥时只能分批 ...

  6. 【BZOJ2073】[POI2004]PRZ 状压DP

    [BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...

  7. 大叔也说并行和串行`性能提升N倍(N由操作系统位数和cpu核数决定)

    返回目录 并行是.net4.5主打的技术,同时被封装到了System.Threading.Tasks命名空间下,对外提供了静态类Parallel,我们可以直接使用它的静态方法,它可以并行一个委托数组, ...

  8. Summarize Series For Burying My College

    Summarize  Series  For  Burying  My  College                                             For  Grade ...

  9. POJ 1700 Crossing River (贪心)

    Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9585 Accepted: 3622 Descri ...

随机推荐

  1. CentOS找回root密码

    如果忘记了root密码,可以进入单用户模式进行密码重置. 重启系统,在grub的启动菜单中按下e键,然后编辑kernel那一行,在最后的quiet后加上single. 按下Enter后,再按b开机进入 ...

  2. 一次奇怪的T-shirt展示

    因为这次的第一也是上次的第一然后顺延下来又正好跟女神并列第二,拿到了一件T-shirt.总之,还是应该继续加油.

  3. django models auto_now和auto_now_add的区别

    DataTimeField()中auto_now参数和auto_now_add参数区别: 前者添加或者修改的都为现在的时间,可以再次更新: 后者仅仅为添加时候的时间,不可更改.

  4. Build to win!——获得小黄衫的感想

    UPDATE: 应栋哥要求,上传了无遮挡的正面照(我的内心其实是拒绝的!(ㄒoㄒ)) 一.前言&背景 从大一上C++课程开始,栋哥就开始安利他大三的软工实践课. 时间过得飞快,大学转眼就过去一 ...

  5. Linux上性能异常定位以及性能监控

    引言:大多数的服务都是跑在Linux上的,Linux现在也已经到了一个很广泛的应用,但是仍然会有很多问题出现,我们就来讨论下我们性能监控的指标,性能监控无非就是从I/O,内存,CPU,TCP连接数,网 ...

  6. 我所了解的JavaScript糟粕和鸡肋

    糟粕 全局变量 众所周知,全局变量在很小的程序中可能会带来方便,但随着程序变得越来大,全局变量将难以处理,全局变量将降低程序的可靠性. 在js中有3种方式定义全局变量 脱离任何函数安排一个var语句  ...

  7. React Native 开发之 (05) flexbox布局

    一  flexbox布局 1 flex布局 flexbox是ReactNative 应用开发中必不可少的内容,也是最常用的内容. 传统的页面布局是基于盒子模型,依赖定位属性,流动属性和显示属性来解决. ...

  8. Saltstack数据系统Grains和Pillar(三)

    Saltstack数据系统 分为Grains和Pillar 一.Grains 静态数据,当Minion启动的时候收集的MInion本地的相关信息.(包含操作系统版本.内核版本.CPU.内存.硬盘.设备 ...

  9. Processing Images

    https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_ ...

  10. WinForm------字段不能为空错误

    错误信息: "System.Data.ConstraintException"类型的异常在 EntityFramework.dll 中发生,但未在用户代码中进行处理 其他信息: T ...