UVa 10037 - Bridge (贪心)

题意

N个人要过河,每次最多走两个人,只有一根手电筒,求最少过河时间。

思路

带人过河有两种方法

设走得最快的两个为a, b,并且假设他们现在在对岸。现在c和d要过河。


a c 

a d

或者


c d 

a b

前者的时间是a + c + a + d = 2a + c + d 
后者的时间是a + c + 2b

所以每运两个人过去的时候都要考虑一下这两种方式,选时间少的那种。

还有一个问题。一开始我是按从小到大算的。但是这样的话

1 2 3 100 100这组数据,会先运走3 100,最后运100,显然应该先运100 100。

所以应该从大到小排序。

然后我用队列记录了选择哪一种方法,直接输出也可以,不过看着太乱了。

代码

 
 
  1. #include <cstdio>
  2. #include <stack>
  3. #include <set>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <queue>
  8. #include <cstdlib>
  9. #include <functional>
  10. #include <cstring>
  11. #include <algorithm>
  12. #include <cctype>
  13. #include <string>
  14. #include <map>
  15. #include <iomanip>
  16. #include <cmath>
  17. #define LL long long
  18. #define ULL unsigned long long
  19. #define SZ(x) (int)x.size()
  20. #define Lowbit(x) ((x) & (-x))
  21. #define MP(a, b) make_pair(a, b)
  22. #define MS(arr, num) memset(arr, num, sizeof(arr))
  23. #define PB push_back
  24. #define F first
  25. #define S second
  26. #define ROP freopen("input.txt", "r", stdin);
  27. #define MID(a, b) (a + ((b - a) >> 1))
  28. #define LC rt << 1, l, mid
  29. #define RC rt << 1|1, mid + 1, r
  30. #define LRT rt << 1
  31. #define RRT rt << 1|1
  32. #define BitCount(x) __builtin_popcount(x)
  33. #define BitCountll(x) __builtin_popcountll(x)
  34. #define LeftPos(x) 32 - __builtin_clz(x) - 1
  35. #define LeftPosll(x) 64 - __builtin_clzll(x) - 1
  36. const double PI = acos(-1.0);
  37. const int INF = 0x3f3f3f3f;
  38. using namespace std;
  39. const double eps = 1e-6;
  40. const int MAXN = 1100 + 10;
  41. const int MOD = 1000007;
  42. typedef pair<int, int> pii;
  43. typedef vector<int>::iterator viti;
  44. typedef vector<pii>::iterator vitii;
  45. vector<int> arr;
  46. queue<int> step;
  47. int main()
  48. {
  49. //ROP;
  50. int T, i, j;
  51. scanf("%d", &T);
  52. while (T--)
  53. {
  54. arr.clear();
  55. int n;
  56. scanf("%d", &n);
  57. for (i = 0; i < n; i++)
  58. {
  59. int tmp;
  60. scanf("%d", &tmp);
  61. arr.PB(tmp);
  62. }
  63. sort(arr.begin(), arr.end(), greater<int>());
  64. int sz = SZ(arr);
  65. if (n == 2)
  66. {
  67. printf("%d\n", arr[sz - 2]);
  68. printf("%d %d\n", arr[sz - 1], arr[sz - 2]);
  69. if (T) puts("");
  70. continue;
  71. }
  72. if (n == 1)
  73. {
  74. printf("%d\n", arr[sz - 1]);
  75. printf("%d\n", arr[sz - 1]);
  76. if (T) puts("");
  77. continue;
  78. }
  79. int fst = arr[sz - 1], sec = arr[sz - 2], ans = sec;
  80. if (n == 3)
  81. {
  82. printf("%d\n", ans + fst + arr[sz - 3]);
  83. printf("%d %d\n", fst, sec);
  84. printf("%d\n", fst);
  85. printf("%d %d\n", fst, arr[sz - 3]);
  86. if (T) puts("");
  87. continue;
  88. }
  89. arr.pop_back(); arr.pop_back();
  90. for (i = 0; i + 1 < SZ(arr); i += 2)
  91. {
  92. int tmp = arr[i] + sec * 2 + fst;
  93. int _tmp = arr[i] + 2 * fst + arr[i + 1];
  94. if (tmp > _tmp)
  95. {
  96. ans += _tmp;
  97. step.push(1);
  98. }
  99. else
  100. {
  101. ans += tmp;
  102. step.push(-1);
  103. }
  104. }
  105. if (n & 1) ans += fst + arr.back();
  106. printf("%d\n", ans);
  107. printf("%d %d\n", fst, sec);
  108. for (i = 0; i + 1 < SZ(arr); i += 2)
  109. {
  110. printf("%d\n", fst);
  111. if (step.front() < 0)
  112. {
  113. printf("%d %d\n", arr[i + 1], arr[i]);
  114. printf("%d\n", sec);
  115. printf("%d %d\n", fst, sec);
  116. }
  117. else
  118. {
  119. printf("%d %d\n", fst, arr[i + 1]);
  120. printf("%d\n", fst);
  121. printf("%d %d\n", fst, arr[i]);
  122. }
  123. step.pop();
  124. }
  125. if (n & 1)
  126. {
  127. printf("%d\n", fst);
  128. printf("%d %d\n", fst, arr.back());
  129. }
  130. if (T) puts("");
  131. }
  132. return 0;
  133. }
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值