Submission #644348

# Submission time Handle Problem Language Result Execution time Memory
644348 2022-09-24T11:58:07 Z NintsiChkhaidze Catfish Farm (IOI22_fish) C++17
100 / 100
475 ms 132032 KB
#include "fish.h"
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define s second
#define f first
using namespace std;
 
const int N = 100005;
ll dp[N][5];
vector <ll> p[N],pref[N];
vector <pair<ll,ll> > prc[N],v[N],suff[N];
 
ll max_weights(int n, int m, vector<int> X, vector<int> Y, vector<int> W) {
 
  for (int i = 0;i < m; i++){
    X[i]++,Y[i]++;
    v[X[i] + 1].pb({Y[i],0});
    v[X[i] - 1].pb({Y[i],0});
    v[X[i]].pb({Y[i],(ll)W[i]});
  }

  for (int j = 1; j <= n; j++){
  	v[j].pb({0,0LL}),v[j].pb({n,0LL});
  	sort(v[j].begin(),v[j].end());

    vector <pair<ll,ll>> vec; vec.clear();
    vec.pb(v[j][0]);
    for (int i = 1; i < v[j].size(); i++){
      if (v[j][i].f == vec.back().f) vec[vec.size() - 1].s += v[j][i].s;
      else vec.pb(v[j][i]);
    }
    
    v[j] = vec;
    p[j].pb(v[j][0].s);
    for (int i = 1; i < v[j].size(); i++)
      p[j].pb(v[j][i].s + p[j][i - 1]);
  }

  for (int j = 1; j <= n; j++){
    for (int i = 0; i < v[j].size(); i++){
      ll a = v[j][i].f,val2 =0,val=0,valp = 0,vals=0;
      int l,r;
      if (j > 1){
        l = 0,r = v[j - 1].size() - 1; 
        while (l <= r){
          int mid = (l+r)>>1;
          if (v[j - 1][mid].f <= a) {
            valp = pref[j - 1][mid];
            val2 = p[j - 1][mid];
            l = mid + 1;
          }
          else r = mid - 1;
        }
      }
      
      l=0,r=suff[j-1].size()-1,vals=0; 
      while (l<=r){
        int mid= (l+r)>>1;
        if (suff[j - 1][mid].f >= a) {
          vals = suff[j - 1][mid].s;
          l = mid + 1;
        } else r = mid - 1;
      }
      
      dp[a][0] = vals - p[j][i];
      dp[a][1] = valp + val2; 
      if (j < 3) continue;
      l = 0,r = prc[j - 2].size() - 1,val = 0;
      while (l<=r){
        int mid= (l+r)>>1;
        if (prc[j - 2][mid].f <= a) 
            val = prc[j - 2][mid].s,l=mid+1;
        else r = mid-1;
      }
      
      dp[a][1] = max(dp[a][1], val + val2);
    }

    if (j == n) break;
    ll ls = 0;
    for (int i=v[j].size() - 1;i >= 0; i--){
      ll a = v[j][i].f,val=0;
      int l =0,r = v[j + 1].size() - 1; 
      while (l<=r){
        int mid=(l+r)>>1;
        if (v[j + 1][mid].f <= a){
          val = p[j + 1][mid];
          l = mid + 1;
        }else r = mid - 1;
      }

      suff[j].pb({a,max(ls, max(dp[a][0],dp[a][1]) + val)});
      ls = max(ls, max(dp[a][0],dp[a][1]) + val);
    }
    
    ls = 0;
    for (int i=0;i<v[j].size();i++){
      ll a = v[j][i].f;
      pref[j].pb(max(ls,dp[a][1] - p[j][i]));
      ls= max(ls,dp[a][1] - p[j][i]);
    }
    
    ls=0;
    for (int i=0;i<v[j].size();i++){
      ll a = v[j][i].f;
      prc[j].pb({a,max(ls, max(dp[a][0],dp[a][1]))}); 
      ls = prc[j][i].s;
    }
    
  }
  
  ll ans = 0;
  for (int i=0;i<v[n].size();i++){
    int a = v[n][i].f;
    ans=max({ans,dp[a][0],dp[a][1]});
  }
      
  return ans;
}

Compilation message

fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:29:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for (int i = 1; i < v[j].size(); i++){
      |                     ~~^~~~~~~~~~~~~
fish.cpp:36:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |     for (int i = 1; i < v[j].size(); i++)
      |                     ~~^~~~~~~~~~~~~
fish.cpp:41:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |     for (int i = 0; i < v[j].size(); i++){
      |                     ~~^~~~~~~~~~~~~
fish.cpp:98:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |     for (int i=0;i<v[j].size();i++){
      |                  ~^~~~~~~~~~~~
fish.cpp:105:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |     for (int i=0;i<v[j].size();i++){
      |                  ~^~~~~~~~~~~~
fish.cpp:114:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |   for (int i=0;i<v[n].size();i++){
      |                ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 124 ms 49184 KB Output is correct
2 Correct 143 ms 54764 KB Output is correct
3 Correct 60 ms 32272 KB Output is correct
4 Correct 63 ms 32496 KB Output is correct
5 Correct 401 ms 112948 KB Output is correct
6 Correct 475 ms 132032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12052 KB Output is correct
2 Correct 195 ms 61232 KB Output is correct
3 Correct 226 ms 68996 KB Output is correct
4 Correct 122 ms 49076 KB Output is correct
5 Correct 142 ms 54756 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 6 ms 12032 KB Output is correct
10 Correct 60 ms 32328 KB Output is correct
11 Correct 59 ms 32388 KB Output is correct
12 Correct 144 ms 53792 KB Output is correct
13 Correct 175 ms 59960 KB Output is correct
14 Correct 136 ms 51872 KB Output is correct
15 Correct 138 ms 49476 KB Output is correct
16 Correct 135 ms 52272 KB Output is correct
17 Correct 148 ms 56080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 32332 KB Output is correct
2 Correct 59 ms 32288 KB Output is correct
3 Correct 116 ms 42556 KB Output is correct
4 Correct 96 ms 40864 KB Output is correct
5 Correct 163 ms 54220 KB Output is correct
6 Correct 158 ms 55012 KB Output is correct
7 Correct 158 ms 55540 KB Output is correct
8 Correct 155 ms 55544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 6 ms 12052 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 12116 KB Output is correct
9 Correct 9 ms 12116 KB Output is correct
10 Correct 8 ms 12500 KB Output is correct
11 Correct 7 ms 12244 KB Output is correct
12 Correct 7 ms 12284 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 7 ms 12248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 6 ms 12052 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 12116 KB Output is correct
9 Correct 9 ms 12116 KB Output is correct
10 Correct 8 ms 12500 KB Output is correct
11 Correct 7 ms 12244 KB Output is correct
12 Correct 7 ms 12284 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 7 ms 12248 KB Output is correct
15 Correct 6 ms 12244 KB Output is correct
16 Correct 7 ms 12372 KB Output is correct
17 Correct 41 ms 21476 KB Output is correct
18 Correct 38 ms 20648 KB Output is correct
19 Correct 40 ms 20472 KB Output is correct
20 Correct 35 ms 19888 KB Output is correct
21 Correct 34 ms 19824 KB Output is correct
22 Correct 64 ms 27420 KB Output is correct
23 Correct 15 ms 14676 KB Output is correct
24 Correct 34 ms 19224 KB Output is correct
25 Correct 7 ms 12372 KB Output is correct
26 Correct 15 ms 14460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 6 ms 12052 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 12116 KB Output is correct
9 Correct 9 ms 12116 KB Output is correct
10 Correct 8 ms 12500 KB Output is correct
11 Correct 7 ms 12244 KB Output is correct
12 Correct 7 ms 12284 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 7 ms 12248 KB Output is correct
15 Correct 6 ms 12244 KB Output is correct
16 Correct 7 ms 12372 KB Output is correct
17 Correct 41 ms 21476 KB Output is correct
18 Correct 38 ms 20648 KB Output is correct
19 Correct 40 ms 20472 KB Output is correct
20 Correct 35 ms 19888 KB Output is correct
21 Correct 34 ms 19824 KB Output is correct
22 Correct 64 ms 27420 KB Output is correct
23 Correct 15 ms 14676 KB Output is correct
24 Correct 34 ms 19224 KB Output is correct
25 Correct 7 ms 12372 KB Output is correct
26 Correct 15 ms 14460 KB Output is correct
27 Correct 10 ms 13888 KB Output is correct
28 Correct 167 ms 48352 KB Output is correct
29 Correct 270 ms 67140 KB Output is correct
30 Correct 346 ms 118324 KB Output is correct
31 Correct 341 ms 117468 KB Output is correct
32 Correct 213 ms 61644 KB Output is correct
33 Correct 352 ms 117672 KB Output is correct
34 Correct 361 ms 117516 KB Output is correct
35 Correct 110 ms 42060 KB Output is correct
36 Correct 322 ms 90728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 32332 KB Output is correct
2 Correct 59 ms 32288 KB Output is correct
3 Correct 116 ms 42556 KB Output is correct
4 Correct 96 ms 40864 KB Output is correct
5 Correct 163 ms 54220 KB Output is correct
6 Correct 158 ms 55012 KB Output is correct
7 Correct 158 ms 55540 KB Output is correct
8 Correct 155 ms 55544 KB Output is correct
9 Correct 179 ms 77392 KB Output is correct
10 Correct 101 ms 35288 KB Output is correct
11 Correct 221 ms 59032 KB Output is correct
12 Correct 6 ms 11988 KB Output is correct
13 Correct 6 ms 11960 KB Output is correct
14 Correct 6 ms 11988 KB Output is correct
15 Correct 6 ms 11988 KB Output is correct
16 Correct 6 ms 11988 KB Output is correct
17 Correct 7 ms 11988 KB Output is correct
18 Correct 61 ms 32332 KB Output is correct
19 Correct 60 ms 32396 KB Output is correct
20 Correct 58 ms 32332 KB Output is correct
21 Correct 67 ms 32436 KB Output is correct
22 Correct 205 ms 67992 KB Output is correct
23 Correct 260 ms 75396 KB Output is correct
24 Correct 269 ms 78144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 124 ms 49184 KB Output is correct
2 Correct 143 ms 54764 KB Output is correct
3 Correct 60 ms 32272 KB Output is correct
4 Correct 63 ms 32496 KB Output is correct
5 Correct 401 ms 112948 KB Output is correct
6 Correct 475 ms 132032 KB Output is correct
7 Correct 6 ms 12052 KB Output is correct
8 Correct 195 ms 61232 KB Output is correct
9 Correct 226 ms 68996 KB Output is correct
10 Correct 122 ms 49076 KB Output is correct
11 Correct 142 ms 54756 KB Output is correct
12 Correct 6 ms 11988 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 6 ms 11988 KB Output is correct
15 Correct 6 ms 12032 KB Output is correct
16 Correct 60 ms 32328 KB Output is correct
17 Correct 59 ms 32388 KB Output is correct
18 Correct 144 ms 53792 KB Output is correct
19 Correct 175 ms 59960 KB Output is correct
20 Correct 136 ms 51872 KB Output is correct
21 Correct 138 ms 49476 KB Output is correct
22 Correct 135 ms 52272 KB Output is correct
23 Correct 148 ms 56080 KB Output is correct
24 Correct 59 ms 32332 KB Output is correct
25 Correct 59 ms 32288 KB Output is correct
26 Correct 116 ms 42556 KB Output is correct
27 Correct 96 ms 40864 KB Output is correct
28 Correct 163 ms 54220 KB Output is correct
29 Correct 158 ms 55012 KB Output is correct
30 Correct 158 ms 55540 KB Output is correct
31 Correct 155 ms 55544 KB Output is correct
32 Correct 6 ms 11988 KB Output is correct
33 Correct 7 ms 11988 KB Output is correct
34 Correct 6 ms 11988 KB Output is correct
35 Correct 6 ms 11988 KB Output is correct
36 Correct 6 ms 11988 KB Output is correct
37 Correct 6 ms 12052 KB Output is correct
38 Correct 6 ms 11988 KB Output is correct
39 Correct 6 ms 12116 KB Output is correct
40 Correct 9 ms 12116 KB Output is correct
41 Correct 8 ms 12500 KB Output is correct
42 Correct 7 ms 12244 KB Output is correct
43 Correct 7 ms 12284 KB Output is correct
44 Correct 6 ms 11988 KB Output is correct
45 Correct 7 ms 12248 KB Output is correct
46 Correct 6 ms 12244 KB Output is correct
47 Correct 7 ms 12372 KB Output is correct
48 Correct 41 ms 21476 KB Output is correct
49 Correct 38 ms 20648 KB Output is correct
50 Correct 40 ms 20472 KB Output is correct
51 Correct 35 ms 19888 KB Output is correct
52 Correct 34 ms 19824 KB Output is correct
53 Correct 64 ms 27420 KB Output is correct
54 Correct 15 ms 14676 KB Output is correct
55 Correct 34 ms 19224 KB Output is correct
56 Correct 7 ms 12372 KB Output is correct
57 Correct 15 ms 14460 KB Output is correct
58 Correct 10 ms 13888 KB Output is correct
59 Correct 167 ms 48352 KB Output is correct
60 Correct 270 ms 67140 KB Output is correct
61 Correct 346 ms 118324 KB Output is correct
62 Correct 341 ms 117468 KB Output is correct
63 Correct 213 ms 61644 KB Output is correct
64 Correct 352 ms 117672 KB Output is correct
65 Correct 361 ms 117516 KB Output is correct
66 Correct 110 ms 42060 KB Output is correct
67 Correct 322 ms 90728 KB Output is correct
68 Correct 179 ms 77392 KB Output is correct
69 Correct 101 ms 35288 KB Output is correct
70 Correct 221 ms 59032 KB Output is correct
71 Correct 6 ms 11988 KB Output is correct
72 Correct 6 ms 11960 KB Output is correct
73 Correct 6 ms 11988 KB Output is correct
74 Correct 6 ms 11988 KB Output is correct
75 Correct 6 ms 11988 KB Output is correct
76 Correct 7 ms 11988 KB Output is correct
77 Correct 61 ms 32332 KB Output is correct
78 Correct 60 ms 32396 KB Output is correct
79 Correct 58 ms 32332 KB Output is correct
80 Correct 67 ms 32436 KB Output is correct
81 Correct 205 ms 67992 KB Output is correct
82 Correct 260 ms 75396 KB Output is correct
83 Correct 269 ms 78144 KB Output is correct
84 Correct 404 ms 102048 KB Output is correct
85 Correct 416 ms 104700 KB Output is correct
86 Correct 432 ms 124040 KB Output is correct
87 Correct 471 ms 128008 KB Output is correct
88 Correct 6 ms 11988 KB Output is correct
89 Correct 468 ms 128856 KB Output is correct
90 Correct 369 ms 106224 KB Output is correct
91 Correct 306 ms 87884 KB Output is correct