A mid-summer night’s dream
中文題目:https://zerojudge.tw/ShowProblem?problemid=e606
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main() {
int n;
while (cin >> n){
int a[n];
for (int i = 0; i < n; i++){
cin >> a[i];
}
sort(a, a+n);
int mid1 = a[(n-1)/2];
int mid2 = a[n/2];
int ans = 0;
for (int i = 0; i < n; i++){
if (a[i] == mid1 || a[i] == mid2) ans++;
}
cout << mid1 << " " << ans << " " << mid2 - mid1 + 1 << "\n";
}
return 0;
}
Tell me the frequencies!
題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1003中文題目:https://zerojudge.tw/ShowProblem?problemid=c012
#include <iostream>
#include <algorithm>
using namespace std;
#define pii pair<int,int>
bool cmp(pii a, pii b){
if (a.first != b.first) return a.first < b.first;
else return a.second > b.second;
}
int main() {
string s;
while (getline(cin, s)) {
pii a[256];
for (int i = 0; i < 256; i++) {
a[i] = {0, i};
}
for (int i = 0; i < s.size(); i++) {
a[(int)s[i]].first++;
}
sort(a, a+256, cmp);
for (auto i: a){
if (i.first > 0) cout << i.second << " " << i.first << "\n";
}
cout << "\n";
}
return 0;
}
Train Swapping
中文題目:https://zerojudge.tw/ShowProblem?problemid=e561
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int T, N;
cin >> T;
while (T--){
cin >> N;
int a[N];
for (int i = 0; i < N; i++){
cin >> a[i];
}
int cnt = 0;
for (int i = 0; i < N-1; i++){
for (int j = i+1; j < N; j++){
if (a[i] > a[j]){
swap(a[i], a[j]);
cnt++;
}
}
}
cout << "Optimal train swapping takes " << cnt << " swaps.\n";
}
return 0;
}