アルゴリズム道中記~20240407~
A Penalty Kick
#include <bits/stdc++.h>
#define print(x) cout << x << endl;
#define rep(i, s, n) for (long long i = s; i < (long long)(n); i++)
using namespace std;
using ll = long long;
int main() {
ll N;
cin >> N;
rep (i, 1, N+1)
{
if (i % 3) cout << "o";
else cout << "x";
}
cout << endl;
}
B Farthest Point
#include <bits/stdc++.h>
#define print(x) cout << x << endl;
#define rep(i, s, n) for (long long i = s; i < (long long)(n); i++)
using namespace std;
using ll = long long;
int main() {
ll N;
cin >> N;
vector<pair<ll, ll>> pos(N);
rep (i, 0, N)
cin >> pos[i].first >> pos[i].second;
rep (i, 0, N)
{
ll maxDistance = 0, maxIndex = 0;
rep (k, 0, N)
{
ll dx = pos[i].first - pos[k].first;
ll dy = pos[i].second - pos[k].second;
ll dis = dx*dx + dy*dy;
if (maxDistance < dis)
{
maxDistance = dis;
maxIndex = k+1;
}
else if (maxDistance == dis)
{
if (maxIndex > k+1)
maxIndex = k+1;
}
}
print(maxIndex);
}
}
C Colorful Beans
#include <bits/stdc++.h>
#define print(x) cout << x << endl;
#define rep(i, s, n) for (long long i = s; i < (long long)(n); i++)
using namespace std;
using ll = long long;
int main() {
ll N;
cin >> N;
map<ll, ll> m;
rep (i, 0, N)
{
ll A, C;
cin >> A >> C;
if (m.count(C))
m[C] = min(m[C], A);
else
m[C] = A;
}
ll ans = 0;
for (const auto &[k, v] : m)
ans = max(ans, m[k]);
print(ans);
}
mapのkeyを取得する方法を知らず、少々戸惑いました。
for (const auto &[k, v] : m)
と記述することで、keyと値を取り出すことができました。
参考
この記事が気に入ったらサポートをしてみませんか?