Submission #617321

# Submission time Handle Problem Language Result Execution time Memory
617321 2022-08-01T10:47:27 Z Mr_Husanboy Diversity (CEOI21_diversity) C++14
64 / 100
334 ms 28776 KB
#pragma GCC optimize("Ofast")

#include<bits/stdc++.h>


 
 
using namespace std;
using ll = long long;


 
mt19937 rng(123);
#define ld long double
#define ull unsigned long long 
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(a) a.begin(), a.end()
#define ff first
#define ss second
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define setp(x) setprecision(x)


ll mod=1e9+7;
int inf=1e9;
const int LOGN = 20;
const int MXX=3e5;

const long double PI = 3.1415926535897932384626433832795;



ll manhat(vector<ll> v){
	ll sum = 0;
	ll dis = 0;
	ll n = v.size();
	for(ll i = 0; i < v.size(); i ++){
		dis +=(v[i] * i - sum);
		sum += v[i];
	}
	return dis + (n-1)*n / 2;
}


ll calc(deque<ll> v){
	ll ans = 0;
	for(auto u : v) ans += (u+1)*u/2;
//	cout << ans << " ans" << endl;
	map<ll,vector<ll>> mp;
	int n = v.size();
	for(int i = 0; i < n; i ++){
		mp[v[i]].push_back(i);
	}
	vector<pair<ll,vll>>  a;
	vector<ll> dis;
	for(auto u : mp) a.push_back({u.ff,u.ss}), dis.push_back(manhat(u.ss));
/*	for(auto u : a){
		cout << u.ff << ": "; for(auto it : u.ss) cout << it << ' '; cout << endl;
	}*/
	
	n = a.size();
	for(int i = 0; i < n; i ++){
		ans += dis[i] * a[i].ff * a[i].ff;
//		cout << dis[i] << endl;;
		for(int j = i + 1; j < n; j ++){
			vector<ll> tem;
			merge(all(a[i].ss), all(a[j].ss), back_inserter(tem));
	//		cout << a[i].ff << "-" << a[j].ff << ' ' << manhat(tem) - dis[i] - dis[j] << endl;
			ans += a[i].ff * a[j].ff * (manhat(tem) - dis[i] - dis[j]);
	//		cout << ans << endl;
		}
	}
	return ans;
}

/*
9 1
1 1 1 2 2 2 3 3 4
*/

ll calcc(deque<ll> v){
	ll ans = 0;
//	cout << "calcc \n";
	for(auto u : v) ans += (u+1)*u/2;
	int n = v.size();
	for(ll i = 0; i < n; i ++){
		for(ll j = i + 1; j < n; j ++){
			ans += (j-i + 1) * (v[j] * v[i]);
		//	cout << v[j] << "-" << v[i] << ' ' << (j-i+1) << endl;
		}
	}
	return ans;
	
}


void solve(){
	int n, q; cin >> n >> q;
	map<int,int> mp;
	for(int i = 0; i<n; i ++){
		int x; cin >> x; x --;
		mp[x] ++;
	}
	vector<pair<int,int>> v;
	for(auto u : mp){
		v.push_back({u.ss,u.ff});
	}
	sort(all(v), greater<>());
//	for(auto u : v) cout << u.ff << ' ' << u.ss << endl;cout << endl;
	deque<int> a;
	int col = 1;
	deque<ll> arr;
	for(auto u : v){
		int tem = u.ff;
		if(col){
		//	while(tem --){ a.push_back(u.ss);}
			arr.push_back(u.ff);
		}else{
		//	while(tem --){	a.push_front(u.ss);}
			arr.push_front(u.ff);
		}
		col^=1;
	}
	cout << calc(arr);
//	cout << manhat({1,2,3});
}

 
int main(){
	int testcases = 1;
//	cin >> testcases;
	while(testcases --){
		solve(); if(testcases) cout << '\n';
	//	cout << endl;
	}
} 

Compilation message

