Submission #1075597

# Submission time Handle Problem Language Result Execution time Memory
1075597 2024-08-26T07:56:30 Z Faisal_Saqib Catfish Farm (IOI22_fish) C++17
84 / 100
1000 ms 390396 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vll vector<ll>
#define all(x) begin(x),end(x)
const int N=1e5+10;
map<ll,ll> pre[N];
map<ll,ll> dp[N][3];
vll possible[N];
vector<pair<ll,ll>> values[N];
ll solve(ll&n,ll&m,vll&x,vll&y,vll&w)
{
    ll ans=0;
    for(int j=0;j<m;j++)
    {
        values[x[j]].push_back({y[j]+1,w[j]});
        possible[x[j]].push_back(y[j]+1);
        possible[x[j]+1].push_back(y[j]+1);
        if(x[j]>0)
            possible[x[j]-1].push_back(y[j]+1);
    }
    for(int i=0;i<=n;i++)
    {
        possible[i].push_back(0);
        possible[i].push_back(n);
        sort(begin(possible[i]),end(possible[i]));
        sort(begin(values[i]),end(values[i]));
        ll sm=0;
        int pp=0;
        for(auto&j:possible[i])
        {
            while(pp<values[i].size() and values[i][pp].first<=j)
            {
                sm+=values[i][pp].second;
                pp++;
            }
            pre[i][j]=sm;
        }
    }
    // for(int i=0;i<n;i++)
    //     for(int j=0;j<=n;j++)
    //         for(int k=0;k<2;k++)
    //             dp[i][j][k]=0;
    for(auto&h:possible[0])
    {
        dp[0][0][h]=0;
        dp[0][1][h]=0;
    }
    for(int i=1;i<n;i++)
    {
        // ll sum=0;
        ll mx1=-2e18;
        ll mx2=-2e18;
        ll mx11=0,mx22=0;
        for(auto&ip:dp[i-1][0])
        {
            int hp=ip.first;
            mx11=max(mx11,ip.second);

            mx1=max(mx1,ip.second-pre[i-1][hp]);
        }
        for(auto&ip:dp[i-1][1])
        {
            int hp=ip.first;
            mx22=max(mx22,ip.second);
            mx2=max(mx2,ip.second+pre[i][hp]);
        }
        for(auto&h:possible[i])
        {
            dp[i][0][h]=max(dp[i][0][h],mx1+pre[i-1][h]);
            dp[i][1][h]=max(dp[i][1][h],mx1+pre[i-1][h]);
            dp[i][1][h]=max(dp[i][1][h],mx2-pre[i][h]);

            dp[i][0][h]=max(dp[i][0][h],mx11);
            dp[i][0][h]=max(dp[i][0][h],mx22);

            dp[i][1][h]=max(dp[i][1][h],mx11);
            dp[i][1][h]=max(dp[i][1][h],mx22);
            // sum+=val[i-1][h];
            // ll aux=0;
            // for(int nh=h+1;nh<=n;nh++)
            // {
            //     // aux+=val[i][nh];
            //     dp[i][h][0]=max(dp[i][h][0],dp[i-1][nh][0]);
            //     dp[i][h][0]=max(dp[i][h][0],dp[i-1][nh][1]);
                
            //     dp[i][h][1]=max(dp[i][h][1],dp[i-1][nh][0]);
            //     dp[i][h][1]=max(dp[i][h][1],dp[i-1][nh][1]+pre[i][nh]-pre[i][h]);

            // }
            // // ll og=sum;
            // for(int nh=0;nh<=h;nh++)
            // {
            //     // sum-=val[i-1][nh];
            //     dp[i][h][0]=max(dp[i][h][0],dp[i-1][nh][0]+pre[i-1][h]-pre[i-1][nh]);
            //     dp[i][h][0]=max(dp[i][h][0],dp[i-1][nh][1]);
                
            //     dp[i][h][1]=max(dp[i][h][1],dp[i-1][nh][0]+pre[i-1][j]-pre[i-1][nh]);
            //     dp[i][h][1]=max(dp[i][h][1],dp[i-1][nh][1]);
            // }
            // sum=og;
        }
    }
    for(int i=0;i<n;i++)
    {
        for(auto&ip:dp[i][0])
        {
            // cout<<"AT "<<ip.first<<' '<<i<<' '<<ip.second<<endl;
            ans=max(ans,ip.second);
        }
        for(auto&ip:dp[i][1])
        {
            // cout<<"AT1 "<<ip.first<<' '<<i<<' '<<ip.second<<endl;
            ans=max(ans,ip.second);
        }
    }
    return ans;
}
ll subtask2(ll&n,ll&m,vll&x,vll&y,vll&w)
{
    ll ans=0;
    ll val[2][n]={0};
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<n;j++)
        {
            val[i][j]=0;
        }
    }
    ll sum1=0,sum0=0;
    for(int j=0;j<m;j++)
    {
        val[x[j]][y[j]]+=w[j];
        if(x[j]==1)
        {
            sum1+=w[j];
        }
        else
        {
            sum0+=w[j];
        }
    }
    ans=max(sum0,sum1);
    ll taken=0;
    ll cur=0;
    for(int j=0;j<n;j++)
    {
        cur+=val[0][j];
        taken+=val[1][j];
        ans=max(ans,cur);
        if(n>2)
            ans=max(ans,cur+sum1-taken);
    }
    return ans;
}
ll subtask3(ll&n,ll&m,vll&x,vll&y,vll&w)
{
    ll ans=0;
    ll val[n+1];
    for(int i=0;i<n;i++)val[i]=0;
    for(int j=0;j<m;j++)val[x[j]]+=w[j];
    ll dp[n+1][3][3];
    for(int i=0;i<n;i++)
        for(int j=0;j<2;j++)
            for(int k=0;k<2;k++)
                dp[i][j][k]=0;
    for(int i=1;i<n;i++)
    {
        // wall not at i
        dp[i][0][0]=max(max(dp[i-1][0][0],dp[i-1][0][1]),dp[i-1][1][0]);
        dp[i][0][1]=max(max(dp[i-1][0][0],dp[i-1][0][1]),dp[i-1][1][0]+val[i]);

        dp[i][1][0]=max(max(dp[i-1][0][1],dp[i-1][0][0]+val[i-1]),dp[i-1][1][0]);
    }
    for(int i=0;i<n;i++)
    {
        ans=max({ans,dp[i][0][0],dp[i][0][1],dp[i][1][0],dp[i][1][1]});
    }
    return ans;
}
const int N2=3e3+1;
ll val1[N2][N2],pre1[N2][N2];
ll dp1[N2][N2][3];
ll solver(ll&n,ll&m,vll&x,vll&y,vll&w)
{
    ll ans=0;
    for(int i=0;i<n;i++)for(int j=0;j<=n;j++)val1[i][j]=pre1[i][j]=0;
    for(int j=0;j<m;j++)val1[x[j]][y[j]+1]+=w[j];
    for(int i=0;i<=n;i++)
    {
        pre1[i][0]=val1[i][0];
        for(int j=1;j<=n;j++)
            pre1[i][j]=pre1[i][j-1]+val1[i][j];
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<=n;j++)
            for(int k=0;k<2;k++)
                dp1[i][j][k]=0;
    for(int i=1;i<n;i++)
    {
        // ll sum=0;
        ll mx1=-2e18;
        ll mx2=-2e18;
        ll mx11=0,mx22=0;
        for(int hp=0;hp<=n;hp++)
        {
            mx11=max(mx11,dp1[i-1][hp][0]);
            mx22=max(mx22,dp1[i-1][hp][1]);
 
            mx1=max(mx1,dp1[i-1][hp][0]-pre1[i-1][hp]);
            mx2=max(mx2,dp1[i-1][hp][1]+pre1[i][hp]);
        }
        for(int h=0;h<=n;h++)
        {
            dp1[i][h][0]=max(dp1[i][h][0],mx1+pre1[i-1][h]);
            dp1[i][h][1]=max(dp1[i][h][1],mx1+pre1[i-1][h]);
            dp1[i][h][1]=max(dp1[i][h][1],mx2-pre1[i][h]);
 
            dp1[i][h][0]=max(dp1[i][h][0],mx11);
            dp1[i][h][0]=max(dp1[i][h][0],mx22);
 
            dp1[i][h][1]=max(dp1[i][h][1],mx11);
            dp1[i][h][1]=max(dp1[i][h][1],mx22);
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int h=0;h<=n;h++)
        {
            ans=max(ans,dp1[i][h][0]);
            ans=max(ans,dp1[i][h][1]);
        }
    }
    return ans;
}
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,std::vector<int> W)
{
    ll n=N,m=M,ans=0;
    vector<ll> x(all(X)),y(all(Y)),w(all(W));
    ll mx_x=*max_element(all(x));
    ll mx_y=*max_element(all(y));
    if(mx_x<=1)
        return subtask2(n,m,x,y,w);
    set<ll> posmod;
    for(auto i:x)
    {
        posmod.insert(i%2);
    }
    if(posmod.size()==1 or mx_y==0)
        return subtask3(n,m,x,y,w);
    if(n<=3000)
    {
        return solver(n,m,x,y,w);
    }
    return solve(n,m,x,y,w);
}

