Submission #621047

# Submission time Handle Problem Language Result Execution time Memory
621047 2022-08-03T11:28:19 Z dozer Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 189396 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));
		memset(dp, -1, sizeof(dp));
		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:115: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]
  115 |   for (int i = 0; i < res.size(); i++)
      |                   ~~^~~~~~~~~~~~
diversity.cpp:118: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]
  118 |   for (int i = 0; i < res.size(); i++)
      |                   ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 59 ms 159156 KB Output is correct
2 Correct 62 ms 159212 KB Output is correct
3 Correct 71 ms 159200 KB Output is correct
4 Correct 74 ms 159112 KB Output is correct
5 Correct 58 ms 159096 KB Output is correct
6 Correct 60 ms 159216 KB Output is correct
7 Correct 60 ms 159104 KB Output is correct
8 Correct 64 ms 159132 KB Output is correct
9 Correct 74 ms 159124 KB Output is correct
10 Correct 62 ms 159180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 159212 KB Output is correct
2 Correct 68 ms 159228 KB Output is correct
3 Correct 66 ms 159240 KB Output is correct
4 Correct 70 ms 160100 KB Output is correct
5 Correct 71 ms 161164 KB Output is correct
6 Correct 94 ms 162232 KB Output is correct
7 Correct 83 ms 162052 KB Output is correct
8 Correct 100 ms 162184 KB Output is correct
9 Correct 96 ms 162108 KB Output is correct
10 Correct 84 ms 162120 KB Output is correct
11 Correct 91 ms 162144 KB Output is correct
12 Correct 84 ms 162228 KB Output is correct
13 Correct 89 ms 162204 KB Output is correct
14 Correct 105 ms 162096 KB Output is correct
15 Correct 92 ms 162096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 159212 KB Output is correct
2 Correct 68 ms 159228 KB Output is correct
3 Correct 66 ms 159240 KB Output is correct
4 Correct 70 ms 160100 KB Output is correct
5 Correct 71 ms 161164 KB Output is correct
6 Correct 94 ms 162232 KB Output is correct
7 Correct 83 ms 162052 KB Output is correct
8 Correct 100 ms 162184 KB Output is correct
9 Correct 96 ms 162108 KB Output is correct
10 Correct 84 ms 162120 KB Output is correct
11 Correct 91 ms 162144 KB Output is correct
12 Correct 84 ms 162228 KB Output is correct
13 Correct 89 ms 162204 KB Output is correct
14 Correct 105 ms 162096 KB Output is correct
15 Correct 92 ms 162096 KB Output is correct
16 Correct 59 ms 159108 KB Output is correct
17 Correct 71 ms 159184 KB Output is correct
18 Correct 80 ms 159236 KB Output is correct
19 Correct 78 ms 160176 KB Output is correct
20 Correct 82 ms 161232 KB Output is correct
21 Correct 85 ms 162376 KB Output is correct
22 Correct 86 ms 162328 KB Output is correct
23 Correct 86 ms 162308 KB Output is correct
24 Correct 99 ms 162316 KB Output is correct
25 Correct 90 ms 162248 KB Output is correct
26 Correct 107 ms 162320 KB Output is correct
27 Correct 104 ms 162248 KB Output is correct
28 Correct 95 ms 162180 KB Output is correct
29 Correct 99 ms 162188 KB Output is correct
30 Correct 99 ms 162340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 159212 KB Output is correct
2 Correct 68 ms 159228 KB Output is correct
3 Correct 66 ms 159240 KB Output is correct
4 Correct 70 ms 160100 KB Output is correct
5 Correct 71 ms 161164 KB Output is correct
6 Correct 94 ms 162232 KB Output is correct
7 Correct 83 ms 162052 KB Output is correct
8 Correct 100 ms 162184 KB Output is correct
9 Correct 96 ms 162108 KB Output is correct
10 Correct 84 ms 162120 KB Output is correct
11 Correct 91 ms 162144 KB Output is correct
12 Correct 84 ms 162228 KB Output is correct
13 Correct 89 ms 162204 KB Output is correct
14 Correct 105 ms 162096 KB Output is correct
15 Correct 92 ms 162096 KB Output is correct
16 Correct 59 ms 159108 KB Output is correct
17 Correct 71 ms 159184 KB Output is correct
18 Correct 80 ms 159236 KB Output is correct
19 Correct 78 ms 160176 KB Output is correct
20 Correct 82 ms 161232 KB Output is correct
21 Correct 85 ms 162376 KB Output is correct
22 Correct 86 ms 162328 KB Output is correct
23 Correct 86 ms 162308 KB Output is correct
24 Correct 99 ms 162316 KB Output is correct
25 Correct 90 ms 162248 KB Output is correct
26 Correct 107 ms 162320 KB Output is correct
27 Correct 104 ms 162248 KB Output is correct
28 Correct 95 ms 162180 KB Output is correct
29 Correct 99 ms 162188 KB Output is correct
30 Correct 99 ms 162340 KB Output is correct
31 Correct 77 ms 159240 KB Output is correct
32 Correct 67 ms 159176 KB Output is correct
33 Correct 64 ms 159132 KB Output is correct
34 Correct 62 ms 159308 KB Output is correct
35 Correct 62 ms 159284 KB Output is correct
36 Correct 67 ms 159444 KB Output is correct
37 Correct 69 ms 159820 KB Output is correct
38 Correct 77 ms 159908 KB Output is correct
39 Correct 90 ms 160376 KB Output is correct
40 Correct 106 ms 161616 KB Output is correct
41 Correct 118 ms 162756 KB Output is correct
42 Correct 113 ms 162764 KB Output is correct
43 Correct 116 ms 162764 KB Output is correct
44 Correct 128 ms 162712 KB Output is correct
45 Correct 110 ms 162768 KB Output is correct
46 Correct 126 ms 162800 KB Output is correct
47 Correct 123 ms 162804 KB Output is correct
48 Correct 111 ms 162748 KB Output is correct
49 Correct 106 ms 162736 KB Output is correct
50 Correct 120 ms 162764 KB Output is correct
51 Correct 125 ms 162812 KB Output is correct
52 Correct 141 ms 162748 KB Output is correct
53 Correct 104 ms 162764 KB Output is correct
54 Correct 139 ms 162716 KB Output is correct
55 Correct 112 ms 162768 KB Output is correct
56 Correct 142 ms 162764 KB Output is correct
57 Correct 131 ms 162768 KB Output is correct
58 Correct 113 ms 162708 KB Output is correct
59 Correct 132 ms 162712 KB Output is correct
60 Correct 108 ms 162764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 159156 KB Output is correct
2 Correct 62 ms 159212 KB Output is correct
3 Correct 71 ms 159200 KB Output is correct
4 Correct 74 ms 159112 KB Output is correct
5 Correct 58 ms 159096 KB Output is correct
6 Correct 60 ms 159216 KB Output is correct
7 Correct 60 ms 159104 KB Output is correct
8 Correct 64 ms 159132 KB Output is correct
9 Correct 74 ms 159124 KB Output is correct
10 Correct 62 ms 159180 KB Output is correct
11 Correct 59 ms 159212 KB Output is correct
12 Correct 68 ms 159228 KB Output is correct
13 Correct 66 ms 159240 KB Output is correct
14 Correct 70 ms 160100 KB Output is correct
15 Correct 71 ms 161164 KB Output is correct
16 Correct 94 ms 162232 KB Output is correct
17 Correct 83 ms 162052 KB Output is correct
18 Correct 100 ms 162184 KB Output is correct
19 Correct 96 ms 162108 KB Output is correct
20 Correct 84 ms 162120 KB Output is correct
21 Correct 91 ms 162144 KB Output is correct
22 Correct 84 ms 162228 KB Output is correct
23 Correct 89 ms 162204 KB Output is correct
24 Correct 105 ms 162096 KB Output is correct
25 Correct 92 ms 162096 KB Output is correct
26 Correct 59 ms 159108 KB Output is correct
27 Correct 71 ms 159184 KB Output is correct
28 Correct 80 ms 159236 KB Output is correct
29 Correct 78 ms 160176 KB Output is correct
30 Correct 82 ms 161232 KB Output is correct
31 Correct 85 ms 162376 KB Output is correct
32 Correct 86 ms 162328 KB Output is correct
33 Correct 86 ms 162308 KB Output is correct
34 Correct 99 ms 162316 KB Output is correct
35 Correct 90 ms 162248 KB Output is correct
36 Correct 107 ms 162320 KB Output is correct
37 Correct 104 ms 162248 KB Output is correct
38 Correct 95 ms 162180 KB Output is correct
39 Correct 99 ms 162188 KB Output is correct
40 Correct 99 ms 162340 KB Output is correct
41 Correct 77 ms 159240 KB Output is correct
42 Correct 67 ms 159176 KB Output is correct
43 Correct 64 ms 159132 KB Output is correct
44 Correct 62 ms 159308 KB Output is correct
45 Correct 62 ms 159284 KB Output is correct
46 Correct 67 ms 159444 KB Output is correct
47 Correct 69 ms 159820 KB Output is correct
48 Correct 77 ms 159908 KB Output is correct
49 Correct 90 ms 160376 KB Output is correct
50 Correct 106 ms 161616 KB Output is correct
51 Correct 118 ms 162756 KB Output is correct
52 Correct 113 ms 162764 KB Output is correct
53 Correct 116 ms 162764 KB Output is correct
54 Correct 128 ms 162712 KB Output is correct
55 Correct 110 ms 162768 KB Output is correct
56 Correct 126 ms 162800 KB Output is correct
57 Correct 123 ms 162804 KB Output is correct
58 Correct 111 ms 162748 KB Output is correct
59 Correct 106 ms 162736 KB Output is correct
60 Correct 120 ms 162764 KB Output is correct
61 Correct 125 ms 162812 KB Output is correct
62 Correct 141 ms 162748 KB Output is correct
63 Correct 104 ms 162764 KB Output is correct
64 Correct 139 ms 162716 KB Output is correct
65 Correct 112 ms 162768 KB Output is correct
66 Correct 142 ms 162764 KB Output is correct
67 Correct 131 ms 162768 KB Output is correct
68 Correct 113 ms 162708 KB Output is correct
69 Correct 132 ms 162712 KB Output is correct
70 Correct 108 ms 162764 KB Output is correct
71 Correct 76 ms 160028 KB Output is correct
72 Correct 75 ms 159952 KB Output is correct
73 Correct 70 ms 159936 KB Output is correct
74 Correct 81 ms 159972 KB Output is correct
75 Correct 71 ms 159976 KB Output is correct
76 Correct 81 ms 161012 KB Output is correct
77 Correct 98 ms 160972 KB Output is correct
78 Correct 98 ms 161016 KB Output is correct
79 Correct 83 ms 160980 KB Output is correct
80 Correct 91 ms 161052 KB Output is correct
81 Correct 115 ms 162780 KB Output is correct
82 Correct 122 ms 162724 KB Output is correct
83 Correct 118 ms 162720 KB Output is correct
84 Correct 110 ms 162752 KB Output is correct
85 Correct 126 ms 162764 KB Output is correct
86 Correct 131 ms 163912 KB Output is correct
87 Correct 151 ms 163936 KB Output is correct
88 Correct 124 ms 163912 KB Output is correct
89 Correct 125 ms 163988 KB Output is correct
90 Correct 134 ms 163932 KB Output is correct
91 Correct 201 ms 173884 KB Output is correct
92 Correct 226 ms 173848 KB Output is correct
93 Correct 203 ms 173908 KB Output is correct
94 Correct 204 ms 173924 KB Output is correct
95 Correct 230 ms 173908 KB Output is correct
96 Correct 320 ms 184568 KB Output is correct
97 Correct 312 ms 184644 KB Output is correct
98 Correct 301 ms 184632 KB Output is correct
99 Correct 274 ms 184632 KB Output is correct
100 Correct 310 ms 184648 KB Output is correct
101 Correct 322 ms 184620 KB Output is correct
102 Correct 304 ms 184576 KB Output is correct
103 Correct 266 ms 184664 KB Output is correct
104 Correct 283 ms 184636 KB Output is correct
105 Correct 323 ms 184656 KB Output is correct
106 Correct 349 ms 189396 KB Output is correct
107 Correct 291 ms 189380 KB Output is correct
108 Correct 316 ms 189388 KB Output is correct
109 Correct 289 ms 189364 KB Output is correct
110 Correct 308 ms 189344 KB Output is correct
111 Correct 341 ms 189372 KB Output is correct
112 Correct 303 ms 189288 KB Output is correct
113 Correct 299 ms 189344 KB Output is correct
114 Correct 367 ms 189328 KB Output is correct
115 Correct 332 ms 189388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 159156 KB Output is correct
2 Correct 62 ms 159212 KB Output is correct
3 Correct 71 ms 159200 KB Output is correct
4 Correct 74 ms 159112 KB Output is correct
5 Correct 58 ms 159096 KB Output is correct
6 Correct 60 ms 159216 KB Output is correct
7 Correct 60 ms 159104 KB Output is correct
8 Correct 64 ms 159132 KB Output is correct
9 Correct 74 ms 159124 KB Output is correct
10 Correct 62 ms 159180 KB Output is correct
11 Correct 59 ms 159212 KB Output is correct
12 Correct 68 ms 159228 KB Output is correct
13 Correct 66 ms 159240 KB Output is correct
14 Correct 70 ms 160100 KB Output is correct
15 Correct 71 ms 161164 KB Output is correct
16 Correct 94 ms 162232 KB Output is correct
17 Correct 83 ms 162052 KB Output is correct
18 Correct 100 ms 162184 KB Output is correct
19 Correct 96 ms 162108 KB Output is correct
20 Correct 84 ms 162120 KB Output is correct
21 Correct 91 ms 162144 KB Output is correct
22 Correct 84 ms 162228 KB Output is correct
23 Correct 89 ms 162204 KB Output is correct
24 Correct 105 ms 162096 KB Output is correct
25 Correct 92 ms 162096 KB Output is correct
26 Correct 59 ms 159108 KB Output is correct
27 Correct 71 ms 159184 KB Output is correct
28 Correct 80 ms 159236 KB Output is correct
29 Correct 78 ms 160176 KB Output is correct
30 Correct 82 ms 161232 KB Output is correct
31 Correct 85 ms 162376 KB Output is correct
32 Correct 86 ms 162328 KB Output is correct
33 Correct 86 ms 162308 KB Output is correct
34 Correct 99 ms 162316 KB Output is correct
35 Correct 90 ms 162248 KB Output is correct
36 Correct 107 ms 162320 KB Output is correct
37 Correct 104 ms 162248 KB Output is correct
38 Correct 95 ms 162180 KB Output is correct
39 Correct 99 ms 162188 KB Output is correct
40 Correct 99 ms 162340 KB Output is correct
41 Correct 77 ms 159240 KB Output is correct
42 Correct 67 ms 159176 KB Output is correct
43 Correct 64 ms 159132 KB Output is correct
44 Correct 62 ms 159308 KB Output is correct
45 Correct 62 ms 159284 KB Output is correct
46 Correct 67 ms 159444 KB Output is correct
47 Correct 69 ms 159820 KB Output is correct
48 Correct 77 ms 159908 KB Output is correct
49 Correct 90 ms 160376 KB Output is correct
50 Correct 106 ms 161616 KB Output is correct
51 Correct 118 ms 162756 KB Output is correct
52 Correct 113 ms 162764 KB Output is correct
53 Correct 116 ms 162764 KB Output is correct
54 Correct 128 ms 162712 KB Output is correct
55 Correct 110 ms 162768 KB Output is correct
56 Correct 126 ms 162800 KB Output is correct
57 Correct 123 ms 162804 KB Output is correct
58 Correct 111 ms 162748 KB Output is correct
59 Correct 106 ms 162736 KB Output is correct
60 Correct 120 ms 162764 KB Output is correct
61 Correct 125 ms 162812 KB Output is correct
62 Correct 141 ms 162748 KB Output is correct
63 Correct 104 ms 162764 KB Output is correct
64 Correct 139 ms 162716 KB Output is correct
65 Correct 112 ms 162768 KB Output is correct
66 Correct 142 ms 162764 KB Output is correct
67 Correct 131 ms 162768 KB Output is correct
68 Correct 113 ms 162708 KB Output is correct
69 Correct 132 ms 162712 KB Output is correct
70 Correct 108 ms 162764 KB Output is correct
71 Correct 76 ms 160028 KB Output is correct
72 Correct 75 ms 159952 KB Output is correct
73 Correct 70 ms 159936 KB Output is correct
74 Correct 81 ms 159972 KB Output is correct
75 Correct 71 ms 159976 KB Output is correct
76 Correct 81 ms 161012 KB Output is correct
77 Correct 98 ms 160972 KB Output is correct
78 Correct 98 ms 161016 KB Output is correct
79 Correct 83 ms 160980 KB Output is correct
80 Correct 91 ms 161052 KB Output is correct
81 Correct 115 ms 162780 KB Output is correct
82 Correct 122 ms 162724 KB Output is correct
83 Correct 118 ms 162720 KB Output is correct
84 Correct 110 ms 162752 KB Output is correct
85 Correct 126 ms 162764 KB Output is correct
86 Correct 131 ms 163912 KB Output is correct
87 Correct 151 ms 163936 KB Output is correct
88 Correct 124 ms 163912 KB Output is correct
89 Correct 125 ms 163988 KB Output is correct
90 Correct 134 ms 163932 KB Output is correct
91 Correct 201 ms 173884 KB Output is correct
92 Correct 226 ms 173848 KB Output is correct
93 Correct 203 ms 173908 KB Output is correct
94 Correct 204 ms 173924 KB Output is correct
95 Correct 230 ms 173908 KB Output is correct
96 Correct 320 ms 184568 KB Output is correct
97 Correct 312 ms 184644 KB Output is correct
98 Correct 301 ms 184632 KB Output is correct
99 Correct 274 ms 184632 KB Output is correct
100 Correct 310 ms 184648 KB Output is correct
101 Correct 322 ms 184620 KB Output is correct
102 Correct 304 ms 184576 KB Output is correct
103 Correct 266 ms 184664 KB Output is correct
104 Correct 283 ms 184636 KB Output is correct
105 Correct 323 ms 184656 KB Output is correct
106 Correct 349 ms 189396 KB Output is correct
107 Correct 291 ms 189380 KB Output is correct
108 Correct 316 ms 189388 KB Output is correct
109 Correct 289 ms 189364 KB Output is correct
110 Correct 308 ms 189344 KB Output is correct
111 Correct 341 ms 189372 KB Output is correct
112 Correct 303 ms 189288 KB Output is correct
113 Correct 299 ms 189344 KB Output is correct
114 Correct 367 ms 189328 KB Output is correct
115 Correct 332 ms 189388 KB Output is correct
116 Execution timed out 7020 ms 159416 KB Time limit exceeded
117 Halted 0 ms 0 KB -