This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
bool issub1 = 1;
for (int i = 0; i < m; i++) {
if (x[i] & 1) {
issub1 = 0;
break;
}
}
if (issub1) {
// just sum up every thing
ll ans = accumulate(w.begin(), w.end(), 0LL);
return ans;
}
bool issub2 = 1;
for (int i = 0; i < m; i++) {
if (x[i] > 1) {
issub2 = 0;
break;
}
}
if (issub2) {
// there is only two columns, BUT be careful since n can be > 2
if (n == 1) {
// you can't choose anything
return 0;
}
if (n == 2) {
// only two columns
vector<ll> col(2);
for (int i = 0; i < m; i++) {
col[x[i]] += w[i];
}
return max(col[0], col[1]);
}
// prefix of col[0] and suffix of col[1]
// 2 pointer
vector<vector<pair<int, int>>> v(2);
for (int i = 0; i < m; i++) {
v[x[i]].push_back({y[i], w[i]});
}
for (int j = 0; j < 2; j++) {
sort(v[j].begin(), v[j].end());
}
ll all2 = 0;
for (auto i : v[1]) all2 += i.second;
ll ans = all2;
int j = 0;
ll cur1 = 0;
while (j < v[0].size() && v[0][j].first <= v[1][0].first) {
cur1 += v[0][j].second;
j++;
}
ans = max(ans, cur1 + all2);
for (int i = 0; i < v[1].size(); i++) {
all2 -= v[1][i].second;
while (j < v[0].size() && v[0][j].first <= v[1][i].first) {
cur1 += v[0][j].second;
j++;
}
ans = max(ans, all2 + cur1);
}
return ans;
}
return 0;
}
//int main() {
// int tmp = 2e9;
//// cout << max_weights(5, 4, {1, 0, 1, 0}, {2, 1, 4, 3}, {tmp, tmp, tmp, tmp}) << '\n';
//// cout << max_weights(5, 4, {0, 1, 4, 3}, {2, 1, 4, 3}, {5, 2, 1, 3}) << '\n';
// cout << max_weights(2, 4, {1, 0, 1, 0}, {2, 1, 4, 3}, {tmp, tmp, tmp, tmp}) << '\n';
// cout << max_weights(1, 4, {0, 0, 0, 0}, {2, 1, 4, 3}, {tmp, tmp, tmp, tmp}) << '\n';
// cout << max_weights(3, 4, {1, 0, 1, 0}, {2, 1, 4, 3}, {0, 0, tmp, tmp}) << '\n';
// cout << max_weights(3, 2, {0, 1}, {1, 2}, {1, 1}) << '\n';
//}
Compilation message (stderr)
fish.cpp: In function 'll max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:64:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | while (j < v[0].size() && v[0][j].first <= v[1][0].first) {
| ~~^~~~~~~~~~~~~
fish.cpp:69:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int i = 0; i < v[1].size(); i++) {
| ~~^~~~~~~~~~~~~
fish.cpp:71:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | while (j < v[0].size() && v[0][j].first <= v[1][i].first) {
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |