按T2-T1排序为什么会WA一个点呢?
查看原帖
按T2-T1排序为什么会WA一个点呢?
10337
ZPC2048楼主2020/9/15 09:58

如题,我按照T2-T1的顺序而不是T2的顺序进行升序排序,先选择T2-T1更小的修复,为什么会WA一个点呢...求解答或者数据...

原WA一个点的代码:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

const int MAXN = 2 * 1e5;

struct building {
  long long t1, t2, left;
  building(int t1 = 0, int t2 = 0, int left = 0) {
    this -> t1 = t1;
    this -> t2 = t2;
    this -> left = left;
  }
};

struct cmp1 {
  inline bool operator ()(const building A, const building B) {
    return A.left > B.left;
  }
};

struct cmp2 {
  inline bool operator ()(const building A, const building B) {
    return A.t1 < B.t1;
  }
};

priority_queue<building, vector<building>, cmp1> input;
priority_queue<building, vector<building>, cmp2> ans;
int sum = 0;

int main() {
  building temp;
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> temp.t1 >> temp.t2;
    temp.left = temp.t2 - temp.t1;
    input.push(temp);
  }
  while (!input.empty()) {
    temp = input.top();
    input.pop();
    if (sum > temp.left && (!ans.empty()) && temp.t1 >= ans.top().t1) continue;
    if (sum > temp.left && (!ans.empty()) && temp.t1 < ans.top().t1) {
      sum -= ans.top().t1;
      sum += temp.t1;
      ans.pop();
      ans.push(temp);
    } else {
      sum += temp.t1;
      ans.push(temp);
    }
  }
  cout << ans.size() << endl;
  return 0;
}

修改后AC的代码:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

const int MAXN = 2 * 1e5;

struct building {
  long long t1, t2, left;
  building(int t1 = 0, int t2 = 0, int left = 0) {
    this -> t1 = t1;
    this -> t2 = t2;
    this -> left = left;
  }
};

struct cmp1 {
  inline bool operator ()(const building A, const building B) {
    return A.t2 > B.t2;
  }
};

struct cmp2 {
  inline bool operator ()(const building A, const building B) {
    return A.t1 < B.t1;
  }
};

priority_queue<building, vector<building>, cmp1> input;
priority_queue<building, vector<building>, cmp2> ans;
int sum = 0;

int main() {
  building temp;
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> temp.t1 >> temp.t2;
    temp.left = temp.t2 - temp.t1;
    input.push(temp);
  }
  while (!input.empty()) {
    temp = input.top();
    input.pop();
    if (sum > temp.left && (!ans.empty()) && temp.t1 >= ans.top().t1) continue;
    if (sum > temp.left && (!ans.empty()) && temp.t1 < ans.top().t1) {
      sum -= ans.top().t1;
      sum += temp.t1;
      ans.pop();
      ans.push(temp);
    } else {
      sum += temp.t1;
      ans.push(temp);
    }
  }
  cout << ans.size() << endl;
  return 0;
}
2020/9/15 09:58
加载中...