# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1056350 |
2024-08-13T08:58:29 Z |
dozer |
Catfish Farm (IOI22_fish) |
C++17 |
|
1000 ms |
29660 KB |
#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n"
#define pii pair<int, int>
#define st first
#define nd second
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define ll long long
#define LL node * 2
#define RR node * 2 + 1
#define MAXN 300005
const int modulo = 1e9 + 7;
const ll INF = 2e18 + 7;
long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
int n = N, m = M;
vector<array<int, 3>> v;
for (int i = 0; i < m; i++) v.pb({X[i], Y[i], W[i]});
sort(v.begin(), v.end());
vector<int> x(m, 0), y(m, 0), w(m, 0);
for (int i = 0; i < m; i++){
x[i] = v[i][0], y[i] = v[i][1], w[i] = v[i][2];
}
vector<vector<ll>> dp(2, vector<ll> (m + 5, 0));
vector<vector<ll>> start(2, vector<ll> (n + 5, 0));
vector<vector<pii>> fish(n + 5);
vector<ll> pre(m + 5, 0);
pre[0] = w[0];
for (int i = 1; i < m; i++) pre[i] = pre[i - 1] + w[i];
auto sum = [&](int l, int r){
ll ans = pre[r];
if (l > 0) ans -= pre[l - 1];
return ans;
};
for (int i = 0; i < m; i++) fish[x[i]].pb({y[i], i});
int it = n - 1;
for (int i = m - 1; i >= 0; i--){
while(it >= x[i]){
//cout<<it + 1<<" : "<<start[0][it + 1]<<sp<<start[1][it + 1]<<endl;
start[0][it] = start[0][it + 1];
start[1][it] = start[1][it + 1];
it--;
}
for (int j = 0; j < 2; j++){
int a = x[i], b = y[i];
if (j == 0){ // increasing
//todo : continue increasing
int k = lower_bound(fish[a + 1].begin(), fish[a + 1].end(), make_pair(b - 1, 0)) - fish[a + 1].begin();
for (int l = k; l < fish[a + 1].size(); l++){
int curr = fish[a + 1][l].st, to = fish[a + 1][l].nd;
int pos = lower_bound(fish[a].begin(), fish[a].end(), make_pair(curr, 0)) - fish[a].end();
if (pos > 0){
pos--;
ll c = sum(i, pre[fish[a][pos].nd]);
dp[j][i] = max(dp[j][i], dp[j][to] + c);
}
}
// todo : make the next column full and then decrease
ll c = sum(i, fish[a].back().nd);
if (a + 2 < n){
for (auto k : fish[a + 2]){
ll c2 = sum(k.nd, fish[a + 2].back().nd);
dp[j][i] = max(dp[j][i], dp[1][k.nd] + c + c2);
}
}
else if (a + 1 < n){
dp[j][i] = max(dp[j][i], c);
}
// todo: make the next 2 columns full
if (a + 3 < n) dp[j][i] = max(dp[j][i], start[1][a + 3] + c);
ll pref = 0;
if (a > 0){
int k = lower_bound(fish[a - 1].begin(), fish[a - 1].end(), make_pair(b, 0)) - fish[a - 1].begin();
k--;
if (k >= 0){
pref = sum(fish[a - 1].front().nd, fish[a - 1][k].nd);
}
}
start[0][a] = max(start[0][a], dp[j][i] + pref);
}
else{ // decreasing
//todo : continue decreasing
int k = lower_bound(fish[a + 1].begin(), fish[a + 1].end(), make_pair(b, 0)) - fish[a + 1].end();
k--;
for (int l = 0; l <= k; l++){
int curr = fish[a + 1][l].st, to = fish[a + 1][l].nd;
ll c = sum(to, fish[a + 1][k].nd);
dp[j][i] = max(dp[j][i], dp[j][to] + c);
}
//todo : decrease to 0 and then increase
ll c = 0;
if (k >= 0) c = sum(fish[a + 1][0].nd, fish[a + 1][k].nd);
if (a + 2 < n){
for (auto l : fish[a + 2]){
int maks = max(l.st, b);
int k = lower_bound(fish[a + 1].begin(), fish[a + 1].end(), make_pair(maks, 0)) - fish[a + 1].end();
k--;
if (k >= 0){
c = sum(fish[a + 1][0].nd, fish[a + 1][k].nd);
dp[j][i] = max(dp[j][i], dp[0][l.nd] + c);
}
}
}
else if (a + 1 < n) {
dp[j][i] = max(dp[j][i], c);
}
//todo make the next 2 columns 0
if (k >= 0) c = sum(fish[a + 1][0].nd, fish[a + 1][k].nd);
else c = 0;
if (a + 3 < n){
dp[j][i] = max(dp[j][i], c + start[0][a + 3]);
}
ll suf = sum(i, fish[a].back().nd);
start[1][a] = max(start[1][a], dp[j][i] + suf);
}
//cout<<i<<sp<<j<<sp<<" : "<<dp[j][i]<<endl;
}
}
while(it >= 0){
//cout<<it + 1<<" : "<<start[0][it + 1]<<sp<<start[1][it + 1]<<endl;
start[0][it] = start[0][it + 1];
start[1][it] = start[1][it + 1];
it--;
}
return start[0][0];
}
/*
int main() {
fileio();
int N, M;
assert(2 == scanf("%d %d", &N, &M));
std::vector<int> X(M), Y(M), W(M);
for (int i = 0; i < M; ++i) {
assert(3 == scanf("%d %d %d", &X[i], &Y[i], &W[i]));
}
long long result = max_weights(N, M, X, Y, W);
printf("%lld\n", result);
return 0;
}*/
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:64:26: 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 | for (int l = k; l < fish[a + 1].size(); l++){
| ~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:108:13: warning: unused variable 'curr' [-Wunused-variable]
108 | int curr = fish[a + 1][l].st, to = fish[a + 1][l].nd;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
11468 KB |
Output is correct |
2 |
Correct |
28 ms |
12984 KB |
Output is correct |
3 |
Correct |
2 ms |
4172 KB |
Output is correct |
4 |
Correct |
2 ms |
4200 KB |
Output is correct |
5 |
Correct |
95 ms |
29292 KB |
Output is correct |
6 |
Incorrect |
94 ms |
29660 KB |
1st lines differ - on the 1st token, expected: '300000000000000', found: '213394000000000' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
1046 ms |
18112 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4172 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4172 KB |
1st lines differ - on the 1st token, expected: '882019', found: '0' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '3', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '3', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '3', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4172 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4172 KB |
1st lines differ - on the 1st token, expected: '882019', found: '0' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
11468 KB |
Output is correct |
2 |
Correct |
28 ms |
12984 KB |
Output is correct |
3 |
Correct |
2 ms |
4172 KB |
Output is correct |
4 |
Correct |
2 ms |
4200 KB |
Output is correct |
5 |
Correct |
95 ms |
29292 KB |
Output is correct |
6 |
Incorrect |
94 ms |
29660 KB |
1st lines differ - on the 1st token, expected: '300000000000000', found: '213394000000000' |
7 |
Halted |
0 ms |
0 KB |
- |