diversity.cpp: In function 'll manhat(std::vector<long long int>)':
diversity.cpp:41:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |  for(ll i = 0; i < v.size(); i ++){
      |                ~~^~~~~~~~~~
diversity.cpp: In function 'void solve()':
diversity.cpp:118:7: warning: unused variable 'tem' [-Wunused-variable]
  118 |   int tem = u.ff;
      |       ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 21 ms 300 KB Output is correct
5 Correct 41 ms 296 KB Output is correct
6 Correct 63 ms 276 KB Output is correct
7 Correct 52 ms 280 KB Output is correct
8 Correct 54 ms 280 KB Output is correct
9 Correct 50 ms 288 KB Output is correct
10 Correct 54 ms 296 KB Output is correct
11 Correct 51 ms 280 KB Output is correct
12 Correct 56 ms 280 KB Output is correct
13 Correct 51 ms 296 KB Output is correct
14 Correct 71 ms 280 KB Output is correct
15 Correct 63 ms 284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 21 ms 300 KB Output is correct
5 Correct 41 ms 296 KB Output is correct
6 Correct 63 ms 276 KB Output is correct
7 Correct 52 ms 280 KB Output is correct
8 Correct 54 ms 280 KB Output is correct
9 Correct 50 ms 288 KB Output is correct
10 Correct 54 ms 296 KB Output is correct
11 Correct 51 ms 280 KB Output is correct
12 Correct 56 ms 280 KB Output is correct
13 Correct 51 ms 296 KB Output is correct
14 Correct 71 ms 280 KB Output is correct
15 Correct 63 ms 284 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 3 ms 212 KB Output is correct
19 Correct 20 ms 300 KB Output is correct
20 Correct 36 ms 276 KB Output is correct
21 Correct 52 ms 280 KB Output is correct
22 Correct 59 ms 292 KB Output is correct
23 Correct 50 ms 280 KB Output is correct
24 Correct 70 ms 280 KB Output is correct
25 Correct 60 ms 288 KB Output is correct
26 Correct 61 ms 292 KB Output is correct
27 Correct 57 ms 276 KB Output is correct
28 Correct 62 ms 296 KB Output is correct
29 Correct 54 ms 212 KB Output is correct
30 Correct 55 ms 276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 21 ms 300 KB Output is correct
5 Correct 41 ms 296 KB Output is correct
6 Correct 63 ms 276 KB Output is correct
7 Correct 52 ms 280 KB Output is correct
8 Correct 54 ms 280 KB Output is correct
9 Correct 50 ms 288 KB Output is correct
10 Correct 54 ms 296 KB Output is correct
11 Correct 51 ms 280 KB Output is correct
12 Correct 56 ms 280 KB Output is correct
13 Correct 51 ms 296 KB Output is correct
14 Correct 71 ms 280 KB Output is correct
15 Correct 63 ms 284 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 3 ms 212 KB Output is correct
19 Correct 20 ms 300 KB Output is correct
20 Correct 36 ms 276 KB Output is correct
21 Correct 52 ms 280 KB Output is correct
22 Correct 59 ms 292 KB Output is correct
23 Correct 50 ms 280 KB Output is correct
24 Correct 70 ms 280 KB Output is correct
25 Correct 60 ms 288 KB Output is correct
26 Correct 61 ms 292 KB Output is correct
27 Correct 57 ms 276 KB Output is correct
28 Correct 62 ms 296 KB Output is correct
29 Correct 54 ms 212 KB Output is correct
30 Correct 55 ms 276 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 3 ms 256 KB Output is correct
35 Correct 5 ms 340 KB Output is correct
36 Correct 4 ms 352 KB Output is correct
37 Correct 20 ms 308 KB Output is correct
38 Correct 22 ms 408 KB Output is correct
39 Correct 51 ms 424 KB Output is correct
40 Correct 96 ms 340 KB Output is correct
41 Correct 156 ms 472 KB Output is correct
42 Correct 159 ms 480 KB Output is correct
43 Correct 135 ms 476 KB Output is correct
44 Correct 148 ms 456 KB Output is correct
45 Correct 133 ms 456 KB Output is correct
46 Correct 112 ms 452 KB Output is correct
47 Correct 116 ms 480 KB Output is correct
48 Correct 100 ms 456 KB Output is correct
49 Correct 117 ms 456 KB Output is correct
50 Correct 107 ms 476 KB Output is correct
51 Correct 101 ms 456 KB Output is correct
52 Correct 116 ms 456 KB Output is correct
53 Correct 121 ms 456 KB Output is correct
54 Correct 122 ms 456 KB Output is correct
55 Correct 93 ms 460 KB Output is correct
56 Correct 110 ms 452 KB Output is correct
57 Correct 104 ms 452 KB Output is correct
58 Correct 101 ms 472 KB Output is correct
59 Correct 118 ms 456 KB Output is correct
60 Correct 112 ms 472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 212 KB Output is correct
14 Correct 21 ms 300 KB Output is correct
15 Correct 41 ms 296 KB Output is correct
16 Correct 63 ms 276 KB Output is correct
17 Correct 52 ms 280 KB Output is correct
18 Correct 54 ms 280 KB Output is correct
19 Correct 50 ms 288 KB Output is correct
20 Correct 54 ms 296 KB Output is correct
21 Correct 51 ms 280 KB Output is correct
22 Correct 56 ms 280 KB Output is correct
23 Correct 51 ms 296 KB Output is correct
24 Correct 71 ms 280 KB Output is correct
25 Correct 63 ms 284 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 3 ms 212 KB Output is correct
29 Correct 20 ms 300 KB Output is correct
30 Correct 36 ms 276 KB Output is correct
31 Correct 52 ms 280 KB Output is correct
32 Correct 59 ms 292 KB Output is correct
33 Correct 50 ms 280 KB Output is correct
34 Correct 70 ms 280 KB Output is correct
35 Correct 60 ms 288 KB Output is correct
36 Correct 61 ms 292 KB Output is correct
37 Correct 57 ms 276 KB Output is correct
38 Correct 62 ms 296 KB Output is correct
39 Correct 54 ms 212 KB Output is correct
40 Correct 55 ms 276 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 3 ms 256 KB Output is correct
45 Correct 5 ms 340 KB Output is correct
46 Correct 4 ms 352 KB Output is correct
47 Correct 20 ms 308 KB Output is correct
48 Correct 22 ms 408 KB Output is correct
49 Correct 51 ms 424 KB Output is correct
50 Correct 96 ms 340 KB Output is correct
51 Correct 156 ms 472 KB Output is correct
52 Correct 159 ms 480 KB Output is correct
53 Correct 135 ms 476 KB Output is correct
54 Correct 148 ms 456 KB Output is correct
55 Correct 133 ms 456 KB Output is correct
56 Correct 112 ms 452 KB Output is correct
57 Correct 116 ms 480 KB Output is correct
58 Correct 100 ms 456 KB Output is correct
59 Correct 117 ms 456 KB Output is correct
60 Correct 107 ms 476 KB Output is correct
61 Correct 101 ms 456 KB Output is correct
62 Correct 116 ms 456 KB Output is correct
63 Correct 121 ms 456 KB Output is correct
64 Correct 122 ms 456 KB Output is correct
65 Correct 93 ms 460 KB Output is correct
66 Correct 110 ms 452 KB Output is correct
67 Correct 104 ms 452 KB Output is correct
68 Correct 101 ms 472 KB Output is correct
69 Correct 118 ms 456 KB Output is correct
70 Correct 112 ms 472 KB Output is correct
71 Correct 26 ms 532 KB Output is correct
72 Correct 23 ms 512 KB Output is correct
73 Correct 23 ms 468 KB Output is correct
74 Correct 24 ms 516 KB Output is correct
75 Correct 19 ms 524 KB Output is correct
76 Correct 48 ms 768 KB Output is correct
77 Correct 46 ms 768 KB Output is correct
78 Correct 40 ms 808 KB Output is correct
79 Correct 44 ms 704 KB Output is correct
80 Correct 45 ms 740 KB Output is correct
81 Correct 110 ms 1204 KB Output is correct
82 Correct 87 ms 1356 KB Output is correct
83 Correct 108 ms 1268 KB Output is correct
84 Correct 83 ms 1248 KB Output is correct
85 Correct 97 ms 1260 KB Output is correct
86 Correct 115 ms 2108 KB Output is correct
87 Correct 113 ms 2080 KB Output is correct
88 Correct 93 ms 2144 KB Output is correct
89 Correct 101 ms 2140 KB Output is correct
90 Correct 109 ms 2316 KB Output is correct
91 Correct 220 ms 10368 KB Output is correct
92 Correct 274 ms 11724 KB Output is correct
93 Correct 223 ms 11744 KB Output is correct
94 Correct 210 ms 11708 KB Output is correct
95 Correct 232 ms 11716 KB Output is correct
96 Correct 243 ms 23760 KB Output is correct
97 Correct 281 ms 23776 KB Output is correct
98 Correct 312 ms 23808 KB Output is correct
99 Correct 248 ms 23740 KB Output is correct
100 Correct 239 ms 23856 KB Output is correct
101 Correct 263 ms 23764 KB Output is correct
102 Correct 237 ms 23768 KB Output is correct
103 Correct 233 ms 23844 KB Output is correct
104 Correct 299 ms 23840 KB Output is correct
105 Correct 234 ms 23844 KB Output is correct
106 Correct 332 ms 28576 KB Output is correct
107 Correct 271 ms 28776 KB Output is correct
108 Correct 282 ms 28540 KB Output is correct
109 Correct 305 ms 28488 KB Output is correct
110 Correct 275 ms 28616 KB Output is correct
111 Correct 334 ms 28588 KB Output is correct
112 Correct 293 ms 28720 KB Output is correct
113 Correct 297 ms 28556 KB Output is correct
114 Correct 284 ms 28604 KB Output is correct
115 Correct 277 ms 28540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 212 KB Output is correct
14 Correct 21 ms 300 KB Output is correct
15 Correct 41 ms 296 KB Output is correct
16 Correct 63 ms 276 KB Output is correct
17 Correct 52 ms 280 KB Output is correct
18 Correct 54 ms 280 KB Output is correct
19 Correct 50 ms 288 KB Output is correct
20 Correct 54 ms 296 KB Output is correct
21 Correct 51 ms 280 KB Output is correct
22 Correct 56 ms 280 KB Output is correct
23 Correct 51 ms 296 KB Output is correct
24 Correct 71 ms 280 KB Output is correct
25 Correct 63 ms 284 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 3 ms 212 KB Output is correct
29 Correct 20 ms 300 KB Output is correct
30 Correct 36 ms 276 KB Output is correct
31 Correct 52 ms 280 KB Output is correct
32 Correct 59 ms 292 KB Output is correct
33 Correct 50 ms 280 KB Output is correct
34 Correct 70 ms 280 KB Output is correct
35 Correct 60 ms 288 KB Output is correct
36 Correct 61 ms 292 KB Output is correct
37 Correct 57 ms 276 KB Output is correct
38 Correct 62 ms 296 KB Output is correct
39 Correct 54 ms 212 KB Output is correct
40 Correct 55 ms 276 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 3 ms 256 KB Output is correct
45 Correct 5 ms 340 KB Output is correct
46 Correct 4 ms 352 KB Output is correct
47 Correct 20 ms 308 KB Output is correct
48 Correct 22 ms 408 KB Output is correct
49 Correct 51 ms 424 KB Output is correct
50 Correct 96 ms 340 KB Output is correct
51 Correct 156 ms 472 KB Output is correct
52 Correct 159 ms 480 KB Output is correct
53 Correct 135 ms 476 KB Output is correct
54 Correct 148 ms 456 KB Output is correct
55 Correct 133 ms 456 KB Output is correct
56 Correct 112 ms 452 KB Output is correct
57 Correct 116 ms 480 KB Output is correct
58 Correct 100 ms 456 KB Output is correct
59 Correct 117 ms 456 KB Output is correct
60 Correct 107 ms 476 KB Output is correct
61 Correct 101 ms 456 KB Output is correct
62 Correct 116 ms 456 KB Output is correct
63 Correct 121 ms 456 KB Output is correct
64 Correct 122 ms 456 KB Output is correct
65 Correct 93 ms 460 KB Output is correct
66 Correct 110 ms 452 KB Output is correct
67 Correct 104 ms 452 KB Output is correct
68 Correct 101 ms 472 KB Output is correct
69 Correct 118 ms 456 KB Output is correct
70 Correct 112 ms 472 KB Output is correct
71 Correct 26 ms 532 KB Output is correct
72 Correct 23 ms 512 KB Output is correct
73 Correct 23 ms 468 KB Output is correct
74 Correct 24 ms 516 KB Output is correct
75 Correct 19 ms 524 KB Output is correct
76 Correct 48 ms 768 KB Output is correct
77 Correct 46 ms 768 KB Output is correct
78 Correct 40 ms 808 KB Output is correct
79 Correct 44 ms 704 KB Output is correct
80 Correct 45 ms 740 KB Output is correct
81 Correct 110 ms 1204 KB Output is correct
82 Correct 87 ms 1356 KB Output is correct
83 Correct 108 ms 1268 KB Output is correct
84 Correct 83 ms 1248 KB Output is correct
85 Correct 97 ms 1260 KB Output is correct
86 Correct 115 ms 2108 KB Output is correct
87 Correct 113 ms 2080 KB Output is correct
88 Correct 93 ms 2144 KB Output is correct
89 Correct 101 ms 2140 KB Output is correct
90 Correct 109 ms 2316 KB Output is correct
91 Correct 220 ms 10368 KB Output is correct
92 Correct 274 ms 11724 KB Output is correct
93 Correct 223 ms 11744 KB Output is correct
94 Correct 210 ms 11708 KB Output is correct
95 Correct 232 ms 11716 KB Output is correct
96 Correct 243 ms 23760 KB Output is correct
97 Correct 281 ms 23776 KB Output is correct
98 Correct 312 ms 23808 KB Output is correct
99 Correct 248 ms 23740 KB Output is correct
100 Correct 239 ms 23856 KB Output is correct
101 Correct 263 ms 23764 KB Output is correct
102 Correct 237 ms 23768 KB Output is correct
103 Correct 233 ms 23844 KB Output is correct
104 Correct 299 ms 23840 KB Output is correct
105 Correct 234 ms 23844 KB Output is correct
106 Correct 332 ms 28576 KB Output is correct
107 Correct 271 ms 28776 KB Output is correct
108 Correct 282 ms 28540 KB Output is correct
109 Correct 305 ms 28488 KB Output is correct
110 Correct 275 ms 28616 KB Output is correct
111 Correct 334 ms 28588 KB Output is correct
112 Correct 293 ms 28720 KB Output is correct
113 Correct 297 ms 28556 KB Output is correct
114 Correct 284 ms 28604 KB Output is correct
115 Correct 277 ms 28540 KB Output is correct
116 Incorrect 3 ms 340 KB Output isn't correct
117 Halted 0 ms 0 KB -