답안 #1031323

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1031323 2024-07-22T17:25:23 Z PieArmy Exam (eJOI20_exam) C++17
100 / 100
106 ms 98752 KB
typedef long long ll;
ll pie(ll army){return (1ll<<army);}
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define endl '\n';
#define mid ((left+right)>>1)
const ll inf=2000000000000000005;
const int sonsuz=2000000005;
using namespace std;
ll fpow(ll x,ll y,ll m=0){if(y<0){cout<<"powError";return -1;}if(m)x%=m;ll res=1;while(y>0){if(y&1)res*=x;x*=x;if(m){x%=m;res%=m;}y>>=1;}return res;}

void code(){
    int n;cin>>n;
    int A[n],B[n];
    for(int i=0;i<n;i++)
        cin>>A[i];
    for(int i=0;i<n;i++)
        cin>>B[i];
    int us[n+1];
    int nex=2,bin=0;
    for(int i=1;i<=n;i++){
        us[i]=bin;
        if(nex==i){
            bin++;nex<<=1;
        }
    }
    int spar[us[n]+1][n];
    for(int i=0;i<n;i++){
        spar[0][i]=A[i];
    }
    for(int i=1;i<=us[n];i++){
        for(int j=0;j<=n-pie(i);j++){
            spar[i][j]=max(spar[i-1][j],spar[i-1][j+pie(i-1)]);
        }
    }
    if(n>5000){
        bool b=true;
        for(int i=1;i<n;i++)
            if(B[i]!=B[i-1]){
                b=false;break;
            }
        if(b){
            bool dp[n+1];for(bool &x:dp)x=false;
            bool cur=false;
            for(int i=0;i<n;i++){
                if(A[i]==B[0])cur=true;
                if(A[i]>B[0])cur=false;
                dp[i]|=cur;
            }
            cur=false;
            for(int i=n-1;i>=0;i--){
                if(A[i]==B[0])cur=true;
                if(A[i]>B[0])cur=false;
                dp[i]|=cur;
            }
            int ans=0;
            for(int i=0;i<n;i++)
                if(dp[i])
                    ans++;
            cout<<ans;
            return;
        }
        else{
            map<int,int>mp;
            for(int i=1;i<=n;i++)
                mp[A[i-1]]=i;
            vector<int>v;
            for(int i=0;i<n;i++){
                int j=mp[B[i]];
                if(j){
                    j--;
                    int boy=abs(i-j)+1;
                    int mx=max(spar[us[boy]][min(i,j)],spar[us[boy]][max(i,j)-pie(us[boy])+1]);
                    if(mx<=B[i])
                        v.pb(j+1);
                }
            }
            vector<int>ans;
            for(int x:v){
                int l=0,r=ans.size();
                while(l<r){
                    int m=(l+r)/2;
                    if(ans[m]<=x)l=m+1;
                    else r=m;
                }
                if(l==ans.size())ans.pb(x);
                else ans[l]=x;
            }
            cout<<ans.size();
            return;
        }
    }
    int dp[n+1][n+1];
    for(auto &x:dp)
        for(auto &y:x)
            y=-1;
    dp[0][0]=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(dp[i][j]==-1)continue;
            if(A[j]==B[i]){
                int boy=abs(i-j)+1;
                int mx=max(spar[us[boy]][min(i,j)],spar[us[boy]][max(i,j)-pie(us[boy])+1]);
                if(mx<=B[i]){
                    dp[i+1][j]=max(dp[i+1][j],dp[i][j]+1);
                    continue;
                }
            }
            dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
            dp[i][j+1]=max(dp[i][j+1],dp[i][j]);
        }
        if(dp[i][n]!=-1)
            dp[i+1][n]=max(dp[i+1][n],dp[i][n]);
    }
    for(int i=0;i<n;i++)
        if(dp[n][i]!=-1)
            dp[n][i+1]=max(dp[n][i+1],dp[n][i]);
    cout<<dp[n][n];
}

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
    int t=1;
    if(!t)cin>>t;
    while(t--){code();cout<<endl;}
    return 0;
}

Compilation message

exam.cpp: In function 'void code()':
exam.cpp:88:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |                 if(l==ans.size())ans.pb(x);
      |                    ~^~~~~~~~~~~~
exam.cpp: In function 'int main()':
exam.cpp:125:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |     bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
      |                            ~~~~~~~^~~~~~~~~~~~~~~~~
exam.cpp:125:60: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |     bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
      |                                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4444 KB Output is correct
2 Correct 4 ms 2140 KB Output is correct
3 Correct 13 ms 8504 KB Output is correct
4 Correct 16 ms 8540 KB Output is correct
5 Correct 18 ms 10332 KB Output is correct
6 Correct 14 ms 8540 KB Output is correct
7 Correct 13 ms 8772 KB Output is correct
8 Correct 19 ms 10068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 1372 KB Output is correct
3 Correct 15 ms 16264 KB Output is correct
4 Correct 81 ms 90964 KB Output is correct
5 Correct 87 ms 98644 KB Output is correct
6 Correct 84 ms 98564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 860 KB Output is correct
2 Correct 22 ms 5872 KB Output is correct
3 Correct 61 ms 13908 KB Output is correct
4 Correct 63 ms 14676 KB Output is correct
5 Correct 55 ms 15548 KB Output is correct
6 Correct 54 ms 14780 KB Output is correct
7 Correct 52 ms 15188 KB Output is correct
8 Correct 58 ms 13908 KB Output is correct
9 Correct 62 ms 14516 KB Output is correct
10 Correct 41 ms 14552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 1372 KB Output is correct
9 Correct 15 ms 16264 KB Output is correct
10 Correct 81 ms 90964 KB Output is correct
11 Correct 87 ms 98644 KB Output is correct
12 Correct 84 ms 98564 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 0 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 4 ms 4188 KB Output is correct
23 Correct 66 ms 98604 KB Output is correct
24 Correct 106 ms 98544 KB Output is correct
25 Correct 97 ms 98752 KB Output is correct
26 Correct 83 ms 98420 KB Output is correct
27 Correct 84 ms 98616 KB Output is correct
28 Correct 82 ms 98644 KB Output is correct
29 Correct 78 ms 98676 KB Output is correct
30 Correct 79 ms 98644 KB Output is correct
31 Correct 93 ms 98644 KB Output is correct
32 Correct 81 ms 98644 KB Output is correct