답안 #961413

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
961413 2024-04-12T04:39:59 Z noobcodur Group Photo (JOI21_ho_t3) C++14
100 / 100
377 ms 924 KB
#include<bits/stdc++.h>
using namespace std;

using ld = long double;

#define int long long
#define pii pair<int,int>
#define forn(i,j) for(int i = 0; i < j; ++i)
#define forrange(i,j,k) for(int i = j; i < k; ++i)
#define vi vector<int>
#define vpii vector<pii>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()

const int MOD = 1e9+7; const int INF = 1e17+1; const int maxN = 5e3+1;

void setIO(string name){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	if(!name.empty()){
		freopen((name + ".in").c_str(),"r",stdin);
		freopen((name + ".out").c_str(),"w",stdout);
	}
}

int dp[maxN],a[maxN],pos[maxN],small[maxN];

int bit1[maxN],bit2[maxN];

void update1(int idx){
	while(idx < maxN){
		bit1[idx]++; idx += idx&-idx;
	}
}

int query1(int idx){
	int res = 0;
	while(idx){
		res += bit1[idx]; idx -= idx&-idx;
	}

	return res;
}

void update2(int idx){
	while(idx < maxN){
		bit2[idx]++; idx += idx&-idx;
	}
}

int query2(int idx){
	int res = 0;
	while(idx){
		res += bit2[idx]; idx -= idx&-idx;
	}

	return res;
}

void init(int n){
	forrange(i,1,n+1){
		bit2[i] = 0;
	}
}

signed main(){
	setIO("");
	int n; cin >> n;

	forrange(i,1,n+1){
		cin >> a[i]; pos[a[i]] = i;
	}

	dp[0] = 0;

	forrange(i,1,n+1){
		dp[i] = INF;
		update1(pos[i]);
		small[i] = query1(n)-query1(pos[i]);
		int cost = 0;
		for(int j = i; j; --j){
			cost += small[j]; //number of elements k < j having pos[k] > pos[j]

			cost += query2(n)-query2(pos[j]); //number of elements k > j, having pos[k] > pos[j]
			cost -= query2(pos[j]-1); //number of elements k > j, having pos[k] < pos[j]

			dp[i] = min(dp[i],dp[j-1]+cost);
			update2(pos[j]);
		}

		init(n);
	}

	cout << dp[n] << endl;
}

Compilation message

Main.cpp: In function 'void setIO(std::string)':
Main.cpp:24:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |   freopen((name + ".in").c_str(),"r",stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:25:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |   freopen((name + ".out").c_str(),"w",stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 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 1 ms 348 KB Output is correct
6 Correct 0 ms 460 KB Output is correct
7 Correct 1 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 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 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 1 ms 348 KB Output is correct
6 Correct 0 ms 460 KB Output is correct
7 Correct 1 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 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 468 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 464 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 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 1 ms 348 KB Output is correct
6 Correct 0 ms 460 KB Output is correct
7 Correct 1 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 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 468 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 464 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 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 1 ms 348 KB Output is correct
6 Correct 0 ms 460 KB Output is correct
7 Correct 1 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 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 468 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 464 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 7 ms 344 KB Output is correct
32 Correct 6 ms 348 KB Output is correct
33 Correct 6 ms 348 KB Output is correct
34 Correct 6 ms 348 KB Output is correct
35 Correct 6 ms 348 KB Output is correct
36 Correct 6 ms 348 KB Output is correct
37 Correct 6 ms 552 KB Output is correct
38 Correct 6 ms 344 KB Output is correct
39 Correct 7 ms 456 KB Output is correct
40 Correct 6 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 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 1 ms 348 KB Output is correct
6 Correct 0 ms 460 KB Output is correct
7 Correct 1 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 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 468 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 464 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 7 ms 344 KB Output is correct
32 Correct 6 ms 348 KB Output is correct
33 Correct 6 ms 348 KB Output is correct
34 Correct 6 ms 348 KB Output is correct
35 Correct 6 ms 348 KB Output is correct
36 Correct 6 ms 348 KB Output is correct
37 Correct 6 ms 552 KB Output is correct
38 Correct 6 ms 344 KB Output is correct
39 Correct 7 ms 456 KB Output is correct
40 Correct 6 ms 348 KB Output is correct
41 Correct 355 ms 668 KB Output is correct
42 Correct 377 ms 924 KB Output is correct
43 Correct 351 ms 692 KB Output is correct
44 Correct 358 ms 924 KB Output is correct
45 Correct 353 ms 664 KB Output is correct
46 Correct 354 ms 876 KB Output is correct
47 Correct 338 ms 600 KB Output is correct
48 Correct 347 ms 848 KB Output is correct
49 Correct 339 ms 664 KB Output is correct
50 Correct 349 ms 912 KB Output is correct
51 Correct 345 ms 692 KB Output is correct
52 Correct 353 ms 900 KB Output is correct
53 Correct 342 ms 692 KB Output is correct
54 Correct 347 ms 848 KB Output is correct
55 Correct 335 ms 660 KB Output is correct