A - Second Half Sum
O(N)
人数/2から合計を計算しましょう。
C++
for(int i=n/2; i<n; i++){
ans += a[i];
}
C++
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }
int main() {
int n;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a[i];
int ans = 0;
for(int i=n/2; i<n; i++){
ans += a[i];
}
cout << ans << endl;
return 0;
}
B - Old Maid
O(不明)
1~Nまでループする。
iの要素を同じ要素がないか判定して、存在していたら2つの要素を削除する。
存在していないなら答えの変数に加算する。
C++
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }
int main() {
int n;
cin >> n;
map<int, int> mp;
rep(i, n){
int a;
cin >> a;
mp[a]++;
if(mp[a]>=2) mp[a]-=2;
}
int ans = 0;
for(auto [k, v]:mp){
ans += k * v;
}
cout << ans << endl;
return 0;
}
コンテスト中のコードでも良いですが、別のコーディングを考えます。
C++
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }
int main() {
int n;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a[i];
int ans = 0;
while(a.size()){
int v = a.back();
a.pop_back();
int nn = a.size();
for(int i=0; i<nn; i++){
if(v == a[i]){
a.erase(a.begin() + i);
break;
}
}
if(nn == a.size()){
ans += v;
}
}
cout << ans << endl;
return 0;
}
公式解説のこのコーディング方法が本回答だと思います。
C - Change Schools
O(NlogN)
コンテスト中は、なぜか二分探索で回答しました。
所属するクラスの人数の配列の要素_i+1より大きい要素が存在するかを判定しました。
C++
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }
int main() {
int n, k;
cin >> n >> k;
vector<int> a(k+1);
rep(i, n){
int aa;
cin >> aa;
a[aa]++;
}
multiset<int> mst;
for(int i=1; i<=k; i++){
mst.insert(a[i]);
}
int ans = 0;
for(int i=1; i<=k; i++){
if(mst.upper_bound(a[i]+1) == mst.end()) ans++;
}
cout << ans << endl;
return 0;
}
二分探索が余計でしたね。
O(N)
人数が最大のクラスの人数-1をmとする。
m-1以上の人数のクラスがいくつあるか探索して答えを出す。
C++
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }
int main() {
int n, k;
cin >> n >> k;
vector<int> a(k+1);
rep(i, n){
int aa;
cin >> aa;
a[aa]++;
}
int m = 0;
for(int i=1; i<=k; i++){
m = max(m, a[i]);
}
int ans = 0;
for(int i=1; i<=k; i++){
if(a[i] >= m-1) ans++;
}
cout << ans << endl;
return 0;
}