Submission #1040213

# Submission time Handle Problem Language Result Execution time Memory
1040213 2024-07-31T18:33:14 Z vnm06 Catfish Farm (IOI22_fish) C++17
100 / 100
298 ms 65872 KB
#include<bits/stdc++.h>
#include "fish.h"
#include <vector>

using namespace std;

int n, m;
vector<long long> sums[100005], height[100005];
vector<long long> dp[100005], dp2[100005], best_dp[100005];
vector<pair<int, int> > pos[100005];
long long bests[100005];
long long bests_osak[100005];

long long get_sum(int row, int k)
{
  if(row<0 || row>=n) return 0;
  if(sums[row].size()==0) return 0;
  int t=sums[row].size();
  if(pos[row][0].first>k) return 0;
  if(pos[row][t-1].first<=k) return sums[row][t-1];
  int le=0, ri=t;
  while(ri-le>1)
  {
    int mid=((le+ri)>>1);
    if(pos[row][mid-1].first<=k) le=mid;
    else ri=mid;
  }
  return sums[row][le-1];
}

long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> W) 
{
  long long otg=0;
  n=N;
  m=M;
  for(int i=0; i<M; i++)
  {
    pos[X[i]].push_back({Y[i], W[i]});
  }
  for(int i=0; i<N; i++) 
  {
    sort(pos[i].begin(), pos[i].end());
    long long s=0;
    for(int j=0; j<pos[i].size(); j++)
    {
      s+=pos[i][j].second;
      sums[i].push_back(s);
    }
  }
  for(int i=0; i<N; i++)
  {
    if(i) for(int j=0; j<pos[i-1].size(); j++) height[i].push_back(pos[i-1][j].first);
    if(i<N-1) for(int j=0; j<pos[i+1].size(); j++) height[i].push_back(pos[i+1][j].first);
    sort(height[i].begin(), height[i].end());
    int id_l=-1;
    long long stv=0;
    if(i) {bests[i]=bests[i-1]; bests_osak[i]=bests_osak[i-1];}
    dp[i].resize(height[i].size());
    dp2[i].resize(height[i].size());
    for(int t=0; t<height[i].size(); t++)
    {
      long long ans=0, k=height[i][t], ans2=0;
      if(t && k==height[i][t-1]) continue;
      //ako ne se snizhava (mozhe i da e pyrvo)
      if(i>=3) ans=max(ans, bests[i-3]+get_sum(i-1, k)+get_sum(i+1, k));
      if(i>=2)
      {
        ans=max(ans, bests[i-2]+get_sum(i+1, k));
        ans=max(ans, bests_osak[i-2]+get_sum(i-1, k)+get_sum(i+1, k));
      }
      if(i)
      {
        while(id_l+1<height[i-1].size() && height[i-1][id_l+1]<=k) 
        {
          id_l++;
          stv=max(stv, dp[i-1][id_l]-get_sum(i, height[i-1][id_l])-get_sum(i-1, height[i-1][id_l]));
          /////cout<<id_l<<endl;
        }
      }
      ans=max(ans, stv+get_sum(i-1, k)+get_sum(i+1, k));
      dp[i][t]=ans;
      otg=max(otg, ans);
      bests[i]=max(bests[i], dp[i][t]);
      bests_osak[i]=max(bests_osak[i], dp[i][t]-get_sum(i+1, k));

      //cout<<t<<endl;
      if(i && id_l+1<best_dp[i-1].size()) ans2=best_dp[i-1][id_l+1]-get_sum(i, k)+get_sum(i+1, k);
      dp2[i][t]=ans2;
      otg=max(otg, ans2);
      bests[i]=max(bests[i], dp2[i][t]);
      bests_osak[i]=max(bests_osak[i], dp2[i][t]-get_sum(i+1, k));
    
      //cout<<i<<" "<<height[i][t]<<" "<<dp[i][t]<<" "<<dp2[i][t]<<endl;
      //ako trygne da se snizhava
    
    }
    for(int t=0; t<height[i].size(); t++)
    {
      best_dp[i].push_back(max(dp[i][t], dp2[i][t]));
    }
    for(int t=height[i].size()-2; t>=0; t--) best_dp[i][t]=max(best_dp[i][t], best_dp[i][t+1]);
  }
  return otg;
}

Compilation message

fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:44:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |     for(int j=0; j<pos[i].size(); j++)
      |                  ~^~~~~~~~~~~~~~
fish.cpp:52:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |     if(i) for(int j=0; j<pos[i-1].size(); j++) height[i].push_back(pos[i-1][j].first);
      |                        ~^~~~~~~~~~~~~~~~
fish.cpp:53:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |     if(i<N-1) for(int j=0; j<pos[i+1].size(); j++) height[i].push_back(pos[i+1][j].first);
      |                            ~^~~~~~~~~~~~~~~~
fish.cpp:60:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     for(int t=0; t<height[i].size(); t++)
      |                  ~^~~~~~~~~~~~~~~~~
fish.cpp:73:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |         while(id_l+1<height[i-1].size() && height[i-1][id_l+1]<=k)
      |               ~~~~~~^~~~~~~~~~~~~~~~~~~
fish.cpp:87:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |       if(i && id_l+1<best_dp[i-1].size()) ans2=best_dp[i-1][id_l+1]-get_sum(i, k)+get_sum(i+1, k);
      |               ~~~~~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:97:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |     for(int t=0; t<height[i].size(); t++)
      |                  ~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 31 ms 23120 KB Output is correct
2 Correct 38 ms 24912 KB Output is correct
3 Correct 7 ms 15964 KB Output is correct
4 Correct 7 ms 15964 KB Output is correct
5 Correct 206 ms 54112 KB Output is correct
6 Correct 145 ms 62544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14424 KB Output is correct
2 Correct 91 ms 32760 KB Output is correct
3 Correct 102 ms 36780 KB Output is correct
4 Correct 30 ms 23124 KB Output is correct
5 Correct 38 ms 24908 KB Output is correct
6 Correct 6 ms 14424 KB Output is correct
7 Correct 5 ms 14428 KB Output is correct
8 Correct 5 ms 14376 KB Output is correct
9 Correct 5 ms 14428 KB Output is correct
10 Correct 6 ms 15964 KB Output is correct
11 Correct 7 ms 16076 KB Output is correct
12 Correct 46 ms 25692 KB Output is correct
13 Correct 56 ms 27996 KB Output is correct
14 Correct 39 ms 24416 KB Output is correct
15 Correct 50 ms 25564 KB Output is correct
16 Correct 60 ms 24456 KB Output is correct
17 Correct 44 ms 25488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 15960 KB Output is correct
2 Correct 7 ms 15964 KB Output is correct
3 Correct 35 ms 28540 KB Output is correct
4 Correct 24 ms 24924 KB Output is correct
5 Correct 49 ms 38736 KB Output is correct
6 Correct 48 ms 37976 KB Output is correct
7 Correct 52 ms 38736 KB Output is correct
8 Correct 52 ms 38732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 14424 KB Output is correct
2 Correct 6 ms 14428 KB Output is correct
3 Correct 6 ms 14528 KB Output is correct
4 Correct 5 ms 14428 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 6 ms 14424 KB Output is correct
7 Correct 5 ms 14428 KB Output is correct
8 Correct 6 ms 14536 KB Output is correct
9 Correct 6 ms 14428 KB Output is correct
10 Correct 9 ms 14940 KB Output is correct
11 Correct 6 ms 14656 KB Output is correct
12 Correct 6 ms 14664 KB Output is correct
13 Correct 8 ms 14428 KB Output is correct
14 Correct 7 ms 14684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 14424 KB Output is correct
2 Correct 6 ms 14428 KB Output is correct
3 Correct 6 ms 14528 KB Output is correct
4 Correct 5 ms 14428 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 6 ms 14424 KB Output is correct
7 Correct 5 ms 14428 KB Output is correct
8 Correct 6 ms 14536 KB Output is correct
9 Correct 6 ms 14428 KB Output is correct
10 Correct 9 ms 14940 KB Output is correct
11 Correct 6 ms 14656 KB Output is correct
12 Correct 6 ms 14664 KB Output is correct
13 Correct 8 ms 14428 KB Output is correct
14 Correct 7 ms 14684 KB Output is correct
15 Correct 6 ms 14428 KB Output is correct
16 Correct 6 ms 14716 KB Output is correct
17 Correct 31 ms 20052 KB Output is correct
18 Correct 30 ms 21852 KB Output is correct
19 Correct 26 ms 21280 KB Output is correct
20 Correct 26 ms 21312 KB Output is correct
21 Correct 28 ms 21332 KB Output is correct
22 Correct 54 ms 28136 KB Output is correct
23 Correct 13 ms 15708 KB Output is correct
24 Correct 25 ms 18512 KB Output is correct
25 Correct 7 ms 14684 KB Output is correct
26 Correct 10 ms 15452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 14424 KB Output is correct
2 Correct 6 ms 14428 KB Output is correct
3 Correct 6 ms 14528 KB Output is correct
4 Correct 5 ms 14428 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 6 ms 14424 KB Output is correct
7 Correct 5 ms 14428 KB Output is correct
8 Correct 6 ms 14536 KB Output is correct
9 Correct 6 ms 14428 KB Output is correct
10 Correct 9 ms 14940 KB Output is correct
11 Correct 6 ms 14656 KB Output is correct
12 Correct 6 ms 14664 KB Output is correct
13 Correct 8 ms 14428 KB Output is correct
14 Correct 7 ms 14684 KB Output is correct
15 Correct 6 ms 14428 KB Output is correct
16 Correct 6 ms 14716 KB Output is correct
17 Correct 31 ms 20052 KB Output is correct
18 Correct 30 ms 21852 KB Output is correct
19 Correct 26 ms 21280 KB Output is correct
20 Correct 26 ms 21312 KB Output is correct
21 Correct 28 ms 21332 KB Output is correct
22 Correct 54 ms 28136 KB Output is correct
23 Correct 13 ms 15708 KB Output is correct
24 Correct 25 ms 18512 KB Output is correct
25 Correct 7 ms 14684 KB Output is correct
26 Correct 10 ms 15452 KB Output is correct
27 Correct 7 ms 15196 KB Output is correct
28 Correct 186 ms 45760 KB Output is correct
29 Correct 206 ms 53992 KB Output is correct
30 Correct 188 ms 55096 KB Output is correct
31 Correct 202 ms 55140 KB Output is correct
32 Correct 169 ms 58996 KB Output is correct
33 Correct 191 ms 55040 KB Output is correct
34 Correct 195 ms 55016 KB Output is correct
35 Correct 67 ms 31312 KB Output is correct
36 Correct 203 ms 59116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 15960 KB Output is correct
2 Correct 7 ms 15964 KB Output is correct
3 Correct 35 ms 28540 KB Output is correct
4 Correct 24 ms 24924 KB Output is correct
5 Correct 49 ms 38736 KB Output is correct
6 Correct 48 ms 37976 KB Output is correct
7 Correct 52 ms 38736 KB Output is correct
8 Correct 52 ms 38732 KB Output is correct
9 Correct 53 ms 38480 KB Output is correct
10 Correct 49 ms 31828 KB Output is correct
11 Correct 96 ms 49132 KB Output is correct
12 Correct 5 ms 14424 KB Output is correct
13 Correct 6 ms 14544 KB Output is correct
14 Correct 5 ms 14428 KB Output is correct
15 Correct 5 ms 14536 KB Output is correct
16 Correct 5 ms 14456 KB Output is correct
17 Correct 5 ms 14424 KB Output is correct
18 Correct 6 ms 15964 KB Output is correct
19 Correct 6 ms 15964 KB Output is correct
20 Correct 9 ms 15964 KB Output is correct
21 Correct 7 ms 16036 KB Output is correct
22 Correct 57 ms 37072 KB Output is correct
23 Correct 95 ms 49236 KB Output is correct
24 Correct 94 ms 49748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 23120 KB Output is correct
2 Correct 38 ms 24912 KB Output is correct
3 Correct 7 ms 15964 KB Output is correct
4 Correct 7 ms 15964 KB Output is correct
5 Correct 206 ms 54112 KB Output is correct
6 Correct 145 ms 62544 KB Output is correct
7 Correct 5 ms 14424 KB Output is correct
8 Correct 91 ms 32760 KB Output is correct
9 Correct 102 ms 36780 KB Output is correct
10 Correct 30 ms 23124 KB Output is correct
11 Correct 38 ms 24908 KB Output is correct
12 Correct 6 ms 14424 KB Output is correct
13 Correct 5 ms 14428 KB Output is correct
14 Correct 5 ms 14376 KB Output is correct
15 Correct 5 ms 14428 KB Output is correct
16 Correct 6 ms 15964 KB Output is correct
17 Correct 7 ms 16076 KB Output is correct
18 Correct 46 ms 25692 KB Output is correct
19 Correct 56 ms 27996 KB Output is correct
20 Correct 39 ms 24416 KB Output is correct
21 Correct 50 ms 25564 KB Output is correct
22 Correct 60 ms 24456 KB Output is correct
23 Correct 44 ms 25488 KB Output is correct
24 Correct 6 ms 15960 KB Output is correct
25 Correct 7 ms 15964 KB Output is correct
26 Correct 35 ms 28540 KB Output is correct
27 Correct 24 ms 24924 KB Output is correct
28 Correct 49 ms 38736 KB Output is correct
29 Correct 48 ms 37976 KB Output is correct
30 Correct 52 ms 38736 KB Output is correct
31 Correct 52 ms 38732 KB Output is correct
32 Correct 6 ms 14424 KB Output is correct
33 Correct 6 ms 14428 KB Output is correct
34 Correct 6 ms 14528 KB Output is correct
35 Correct 5 ms 14428 KB Output is correct
36 Correct 6 ms 14428 KB Output is correct
37 Correct 6 ms 14424 KB Output is correct
38 Correct 5 ms 14428 KB Output is correct
39 Correct 6 ms 14536 KB Output is correct
40 Correct 6 ms 14428 KB Output is correct
41 Correct 9 ms 14940 KB Output is correct
42 Correct 6 ms 14656 KB Output is correct
43 Correct 6 ms 14664 KB Output is correct
44 Correct 8 ms 14428 KB Output is correct
45 Correct 7 ms 14684 KB Output is correct
46 Correct 6 ms 14428 KB Output is correct
47 Correct 6 ms 14716 KB Output is correct
48 Correct 31 ms 20052 KB Output is correct
49 Correct 30 ms 21852 KB Output is correct
50 Correct 26 ms 21280 KB Output is correct
51 Correct 26 ms 21312 KB Output is correct
52 Correct 28 ms 21332 KB Output is correct
53 Correct 54 ms 28136 KB Output is correct
54 Correct 13 ms 15708 KB Output is correct
55 Correct 25 ms 18512 KB Output is correct
56 Correct 7 ms 14684 KB Output is correct
57 Correct 10 ms 15452 KB Output is correct
58 Correct 7 ms 15196 KB Output is correct
59 Correct 186 ms 45760 KB Output is correct
60 Correct 206 ms 53992 KB Output is correct
61 Correct 188 ms 55096 KB Output is correct
62 Correct 202 ms 55140 KB Output is correct
63 Correct 169 ms 58996 KB Output is correct
64 Correct 191 ms 55040 KB Output is correct
65 Correct 195 ms 55016 KB Output is correct
66 Correct 67 ms 31312 KB Output is correct
67 Correct 203 ms 59116 KB Output is correct
68 Correct 53 ms 38480 KB Output is correct
69 Correct 49 ms 31828 KB Output is correct
70 Correct 96 ms 49132 KB Output is correct
71 Correct 5 ms 14424 KB Output is correct
72 Correct 6 ms 14544 KB Output is correct
73 Correct 5 ms 14428 KB Output is correct
74 Correct 5 ms 14536 KB Output is correct
75 Correct 5 ms 14456 KB Output is correct
76 Correct 5 ms 14424 KB Output is correct
77 Correct 6 ms 15964 KB Output is correct
78 Correct 6 ms 15964 KB Output is correct
79 Correct 9 ms 15964 KB Output is correct
80 Correct 7 ms 16036 KB Output is correct
81 Correct 57 ms 37072 KB Output is correct
82 Correct 95 ms 49236 KB Output is correct
83 Correct 94 ms 49748 KB Output is correct
84 Correct 269 ms 53552 KB Output is correct
85 Correct 298 ms 52948 KB Output is correct
86 Correct 165 ms 64596 KB Output is correct
87 Correct 170 ms 65872 KB Output is correct
88 Correct 6 ms 14424 KB Output is correct
89 Correct 160 ms 65808 KB Output is correct
90 Correct 173 ms 61520 KB Output is correct
91 Correct 141 ms 59476 KB Output is correct