이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
}
컴파일 시 표준 에러 (stderr) 메시지
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 |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |