답안 #1075629

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1075629 2024-08-26T08:08:13 Z Faisal_Saqib 메기 농장 (IOI22_fish) C++17
70 / 100
260 ms 386544 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;
vll pre[N];
ll sz[N];
vll 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]));
        sz[i]=possible[i].size();
        pre[i].resize(sz[i]+1,0);
        ll sm=0;
        int pp=0;
        int rp=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][rp]=sm;
            rp++;
        }
        dp[i][0].resize(sz[i]+1,0);
        dp[i][1].resize(sz[i]+1,0);
    }
    // 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(int i=1;i<n;i++)
    {
        // ll sum=0;
        ll mx1=-2e18;
        ll mx2=-2e18;
        ll mx11=0,mx22=0;
        for(int k=0;k<sz[i];k++)
        {
            int hp=possible[i][k];
            mx11=max(mx11,dp[i-1][0][k]);
            auto index=lower_bound(all(possible[i-1]),hp)-begin(possible[i-1]);
            mx1=max(mx1,dp[i-1][0][k]-pre[i-1][index]);
        }
        for(int k=0;k<sz[i];k++)
        {
            int hp=possible[i][k];
            mx11=max(mx11,dp[i-1][1][k]);

            auto index=lower_bound(all(possible[i]),hp)-begin(possible[i]);
            mx1=max(mx1,dp[i-1][1][k]+pre[i][index]);
        }
        int pp=0;
        for(auto&h:possible[i])
        {
            auto index=lower_bound(all(possible[i-1]),h)-begin(possible[i-1]);
            dp[i][0][pp]=max(dp[i][0][pp],mx1+pre[i-1][index]);
            dp[i][1][pp]=max(dp[i][1][pp],mx1+pre[i-1][index]);
            dp[i][1][pp]=max(dp[i][1][pp],mx2-pre[i][pp]);

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

            dp[i][1][pp]=max(dp[i][1][pp],mx11);
            dp[i][1][pp]=max(dp[i][1][pp],mx22);
            pp++;
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<sz[i];j++)
        {
            ans=max(ans,dp[i][0][j]);
            ans=max(ans,dp[i][1][j]);
        }
    }
    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:36: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]
   36 |             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:217:16: warning: unused variable 'ans' [-Wunused-variable]
  217 |     ll n=N,m=M,ans=0;
      |                ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 21080 KB Output is correct
2 Correct 25 ms 22488 KB Output is correct
3 Correct 7 ms 15964 KB Output is correct
4 Correct 7 ms 15964 KB Output is correct
5 Correct 70 ms 42604 KB Output is correct
6 Correct 72 ms 43120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14428 KB Output is correct
2 Correct 41 ms 26196 KB Output is correct
3 Correct 75 ms 29008 KB Output is correct
4 Correct 24 ms 21084 KB Output is correct
5 Correct 29 ms 22484 KB Output is correct
6 Correct 7 ms 14428 KB Output is correct
7 Correct 6 ms 14428 KB Output is correct
8 Correct 6 ms 14424 KB Output is correct
9 Correct 7 ms 14428 KB Output is correct
10 Correct 7 ms 15964 KB Output is correct
11 Correct 7 ms 15940 KB Output is correct
12 Correct 23 ms 21000 KB Output is correct
13 Correct 30 ms 22492 KB Output is correct
14 Correct 25 ms 21072 KB Output is correct
15 Correct 26 ms 21844 KB Output is correct
16 Correct 31 ms 21120 KB Output is correct
17 Correct 29 ms 21776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 15960 KB Output is correct
2 Correct 10 ms 22364 KB Output is correct
3 Correct 23 ms 24900 KB Output is correct
4 Correct 20 ms 24536 KB Output is correct
5 Correct 32 ms 28500 KB Output is correct
6 Correct 27 ms 28024 KB Output is correct
7 Correct 30 ms 28380 KB Output is correct
8 Correct 34 ms 28508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14424 KB Output is correct
2 Correct 7 ms 14656 KB Output is correct
3 Correct 6 ms 14428 KB Output is correct
4 Correct 6 ms 14544 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 6 ms 14548 KB Output is correct
7 Correct 7 ms 14428 KB Output is correct
8 Correct 8 ms 14428 KB Output is correct
9 Correct 8 ms 17144 KB Output is correct
10 Correct 12 ms 21864 KB Output is correct
11 Correct 8 ms 17316 KB Output is correct
12 Correct 11 ms 21816 KB Output is correct
13 Correct 7 ms 15708 KB Output is correct
14 Correct 10 ms 21596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14424 KB Output is correct
2 Correct 7 ms 14656 KB Output is correct
3 Correct 6 ms 14428 KB Output is correct
4 Correct 6 ms 14544 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 6 ms 14548 KB Output is correct
7 Correct 7 ms 14428 KB Output is correct
8 Correct 8 ms 14428 KB Output is correct
9 Correct 8 ms 17144 KB Output is correct
10 Correct 12 ms 21864 KB Output is correct
11 Correct 8 ms 17316 KB Output is correct
12 Correct 11 ms 21816 KB Output is correct
13 Correct 7 ms 15708 KB Output is correct
14 Correct 10 ms 21596 KB Output is correct
15 Correct 10 ms 21596 KB Output is correct
16 Correct 8 ms 15616 KB Output is correct
17 Correct 20 ms 24680 KB Output is correct
18 Correct 20 ms 24360 KB Output is correct
19 Correct 21 ms 24400 KB Output is correct
20 Correct 18 ms 24412 KB Output is correct
21 Correct 20 ms 24524 KB Output is correct
22 Correct 27 ms 27484 KB Output is correct
23 Correct 12 ms 22108 KB Output is correct
24 Correct 17 ms 23644 KB Output is correct
25 Correct 11 ms 21592 KB Output is correct
26 Correct 12 ms 22108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14424 KB Output is correct
2 Correct 7 ms 14656 KB Output is correct
3 Correct 6 ms 14428 KB Output is correct
4 Correct 6 ms 14544 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 6 ms 14548 KB Output is correct
7 Correct 7 ms 14428 KB Output is correct
8 Correct 8 ms 14428 KB Output is correct
9 Correct 8 ms 17144 KB Output is correct
10 Correct 12 ms 21864 KB Output is correct
11 Correct 8 ms 17316 KB Output is correct
12 Correct 11 ms 21816 KB Output is correct
13 Correct 7 ms 15708 KB Output is correct
14 Correct 10 ms 21596 KB Output is correct
15 Correct 10 ms 21596 KB Output is correct
16 Correct 8 ms 15616 KB Output is correct
17 Correct 20 ms 24680 KB Output is correct
18 Correct 20 ms 24360 KB Output is correct
19 Correct 21 ms 24400 KB Output is correct
20 Correct 18 ms 24412 KB Output is correct
21 Correct 20 ms 24524 KB Output is correct
22 Correct 27 ms 27484 KB Output is correct
23 Correct 12 ms 22108 KB Output is correct
24 Correct 17 ms 23644 KB Output is correct
25 Correct 11 ms 21592 KB Output is correct
26 Correct 12 ms 22108 KB Output is correct
27 Correct 177 ms 366928 KB Output is correct
28 Correct 75 ms 59416 KB Output is correct
29 Correct 240 ms 386436 KB Output is correct
30 Correct 257 ms 386384 KB Output is correct
31 Correct 260 ms 386544 KB Output is correct
32 Correct 76 ms 52048 KB Output is correct
33 Correct 237 ms 386388 KB Output is correct
34 Correct 252 ms 386416 KB Output is correct
35 Correct 201 ms 374352 KB Output is correct
36 Correct 251 ms 386420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 15960 KB Output is correct
2 Correct 10 ms 22364 KB Output is correct
3 Correct 23 ms 24900 KB Output is correct
4 Correct 20 ms 24536 KB Output is correct
5 Correct 32 ms 28500 KB Output is correct
6 Correct 27 ms 28024 KB Output is correct
7 Correct 30 ms 28380 KB Output is correct
8 Correct 34 ms 28508 KB Output is correct
9 Incorrect 98 ms 52312 KB 1st lines differ - on the 1st token, expected: '99999', found: '199997'
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 21080 KB Output is correct
2 Correct 25 ms 22488 KB Output is correct
3 Correct 7 ms 15964 KB Output is correct
4 Correct 7 ms 15964 KB Output is correct
5 Correct 70 ms 42604 KB Output is correct
6 Correct 72 ms 43120 KB Output is correct
7 Correct 6 ms 14428 KB Output is correct
8 Correct 41 ms 26196 KB Output is correct
9 Correct 75 ms 29008 KB Output is correct
10 Correct 24 ms 21084 KB Output is correct
11 Correct 29 ms 22484 KB Output is correct
12 Correct 7 ms 14428 KB Output is correct
13 Correct 6 ms 14428 KB Output is correct
14 Correct 6 ms 14424 KB Output is correct
15 Correct 7 ms 14428 KB Output is correct
16 Correct 7 ms 15964 KB Output is correct
17 Correct 7 ms 15940 KB Output is correct
18 Correct 23 ms 21000 KB Output is correct
19 Correct 30 ms 22492 KB Output is correct
20 Correct 25 ms 21072 KB Output is correct
21 Correct 26 ms 21844 KB Output is correct
22 Correct 31 ms 21120 KB Output is correct
23 Correct 29 ms 21776 KB Output is correct
24 Correct 7 ms 15960 KB Output is correct
25 Correct 10 ms 22364 KB Output is correct
26 Correct 23 ms 24900 KB Output is correct
27 Correct 20 ms 24536 KB Output is correct
28 Correct 32 ms 28500 KB Output is correct
29 Correct 27 ms 28024 KB Output is correct
30 Correct 30 ms 28380 KB Output is correct
31 Correct 34 ms 28508 KB Output is correct
32 Correct 6 ms 14424 KB Output is correct
33 Correct 7 ms 14656 KB Output is correct
34 Correct 6 ms 14428 KB Output is correct
35 Correct 6 ms 14544 KB Output is correct
36 Correct 6 ms 14428 KB Output is correct
37 Correct 6 ms 14548 KB Output is correct
38 Correct 7 ms 14428 KB Output is correct
39 Correct 8 ms 14428 KB Output is correct
40 Correct 8 ms 17144 KB Output is correct
41 Correct 12 ms 21864 KB Output is correct
42 Correct 8 ms 17316 KB Output is correct
43 Correct 11 ms 21816 KB Output is correct
44 Correct 7 ms 15708 KB Output is correct
45 Correct 10 ms 21596 KB Output is correct
46 Correct 10 ms 21596 KB Output is correct
47 Correct 8 ms 15616 KB Output is correct
48 Correct 20 ms 24680 KB Output is correct
49 Correct 20 ms 24360 KB Output is correct
50 Correct 21 ms 24400 KB Output is correct
51 Correct 18 ms 24412 KB Output is correct
52 Correct 20 ms 24524 KB Output is correct
53 Correct 27 ms 27484 KB Output is correct
54 Correct 12 ms 22108 KB Output is correct
55 Correct 17 ms 23644 KB Output is correct
56 Correct 11 ms 21592 KB Output is correct
57 Correct 12 ms 22108 KB Output is correct
58 Correct 177 ms 366928 KB Output is correct
59 Correct 75 ms 59416 KB Output is correct
60 Correct 240 ms 386436 KB Output is correct
61 Correct 257 ms 386384 KB Output is correct
62 Correct 260 ms 386544 KB Output is correct
63 Correct 76 ms 52048 KB Output is correct
64 Correct 237 ms 386388 KB Output is correct
65 Correct 252 ms 386416 KB Output is correct
66 Correct 201 ms 374352 KB Output is correct
67 Correct 251 ms 386420 KB Output is correct
68 Incorrect 98 ms 52312 KB 1st lines differ - on the 1st token, expected: '99999', found: '199997'
69 Halted 0 ms 0 KB -