思路完全同P2085,用大根堆,如果比堆顶小,就把堆顶的换下来,比堆顶大就扔掉不要break。
这种思路看起来很好用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include<iostream> #include<cmath> #include<algorithm> #include<queue> #include<stack> #define ll long long #define pii pair<int,int> #define PINF 0x7fffffff #define NINF 0x80000000 using namespace std; priority_queue<ll> pq; stack<ll> s; ll n, A[100005], B[100005]; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++)cin >> A[i]; for (int i = 1; i <= n; i++)cin >> B[i]; for (int i = 1; i <= n; i++) { pq.push(A[1] + B[i]); } for (int i = 2; i <= n; i++) { for (int j = 1; j <= n; j++) { if (pq.top() > A[i] + B[j]) { pq.pop(); pq.push(A[i] + B[j]); } else break; } } while (!pq.empty()) { s.push(pq.top()); pq.pop(); } while (!s.empty()) { cout << s.top() << " "; s.pop(); } } |