Submission #371266

# Submission time Handle Problem Language Result Execution time Memory
371266 2021-02-26T10:12:54 Z arnold518 Group Photo (JOI21_ho_t3) C++14
100 / 100
525 ms 640 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 5000;
const ll INF = 1e18;

int N, A[MAXN+10];
ll dp[MAXN+10];

int B[MAXN+10], C[MAXN+10], D[MAXN+10];

struct BIT
{
	ll tree[MAXN+10];
	void init()
	{
		memset(tree, 0, sizeof(tree));
	}
	void update(int i, ll k)
	{
		for(; i<=N; i+=(i&-i)) tree[i]+=k;
	}
	ll query(int i)
	{
		ll ret=0;
		for(; i>0; i-=(i&-i)) ret+=tree[i];
		return ret;
	}
}bit;

int main()
{
	scanf("%d", &N);
	for(int i=1; i<=N; i++) scanf("%d", &A[i]);

	for(int i=1; i<=N; i++) dp[i]=INF;
	for(int i=0; i<N; i++)
	{
		for(int j=1, k=i+1; j<=N; j++) if(A[j]>i) B[k++]=A[j];
		for(int j=1; j<=N; j++) C[B[j]]=j, D[j]=0;

		//for(int j=1; j<=N; j++) printf("%d ", B[j]); printf("\n");

		bit.init();
		for(int j=i+1; j<=N; j++)
		{
			D[j]=D[j-1]+bit.query(C[j]);
			bit.update(C[j], 1);
		}
		ll val=0;
		for(int j=N; j>i; j--)
		{
			val+=j;
			val-=C[j];
			D[j-1]+=val;
		}
		for(int j=i+1; j<=N; j++)
		{
			dp[j]=min(dp[j], dp[i]+D[j]);
		}
	}
	printf("%lld\n", dp[N]);
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:37:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   37 |  scanf("%d", &N);
      |  ~~~~~^~~~~~~~~~
Main.cpp:38:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   38 |  for(int i=1; i<=N; i++) scanf("%d", &A[i]);
      |                          ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 2 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 2 ms 364 KB Output is correct
26 Correct 2 ms 364 KB Output is correct
27 Correct 2 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 2 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 2 ms 364 KB Output is correct
26 Correct 2 ms 364 KB Output is correct
27 Correct 2 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 13 ms 364 KB Output is correct
32 Correct 13 ms 364 KB Output is correct
33 Correct 14 ms 364 KB Output is correct
34 Correct 13 ms 364 KB Output is correct
35 Correct 13 ms 364 KB Output is correct
36 Correct 13 ms 364 KB Output is correct
37 Correct 13 ms 364 KB Output is correct
38 Correct 13 ms 364 KB Output is correct
39 Correct 13 ms 364 KB Output is correct
40 Correct 13 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 2 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 2 ms 364 KB Output is correct
26 Correct 2 ms 364 KB Output is correct
27 Correct 2 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 13 ms 364 KB Output is correct
32 Correct 13 ms 364 KB Output is correct
33 Correct 14 ms 364 KB Output is correct
34 Correct 13 ms 364 KB Output is correct
35 Correct 13 ms 364 KB Output is correct
36 Correct 13 ms 364 KB Output is correct
37 Correct 13 ms 364 KB Output is correct
38 Correct 13 ms 364 KB Output is correct
39 Correct 13 ms 364 KB Output is correct
40 Correct 13 ms 364 KB Output is correct
41 Correct 525 ms 524 KB Output is correct
42 Correct 513 ms 640 KB Output is correct
43 Correct 516 ms 620 KB Output is correct
44 Correct 514 ms 620 KB Output is correct
45 Correct 516 ms 620 KB Output is correct
46 Correct 513 ms 492 KB Output is correct
47 Correct 511 ms 492 KB Output is correct
48 Correct 510 ms 492 KB Output is correct
49 Correct 513 ms 492 KB Output is correct
50 Correct 513 ms 620 KB Output is correct
51 Correct 512 ms 620 KB Output is correct
52 Correct 518 ms 492 KB Output is correct
53 Correct 523 ms 528 KB Output is correct
54 Correct 513 ms 492 KB Output is correct
55 Correct 511 ms 620 KB Output is correct