답안 #621049

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
621049 2022-08-03T11:30:02 Z dozer Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 31000 KB
#include <bits/stdc++.h>
using namespace std;
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define sp " "
#define endl "\n"
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define N 300005
#define modulo 1000000007
#define int long long
#define M 20000005
const int INF = 2e18 + 7;

int arr[N], cnt[N], dp[M], pre[N];

int f(int sz, int n, int mask, int ind)
{
	if (ind > sz) return 0;
	if (dp[mask] != -1) return dp[mask];
	int ans = INF;
	for (int i = 0; i < n; i++)
	{
		if (mask & (1<<i)) continue;
		int fin = ind + cnt[i + 1] - 1;
		int tmp = (sz * (sz + 1)) / 2 - (((ind - 1) * ind) / 2 + ((sz - fin) * (sz - fin + 1)) / 2);
		ans = min(ans, tmp + f(sz, n, mask | (1<<i), fin + 1));
	}
	return dp[mask] = ans;
}

void print(int sz, int n, int mask, int ind)
{
	if (ind > sz) 
	{
		cout<<endl;
		return;
	}
	int ans = INF;
	for (int i = 0; i < n; i++)
	{
		if (mask & (1<<i)) continue;
		int fin = ind + cnt[i + 1] - 1;
		int tmp = (sz * (sz + 1)) / 2 - (((ind - 1) * ind) / 2 + ((sz - fin) * (sz - fin + 1)) / 2);
		ans = min(ans, tmp + f(sz, n, mask | (1<<i), fin + 1));
	}
	for (int i = 0; i < n; i++)
	{
		if (mask & (1<<i)) continue;
		int fin = ind + cnt[i + 1] - 1;
		int tmp = (sz * (sz + 1)) / 2 - (((ind - 1) * ind) / 2 + ((sz - fin) * (sz - fin + 1)) / 2);
		if (ans == tmp + f(sz, n, mask | (1<<i), fin + 1))
		{
			cout<<cnt[i + 1]<<sp;
			print(sz, n, mask | (1<<i), fin + 1);
			return;
		}
	}
}

int32_t main()
{
	fastio();

	int n, q;
	cin>>n>>q;
	for (int i = 1; i <= n; i++)
		cin>>arr[i];

	while(q--)
	{
		memset(cnt, 0, sizeof(cnt));
		int l, r;
		cin>>l>>r;
		map<int, int> val;
		int counter = 1;
		for (int i = l; i <= r; i++)
		{
			if (val[arr[i]] == 0)
			{
				val[arr[i]] = counter;
				counter++;
			}
			cnt[val[arr[i]]]++;
		}
		/*
		int res = f(r - l + 1, counter - 1, 0, 1);
		print(r - l + 1, counter - 1, 0, 1);
		cout<<res<<endl;
		*/
		vector<int> sz;
		for (int i = 1; i < counter; i++)
			sz.pb(cnt[i]);

		sort(sz.begin(), sz.end());
		vector<int> a, b;
		if (sz.size() % 2 == 1)
		{
			a.pb(sz.back());
			sz.pop_back();
		}
		while(!sz.empty())
		{
			a.pb(sz.back());
			sz.pop_back();
			b.pb(sz.back());
			sz.pop_back();
		}
		reverse(a.begin(), a.end());
		vector<int> res;
		for (auto i : a) res.pb(i);
		for (auto i : b) res.pb(i);
		for (int i = 0; i < res.size(); i++)
			pre[i + 1] = pre[i] + res[i];
		int m = res.size(), ans = 0;
		for (int i = 0; i < res.size(); i++)
		{
			int tmp = pre[m] * (pre[m] + 1);
			tmp /= 2;
			tmp -= (pre[i] * (pre[i] + 1)) / 2;
			tmp -= ((pre[m] - pre[i + 1]) * (pre[m] - pre[i + 1] + 1)) / 2;
			ans += tmp;
		}
		cout<<ans<<endl;
	}
}

Compilation message

