section3-5.cpp 2.1 KB

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