Compilation message

fish.cpp: In function 'long long int solve(long long int&, long long int&, std::vector<long long int>&, std::vector<long long int>&, std::vector<long long int>&)':
fish.cpp:32:21: 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]
   32 |             while(pp<values[i].size() and values[i][pp].first<=j)
      |                   ~~^~~~~~~~~~~~~~~~~
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:238:16: warning: unused variable 'ans' [-Wunused-variable]
  238 |     ll n=N,m=M,ans=0;
      |                ^~~
# Verdict Execution time Memory Grader output
1 Correct 28 ms 29016 KB Output is correct
2 Correct 35 ms 30032 KB Output is correct
3 Correct 11 ms 25436 KB Output is correct
4 Correct 12 ms 25436 KB Output is correct
5 Correct 78 ms 45652 KB Output is correct
6 Correct 77 ms 45652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23900 KB Output is correct
2 Correct 51 ms 32748 KB Output is correct
3 Correct 61 ms 34640 KB Output is correct
4 Correct 34 ms 29008 KB Output is correct
5 Correct 33 ms 30036 KB Output is correct
6 Correct 10 ms 23900 KB Output is correct
7 Correct 10 ms 23900 KB Output is correct
8 Correct 10 ms 23824 KB Output is correct
9 Correct 9 ms 23900 KB Output is correct
10 Correct 9 ms 25436 KB Output is correct
11 Correct 11 ms 25432 KB Output is correct
12 Correct 26 ms 29016 KB Output is correct
13 Correct 32 ms 29976 KB Output is correct
14 Correct 26 ms 29056 KB Output is correct
15 Correct 28 ms 29532 KB Output is correct
16 Correct 47 ms 29012 KB Output is correct
17 Correct 43 ms 29524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 25432 KB Output is correct
2 Correct 14 ms 31580 KB Output is correct
3 Correct 28 ms 33276 KB Output is correct
4 Correct 31 ms 33360 KB Output is correct
5 Correct 43 ms 36436 KB Output is correct
6 Correct 36 ms 36372 KB Output is correct
7 Correct 36 ms 36440 KB Output is correct
8 Correct 40 ms 36444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23900 KB Output is correct
2 Correct 15 ms 23968 KB Output is correct
3 Correct 11 ms 23820 KB Output is correct
4 Correct 10 ms 23900 KB Output is correct
5 Correct 15 ms 23900 KB Output is correct
6 Correct 10 ms 23900 KB Output is correct
7 Correct 10 ms 23860 KB Output is correct
8 Correct 10 ms 23900 KB Output is correct
9 Correct 12 ms 26460 KB Output is correct
10 Correct 14 ms 31068 KB Output is correct
11 Correct 16 ms 26596 KB Output is correct
12 Correct 15 ms 31068 KB Output is correct
13 Correct 11 ms 25036 KB Output is correct
14 Correct 14 ms 31068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23900 KB Output is correct
2 Correct 15 ms 23968 KB Output is correct
3 Correct 11 ms 23820 KB Output is correct
4 Correct 10 ms 23900 KB Output is correct
5 Correct 15 ms 23900 KB Output is correct
6 Correct 10 ms 23900 KB Output is correct
7 Correct 10 ms 23860 KB Output is correct
8 Correct 10 ms 23900 KB Output is correct
9 Correct 12 ms 26460 KB Output is correct
10 Correct 14 ms 31068 KB Output is correct
11 Correct 16 ms 26596 KB Output is correct
12 Correct 15 ms 31068 KB Output is correct
13 Correct 11 ms 25036 KB Output is correct
14 Correct 14 ms 31068 KB Output is correct
15 Correct 15 ms 31064 KB Output is correct
16 Correct 12 ms 25180 KB Output is correct
17 Correct 24 ms 33108 KB Output is correct
18 Correct 24 ms 33104 KB Output is correct
19 Correct 23 ms 33116 KB Output is correct
20 Correct 33 ms 33112 KB Output is correct
21 Correct 28 ms 33104 KB Output is correct
22 Correct 30 ms 35212 KB Output is correct
23 Correct 16 ms 31556 KB Output is correct
24 Correct 28 ms 32340 KB Output is correct
25 Correct 14 ms 31064 KB Output is correct
26 Correct 15 ms 31320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23900 KB Output is correct
2 Correct 15 ms 23968 KB Output is correct
3 Correct 11 ms 23820 KB Output is correct
4 Correct 10 ms 23900 KB Output is correct
5 Correct 15 ms 23900 KB Output is correct
6 Correct 10 ms 23900 KB Output is correct
7 Correct 10 ms 23860 KB Output is correct
8 Correct 10 ms 23900 KB Output is correct
9 Correct 12 ms 26460 KB Output is correct
10 Correct 14 ms 31068 KB Output is correct
11 Correct 16 ms 26596 KB Output is correct
12 Correct 15 ms 31068 KB Output is correct
13 Correct 11 ms 25036 KB Output is correct
14 Correct 14 ms 31068 KB Output is correct
15 Correct 15 ms 31064 KB Output is correct
16 Correct 12 ms 25180 KB Output is correct
17 Correct 24 ms 33108 KB Output is correct
18 Correct 24 ms 33104 KB Output is correct
19 Correct 23 ms 33116 KB Output is correct
20 Correct 33 ms 33112 KB Output is correct
21 Correct 28 ms 33104 KB Output is correct
22 Correct 30 ms 35212 KB Output is correct
23 Correct 16 ms 31556 KB Output is correct
24 Correct 28 ms 32340 KB Output is correct
25 Correct 14 ms 31064 KB Output is correct
26 Correct 15 ms 31320 KB Output is correct
27 Correct 207 ms 376192 KB Output is correct
28 Correct 79 ms 65104 KB Output is correct
29 Correct 258 ms 390204 KB Output is correct
30 Correct 315 ms 390396 KB Output is correct
31 Correct 236 ms 390224 KB Output is correct
32 Correct 91 ms 56148 KB Output is correct
33 Correct 273 ms 390168 KB Output is correct
34 Correct 310 ms 390356 KB Output is correct
35 Correct 214 ms 381744 KB Output is correct
36 Correct 279 ms 390224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 25432 KB Output is correct
2 Correct 14 ms 31580 KB Output is correct
3 Correct 28 ms 33276 KB Output is correct
4 Correct 31 ms 33360 KB Output is correct
5 Correct 43 ms 36436 KB Output is correct
6 Correct 36 ms 36372 KB Output is correct
7 Correct 36 ms 36440 KB Output is correct
8 Correct 40 ms 36444 KB Output is correct
9 Correct 257 ms 147280 KB Output is correct
10 Correct 125 ms 72636 KB Output is correct
11 Correct 242 ms 121428 KB Output is correct
12 Correct 10 ms 23896 KB Output is correct
13 Correct 8 ms 23900 KB Output is correct
14 Correct 9 ms 23900 KB Output is correct
15 Correct 12 ms 23900 KB Output is correct
16 Correct 13 ms 23856 KB Output is correct
17 Correct 9 ms 23972 KB Output is correct
18 Correct 10 ms 25432 KB Output is correct
19 Correct 12 ms 25440 KB Output is correct
20 Correct 20 ms 31708 KB Output is correct
21 Correct 16 ms 31580 KB Output is correct
22 Correct 292 ms 144832 KB Output is correct
23 Correct 327 ms 163412 KB Output is correct
24 Correct 387 ms 171500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 29016 KB Output is correct
2 Correct 35 ms 30032 KB Output is correct
3 Correct 11 ms 25436 KB Output is correct
4 Correct 12 ms 25436 KB Output is correct
5 Correct 78 ms 45652 KB Output is correct
6 Correct 77 ms 45652 KB Output is correct
7 Correct 12 ms 23900 KB Output is correct
8 Correct 51 ms 32748 KB Output is correct
9 Correct 61 ms 34640 KB Output is correct
10 Correct 34 ms 29008 KB Output is correct
11 Correct 33 ms 30036 KB Output is correct
12 Correct 10 ms 23900 KB Output is correct
13 Correct 10 ms 23900 KB Output is correct
14 Correct 10 ms 23824 KB Output is correct
15 Correct 9 ms 23900 KB Output is correct
16 Correct 9 ms 25436 KB Output is correct
17 Correct 11 ms 25432 KB Output is correct
18 Correct 26 ms 29016 KB Output is correct
19 Correct 32 ms 29976 KB Output is correct
20 Correct 26 ms 29056 KB Output is correct
21 Correct 28 ms 29532 KB Output is correct
22 Correct 47 ms 29012 KB Output is correct
23 Correct 43 ms 29524 KB Output is correct
24 Correct 11 ms 25432 KB Output is correct
25 Correct 14 ms 31580 KB Output is correct
26 Correct 28 ms 33276 KB Output is correct
27 Correct 31 ms 33360 KB Output is correct
28 Correct 43 ms 36436 KB Output is correct
29 Correct 36 ms 36372 KB Output is correct
30 Correct 36 ms 36440 KB Output is correct
31 Correct 40 ms 36444 KB Output is correct
32 Correct 11 ms 23900 KB Output is correct
33 Correct 15 ms 23968 KB Output is correct
34 Correct 11 ms 23820 KB Output is correct
35 Correct 10 ms 23900 KB Output is correct
36 Correct 15 ms 23900 KB Output is correct
37 Correct 10 ms 23900 KB Output is correct
38 Correct 10 ms 23860 KB Output is correct
39 Correct 10 ms 23900 KB Output is correct
40 Correct 12 ms 26460 KB Output is correct
41 Correct 14 ms 31068 KB Output is correct
42 Correct 16 ms 26596 KB Output is correct
43 Correct 15 ms 31068 KB Output is correct
44 Correct 11 ms 25036 KB Output is correct
45 Correct 14 ms 31068 KB Output is correct
46 Correct 15 ms 31064 KB Output is correct
47 Correct 12 ms 25180 KB Output is correct
48 Correct 24 ms 33108 KB Output is correct
49 Correct 24 ms 33104 KB Output is correct
50 Correct 23 ms 33116 KB Output is correct
51 Correct 33 ms 33112 KB Output is correct
52 Correct 28 ms 33104 KB Output is correct
53 Correct 30 ms 35212 KB Output is correct
54 Correct 16 ms 31556 KB Output is correct
55 Correct 28 ms 32340 KB Output is correct
56 Correct 14 ms 31064 KB Output is correct
57 Correct 15 ms 31320 KB Output is correct
58 Correct 207 ms 376192 KB Output is correct
59 Correct 79 ms 65104 KB Output is correct
60 Correct 258 ms 390204 KB Output is correct
61 Correct 315 ms 390396 KB Output is correct
62 Correct 236 ms 390224 KB Output is correct
63 Correct 91 ms 56148 KB Output is correct
64 Correct 273 ms 390168 KB Output is correct
65 Correct 310 ms 390356 KB Output is correct
66 Correct 214 ms 381744 KB Output is correct
67 Correct 279 ms 390224 KB Output is correct
68 Correct 257 ms 147280 KB Output is correct
69 Correct 125 ms 72636 KB Output is correct
70 Correct 242 ms 121428 KB Output is correct
71 Correct 10 ms 23896 KB Output is correct
72 Correct 8 ms 23900 KB Output is correct
73 Correct 9 ms 23900 KB Output is correct
74 Correct 12 ms 23900 KB Output is correct
75 Correct 13 ms 23856 KB Output is correct
76 Correct 9 ms 23972 KB Output is correct
77 Correct 10 ms 25432 KB Output is correct
78 Correct 12 ms 25440 KB Output is correct
79 Correct 20 ms 31708 KB Output is correct
80 Correct 16 ms 31580 KB Output is correct
81 Correct 292 ms 144832 KB Output is correct
82 Correct 327 ms 163412 KB Output is correct
83 Correct 387 ms 171500 KB Output is correct
84 Execution timed out 1044 ms 201224 KB Time limit exceeded
85 Halted 0 ms 0 KB -