diversity.cpp: In function 'int32_t main()':
diversity.cpp:114:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |   for (int i = 0; i < res.size(); i++)
      |                   ~~^~~~~~~~~~~~
diversity.cpp:117:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |   for (int i = 0; i < res.size(); i++)
      |                   ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2684 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 9 ms 3412 KB Output is correct
5 Correct 19 ms 4248 KB Output is correct
6 Correct 26 ms 4984 KB Output is correct
7 Correct 22 ms 5008 KB Output is correct
8 Correct 27 ms 5084 KB Output is correct
9 Correct 22 ms 5020 KB Output is correct
10 Correct 24 ms 4912 KB Output is correct
11 Correct 28 ms 5092 KB Output is correct
12 Correct 31 ms 5000 KB Output is correct
13 Correct 26 ms 4948 KB Output is correct
14 Correct 35 ms 4948 KB Output is correct
15 Correct 32 ms 4980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 9 ms 3412 KB Output is correct
5 Correct 19 ms 4248 KB Output is correct
6 Correct 26 ms 4984 KB Output is correct
7 Correct 22 ms 5008 KB Output is correct
8 Correct 27 ms 5084 KB Output is correct
9 Correct 22 ms 5020 KB Output is correct
10 Correct 24 ms 4912 KB Output is correct
11 Correct 28 ms 5092 KB Output is correct
12 Correct 31 ms 5000 KB Output is correct
13 Correct 26 ms 4948 KB Output is correct
14 Correct 35 ms 4948 KB Output is correct
15 Correct 32 ms 4980 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 2 ms 2772 KB Output is correct
19 Correct 15 ms 3412 KB Output is correct
20 Correct 19 ms 4188 KB Output is correct
21 Correct 28 ms 5008 KB Output is correct
22 Correct 40 ms 5008 KB Output is correct
23 Correct 29 ms 4948 KB Output is correct
24 Correct 27 ms 4948 KB Output is correct
25 Correct 27 ms 4948 KB Output is correct
26 Correct 26 ms 4984 KB Output is correct
27 Correct 24 ms 5024 KB Output is correct
28 Correct 26 ms 4936 KB Output is correct
29 Correct 25 ms 4980 KB Output is correct
30 Correct 26 ms 4992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 9 ms 3412 KB Output is correct
5 Correct 19 ms 4248 KB Output is correct
6 Correct 26 ms 4984 KB Output is correct
7 Correct 22 ms 5008 KB Output is correct
8 Correct 27 ms 5084 KB Output is correct
9 Correct 22 ms 5020 KB Output is correct
10 Correct 24 ms 4912 KB Output is correct
11 Correct 28 ms 5092 KB Output is correct
12 Correct 31 ms 5000 KB Output is correct
13 Correct 26 ms 4948 KB Output is correct
14 Correct 35 ms 4948 KB Output is correct
15 Correct 32 ms 4980 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 2 ms 2772 KB Output is correct
19 Correct 15 ms 3412 KB Output is correct
20 Correct 19 ms 4188 KB Output is correct
21 Correct 28 ms 5008 KB Output is correct
22 Correct 40 ms 5008 KB Output is correct
23 Correct 29 ms 4948 KB Output is correct
24 Correct 27 ms 4948 KB Output is correct
25 Correct 27 ms 4948 KB Output is correct
26 Correct 26 ms 4984 KB Output is correct
27 Correct 24 ms 5024 KB Output is correct
28 Correct 26 ms 4936 KB Output is correct
29 Correct 25 ms 4980 KB Output is correct
30 Correct 26 ms 4992 KB Output is correct
31 Correct 1 ms 2644 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 2 ms 2716 KB Output is correct
34 Correct 2 ms 2772 KB Output is correct
35 Correct 4 ms 2680 KB Output is correct
36 Correct 3 ms 2772 KB Output is correct
37 Correct 8 ms 3028 KB Output is correct
38 Correct 9 ms 3140 KB Output is correct
39 Correct 18 ms 3540 KB Output is correct
40 Correct 33 ms 4308 KB Output is correct
41 Correct 50 ms 5064 KB Output is correct
42 Correct 53 ms 5092 KB Output is correct
43 Correct 50 ms 5024 KB Output is correct
44 Correct 47 ms 5084 KB Output is correct
45 Correct 51 ms 5072 KB Output is correct
46 Correct 52 ms 5084 KB Output is correct
47 Correct 49 ms 5084 KB Output is correct
48 Correct 62 ms 5072 KB Output is correct
49 Correct 50 ms 5072 KB Output is correct
50 Correct 51 ms 5084 KB Output is correct
51 Correct 49 ms 5072 KB Output is correct
52 Correct 48 ms 5076 KB Output is correct
53 Correct 47 ms 5096 KB Output is correct
54 Correct 61 ms 5076 KB Output is correct
55 Correct 56 ms 5076 KB Output is correct
56 Correct 48 ms 5100 KB Output is correct
57 Correct 48 ms 5084 KB Output is correct
58 Correct 48 ms 5068 KB Output is correct
59 Correct 46 ms 5076 KB Output is correct
60 Correct 49 ms 5132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2684 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 9 ms 3412 KB Output is correct
15 Correct 19 ms 4248 KB Output is correct
16 Correct 26 ms 4984 KB Output is correct
17 Correct 22 ms 5008 KB Output is correct
18 Correct 27 ms 5084 KB Output is correct
19 Correct 22 ms 5020 KB Output is correct
20 Correct 24 ms 4912 KB Output is correct
21 Correct 28 ms 5092 KB Output is correct
22 Correct 31 ms 5000 KB Output is correct
23 Correct 26 ms 4948 KB Output is correct
24 Correct 35 ms 4948 KB Output is correct
25 Correct 32 ms 4980 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 15 ms 3412 KB Output is correct
30 Correct 19 ms 4188 KB Output is correct
31 Correct 28 ms 5008 KB Output is correct
32 Correct 40 ms 5008 KB Output is correct
33 Correct 29 ms 4948 KB Output is correct
34 Correct 27 ms 4948 KB Output is correct
35 Correct 27 ms 4948 KB Output is correct
36 Correct 26 ms 4984 KB Output is correct
37 Correct 24 ms 5024 KB Output is correct
38 Correct 26 ms 4936 KB Output is correct
39 Correct 25 ms 4980 KB Output is correct
40 Correct 26 ms 4992 KB Output is correct
41 Correct 1 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 2 ms 2716 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
45 Correct 4 ms 2680 KB Output is correct
46 Correct 3 ms 2772 KB Output is correct
47 Correct 8 ms 3028 KB Output is correct
48 Correct 9 ms 3140 KB Output is correct
49 Correct 18 ms 3540 KB Output is correct
50 Correct 33 ms 4308 KB Output is correct
51 Correct 50 ms 5064 KB Output is correct
52 Correct 53 ms 5092 KB Output is correct
53 Correct 50 ms 5024 KB Output is correct
54 Correct 47 ms 5084 KB Output is correct
55 Correct 51 ms 5072 KB Output is correct
56 Correct 52 ms 5084 KB Output is correct
57 Correct 49 ms 5084 KB Output is correct
58 Correct 62 ms 5072 KB Output is correct
59 Correct 50 ms 5072 KB Output is correct
60 Correct 51 ms 5084 KB Output is correct
61 Correct 49 ms 5072 KB Output is correct
62 Correct 48 ms 5076 KB Output is correct
63 Correct 47 ms 5096 KB Output is correct
64 Correct 61 ms 5076 KB Output is correct
65 Correct 56 ms 5076 KB Output is correct
66 Correct 48 ms 5100 KB Output is correct
67 Correct 48 ms 5084 KB Output is correct
68 Correct 48 ms 5068 KB Output is correct
69 Correct 46 ms 5076 KB Output is correct
70 Correct 49 ms 5132 KB Output is correct
71 Correct 11 ms 3284 KB Output is correct
72 Correct 13 ms 3284 KB Output is correct
73 Correct 13 ms 3200 KB Output is correct
74 Correct 13 ms 3276 KB Output is correct
75 Correct 14 ms 3200 KB Output is correct
76 Correct 24 ms 3960 KB Output is correct
77 Correct 23 ms 3924 KB Output is correct
78 Correct 26 ms 3932 KB Output is correct
79 Correct 25 ms 4024 KB Output is correct
80 Correct 24 ms 3916 KB Output is correct
81 Correct 54 ms 5244 KB Output is correct
82 Correct 59 ms 5220 KB Output is correct
83 Correct 53 ms 5240 KB Output is correct
84 Correct 51 ms 5232 KB Output is correct
85 Correct 52 ms 5292 KB Output is correct
86 Correct 64 ms 6340 KB Output is correct
87 Correct 62 ms 6348 KB Output is correct
88 Correct 65 ms 6356 KB Output is correct
89 Correct 66 ms 6508 KB Output is correct
90 Correct 63 ms 6388 KB Output is correct
91 Correct 144 ms 15584 KB Output is correct
92 Correct 155 ms 15564 KB Output is correct
93 Correct 153 ms 15628 KB Output is correct
94 Correct 156 ms 15552 KB Output is correct
95 Correct 144 ms 15608 KB Output is correct
96 Correct 236 ms 26180 KB Output is correct
97 Correct 249 ms 26208 KB Output is correct
98 Correct 205 ms 26108 KB Output is correct
99 Correct 215 ms 26156 KB Output is correct
100 Correct 226 ms 26264 KB Output is correct
101 Correct 220 ms 26104 KB Output is correct
102 Correct 228 ms 26200 KB Output is correct
103 Correct 240 ms 26148 KB Output is correct
104 Correct 227 ms 26124 KB Output is correct
105 Correct 206 ms 26180 KB Output is correct
106 Correct 234 ms 30996 KB Output is correct
107 Correct 251 ms 30808 KB Output is correct
108 Correct 267 ms 30948 KB Output is correct
109 Correct 258 ms 30840 KB Output is correct
110 Correct 244 ms 31000 KB Output is correct
111 Correct 257 ms 30892 KB Output is correct
112 Correct 243 ms 30880 KB Output is correct
113 Correct 251 ms 30892 KB Output is correct
114 Correct 256 ms 30828 KB Output is correct
115 Correct 239 ms 30908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2684 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 9 ms 3412 KB Output is correct
15 Correct 19 ms 4248 KB Output is correct
16 Correct 26 ms 4984 KB Output is correct
17 Correct 22 ms 5008 KB Output is correct
18 Correct 27 ms 5084 KB Output is correct
19 Correct 22 ms 5020 KB Output is correct
20 Correct 24 ms 4912 KB Output is correct
21 Correct 28 ms 5092 KB Output is correct
22 Correct 31 ms 5000 KB Output is correct
23 Correct 26 ms 4948 KB Output is correct
24 Correct 35 ms 4948 KB Output is correct
25 Correct 32 ms 4980 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 15 ms 3412 KB Output is correct
30 Correct 19 ms 4188 KB Output is correct
31 Correct 28 ms 5008 KB Output is correct
32 Correct 40 ms 5008 KB Output is correct
33 Correct 29 ms 4948 KB Output is correct
34 Correct 27 ms 4948 KB Output is correct
35 Correct 27 ms 4948 KB Output is correct
36 Correct 26 ms 4984 KB Output is correct
37 Correct 24 ms 5024 KB Output is correct
38 Correct 26 ms 4936 KB Output is correct
39 Correct 25 ms 4980 KB Output is correct
40 Correct 26 ms 4992 KB Output is correct
41 Correct 1 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 2 ms 2716 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
45 Correct 4 ms 2680 KB Output is correct
46 Correct 3 ms 2772 KB Output is correct
47 Correct 8 ms 3028 KB Output is correct
48 Correct 9 ms 3140 KB Output is correct
49 Correct 18 ms 3540 KB Output is correct
50 Correct 33 ms 4308 KB Output is correct
51 Correct 50 ms 5064 KB Output is correct
52 Correct 53 ms 5092 KB Output is correct
53 Correct 50 ms 5024 KB Output is correct
54 Correct 47 ms 5084 KB Output is correct
55 Correct 51 ms 5072 KB Output is correct
56 Correct 52 ms 5084 KB Output is correct
57 Correct 49 ms 5084 KB Output is correct
58 Correct 62 ms 5072 KB Output is correct
59 Correct 50 ms 5072 KB Output is correct
60 Correct 51 ms 5084 KB Output is correct
61 Correct 49 ms 5072 KB Output is correct
62 Correct 48 ms 5076 KB Output is correct
63 Correct 47 ms 5096 KB Output is correct
64 Correct 61 ms 5076 KB Output is correct
65 Correct 56 ms 5076 KB Output is correct
66 Correct 48 ms 5100 KB Output is correct
67 Correct 48 ms 5084 KB Output is correct
68 Correct 48 ms 5068 KB Output is correct
69 Correct 46 ms 5076 KB Output is correct
70 Correct 49 ms 5132 KB Output is correct
71 Correct 11 ms 3284 KB Output is correct
72 Correct 13 ms 3284 KB Output is correct
73 Correct 13 ms 3200 KB Output is correct
74 Correct 13 ms 3276 KB Output is correct
75 Correct 14 ms 3200 KB Output is correct
76 Correct 24 ms 3960 KB Output is correct
77 Correct 23 ms 3924 KB Output is correct
78 Correct 26 ms 3932 KB Output is correct
79 Correct 25 ms 4024 KB Output is correct
80 Correct 24 ms 3916 KB Output is correct
81 Correct 54 ms 5244 KB Output is correct
82 Correct 59 ms 5220 KB Output is correct
83 Correct 53 ms 5240 KB Output is correct
84 Correct 51 ms 5232 KB Output is correct
85 Correct 52 ms 5292 KB Output is correct
86 Correct 64 ms 6340 KB Output is correct
87 Correct 62 ms 6348 KB Output is correct
88 Correct 65 ms 6356 KB Output is correct
89 Correct 66 ms 6508 KB Output is correct
90 Correct 63 ms 6388 KB Output is correct
91 Correct 144 ms 15584 KB Output is correct
92 Correct 155 ms 15564 KB Output is correct
93 Correct 153 ms 15628 KB Output is correct
94 Correct 156 ms 15552 KB Output is correct
95 Correct 144 ms 15608 KB Output is correct
96 Correct 236 ms 26180 KB Output is correct
97 Correct 249 ms 26208 KB Output is correct
98 Correct 205 ms 26108 KB Output is correct
99 Correct 215 ms 26156 KB Output is correct
100 Correct 226 ms 26264 KB Output is correct
101 Correct 220 ms 26104 KB Output is correct
102 Correct 228 ms 26200 KB Output is correct
103 Correct 240 ms 26148 KB Output is correct
104 Correct 227 ms 26124 KB Output is correct
105 Correct 206 ms 26180 KB Output is correct
106 Correct 234 ms 30996 KB Output is correct
107 Correct 251 ms 30808 KB Output is correct
108 Correct 267 ms 30948 KB Output is correct
109 Correct 258 ms 30840 KB Output is correct
110 Correct 244 ms 31000 KB Output is correct
111 Correct 257 ms 30892 KB Output is correct
112 Correct 243 ms 30880 KB Output is correct
113 Correct 251 ms 30892 KB Output is correct
114 Correct 256 ms 30828 KB Output is correct
115 Correct 239 ms 30908 KB Output is correct
116 Execution timed out 7023 ms 3492 KB Time limit exceeded
117 Halted 0 ms 0 KB -