답안 #803259

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
803259 2023-08-03T02:36:10 Z guagua0407 Group Photo (JOI21_ho_t3) C++17
100 / 100
530 ms 564 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

const int mxn=5005;
int pos[mxn];
int num[mxn];
ll dp[mxn];
int bit[2][mxn];

void update(int id,int pos,int val){
    for(;pos<mxn;pos+=(pos&-pos)){
        bit[id][pos]+=val;
    }
}

int query(int id,int pos){
    int ans=0;
    for(;pos;pos-=(pos&-pos)){
        ans+=bit[id][pos];
    }
    return ans;
}

int main() {_
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>num[i];
        pos[num[i]]=i;
    }
    for(int i=1;i<=n;i++){
        memset(bit[0],0,sizeof(bit[0]));
        memset(bit[1],0,sizeof(bit[1]));
        ll cur=0;
        dp[i]=(ll)1e18;
        for(int j=n;j>i;j--){
            update(0,pos[j],1);
        }
        for(int j=i;j>=1;j--){
            cur+=query(0,pos[j]);
            cur+=query(1,n)-query(1,pos[j]);
            //cout<<j<<' '<<i<<' '<<cur<<'\n';
            dp[i]=min(dp[i],dp[j-1]+cur);
            update(1,pos[j],1);
        }
    }
    cout<<dp[n]<<'\n';
    return 0;
}
//maybe its multiset not set

Compilation message

Main.cpp: In function 'void setIO(std::string)':
Main.cpp:12:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 328 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 328 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 328 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 328 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 328 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 280 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 328 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 328 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 280 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 9 ms 392 KB Output is correct
32 Correct 10 ms 340 KB Output is correct
33 Correct 10 ms 392 KB Output is correct
34 Correct 8 ms 324 KB Output is correct
35 Correct 8 ms 392 KB Output is correct
36 Correct 8 ms 340 KB Output is correct
37 Correct 8 ms 388 KB Output is correct
38 Correct 8 ms 396 KB Output is correct
39 Correct 9 ms 328 KB Output is correct
40 Correct 8 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 328 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 328 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 280 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 9 ms 392 KB Output is correct
32 Correct 10 ms 340 KB Output is correct
33 Correct 10 ms 392 KB Output is correct
34 Correct 8 ms 324 KB Output is correct
35 Correct 8 ms 392 KB Output is correct
36 Correct 8 ms 340 KB Output is correct
37 Correct 8 ms 388 KB Output is correct
38 Correct 8 ms 396 KB Output is correct
39 Correct 9 ms 328 KB Output is correct
40 Correct 8 ms 340 KB Output is correct
41 Correct 530 ms 436 KB Output is correct
42 Correct 521 ms 556 KB Output is correct
43 Correct 519 ms 444 KB Output is correct
44 Correct 525 ms 460 KB Output is correct
45 Correct 529 ms 460 KB Output is correct
46 Correct 521 ms 340 KB Output is correct
47 Correct 509 ms 460 KB Output is correct
48 Correct 506 ms 416 KB Output is correct
49 Correct 511 ms 412 KB Output is correct
50 Correct 509 ms 440 KB Output is correct
51 Correct 517 ms 440 KB Output is correct
52 Correct 505 ms 444 KB Output is correct
53 Correct 508 ms 540 KB Output is correct
54 Correct 510 ms 440 KB Output is correct
55 Correct 504 ms 564 KB Output is correct