section3-5.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // section35.cpp: 定义控制台应用程序的入口点。
  2. //
  3. #include"stdafx.h"
  4. #include<iostream>
  5. #include<vector>
  6. #include<string>
  7. #include<algorithm>
  8. using namespace std;
  9. struct Wooden
  10. {
  11. int length;
  12. int weight;
  13. string status;
  14. };
  15. const string CURRENT = "current";
  16. const string DELETED = "delete";
  17. bool compare(Wooden a, Wooden b) {
  18. return (a.length < b.length) && (a.weight < b.weight);
  19. }
  20. int calc(vector<Wooden> woodens) {
  21. int size = woodens.size();
  22. if (size == 0) {
  23. return 0;
  24. }
  25. if (size == 1) {
  26. return 1;
  27. }
  28. int cost = 0;
  29. int index = -1;
  30. Wooden node;
  31. for (int i = 0; i < size; i++) {
  32. if (node.status != CURRENT)
  33. {
  34. if (DELETED == woodens.at(i).status) {
  35. continue;
  36. }
  37. else {
  38. woodens.at(i).status = CURRENT;
  39. cost++;
  40. node = woodens[i];
  41. index = i;
  42. }
  43. }
  44. if (CURRENT == woodens.at(i).status) {
  45. continue;
  46. }
  47. if (woodens.at(i).length >= node.length && woodens.at(i).weight >= node.weight) {
  48. woodens.at(i).status = DELETED;
  49. continue;
  50. }
  51. if (woodens.at(i).length <= node.length &&woodens.at(i).weight <= node.weight) {
  52. woodens.at(index).status = DELETED;
  53. woodens[i].status = CURRENT;
  54. node = woodens[i];
  55. index = i;
  56. i = -1;
  57. continue;
  58. }
  59. if (i == (size - 1)) {
  60. woodens.at(index).status = DELETED;
  61. index = -1;
  62. node = Wooden{};
  63. }
  64. }
  65. return cost;
  66. }
  67. int main()
  68. {
  69. int T, n;
  70. vector<Wooden> woodens;
  71. int l, w;
  72. while (cin >> T) {
  73. for (int i = 0; i < T; i++) {
  74. cin >> n;
  75. for (int j = 0; j < n; j++) {
  76. cin >> l >> w;
  77. woodens.push_back(Wooden{ l, w, "" });
  78. }
  79. cout << calc(woodens) << endl;
  80. woodens.clear();
  81. }
  82. }
  83. return 0;
  84. }