笔试时写的O(n2),过70%,笔试后想到的O(nlongn),(这里假设冲突对不会重复) #include<iostream> (5488)#include<algorithm> #include<vector> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> a(n), b(n); vector<int> del(n); int sup = 0; for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i]; sup += b[i]; del[i] = b[i] - a[i]; } vector<int> ans(n, sup); for (int i = 0; i < n; ++i) ans[i] += n * b[i]; sort(del.begin(), del.end()); vector<int> sum(n + 1, 0); for (int i = 0; i < n; ++i) sum[i + 1] = sum[i] + del[i]; for (int i = 0; i < n; ++i) { int curDel = b[i] - a[i]; int idx = upper_bound(del.begin(), del.end(), curDel)- del.begin(); ans[i] -= sum[n] - sum[idx]; ans[i] -= idx * curDel; ans[i] -= b[i] + a[i]; } for (int i = 0; i < m; ++i) { int l, r; cin >> l >> r; --l; --r; int curDel = min(a[l] + b[r], a[r] + b[l]); ans[l] -= curDel; ans[r] -= curDel; } for (int i = 0; i < n; ++i) cout << ans[i] << " "; return 0; }
1 3

相关推荐

牛客网
牛客企业服务