A
#include
#include
#include
using namespace std;
int main() {
int n, m; cin >> n >> m;
vector<string> a, b, ans;
map<string, bool> aa, bb, mp;
for (int i = 0; i < n; i ++) {
string ss; cin >> ss;
if (aa[ss]) continue;
aa[ss] = 1;
a.push_back(ss);
}
for (int i = 0; i < m; i ++) {
string ss; cin >> ss;
if (bb[ss]) continue;
bb[ss] = 1;
b.push_back(ss);
}
int l = 0, r = 0;
while (l < a.size() && r < b.size()) {
if (!mp[a[l]]) {
mp[a[l]] = 1;
ans.push_back(a[l]);
}
if (!mp[b[r]]) {
mp[b[r]] = 1;
ans.push_back(b[r]);
}
l ++, r ++;
}
while (l < a.size()) {
if (!mp[a[l]]) {
mp[a[l]] = 1;
ans.push_back(a[l]);
}
l ++;
}
while (r < b.size()) {
if (!mp[b[r]]) {
mp[b[r]] = 1;
ans.push_back(b[r]);
}
r ++;
}
for (int i = 0; i < ans.size(); i ++) {
cout << ans[i];
if (i != ans.size() - 1) cout << endl;
}
return 0;
}
D
#include
#include
using namespace std;
typedef long long ll;
const int N = 1e6 + 2;
int f[N], w[N], v[N];
bool vis[N];
int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); }
ll fx(ll x) { return x * (x - 1) / 2; }
int main() {
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i ++) f[i] = i, w[i] = 1;
while (m --) {
int a, b; cin >> a >> b;
int pa = find(a), pb = find(b);
if (pa == pb) {
v[pa] ++;
continue;
}
f[pa] = pb;
w[pb] += w[pa];
v[pb] += v[pa] + 1;
}
ll res = 0;
vector<int> ans;
int cnt1 = 0;
bool ok = 0;
for (int i = 1; i <= n; i ++) {
int x = find(i);
if (vis[x]) continue;
vis[x] = 1;
ans.push_back(x);
if (w[x] == 1) cnt1 ++;
else if (v[x] != fx(w[x])) ok = 1;
}
for (auto i : ans) res += fx(w[i]) - v[i];
if (!ok) {
if (cnt1 >= 2) res ++;
else if (!cnt1) {
int a = 1e6, b = 1e6;
for (auto i : ans) {
if (w[i] < a) {
a = w[i];
if (a < b) swap(a, b);
}
}
res += fx(a + b) - fx(a) - fx(b);
} else {
ll add = 1e12;
for (auto i : ans) if (w[i] != 1) add = min(add, fx(w[i] + 1) - v[i]);
res += add;
}
}
cout << res << endl;
return 0;
}
J
#include
#include
using namespace std;
const double eps = 1e-8;
double dis2(double x1, double y1, double x2, double y2) {
return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
}
struct Point {
double x, y, r;
double x1, y1, x2, y2;
Point(double x1, double y1, double x2, double y2) {
x = (x1 + x2) / 2;
y = (y1 + y2) / 2;
r = dis2(x1, y1, x2, y2) / 2;
}
Point(int x, int y): x(x), y(y) {}
};
double ManDis(double x1, double y1, double x2, double y2) {
return abs(x1 - x2) + abs(y1 - y2);
}
double findy(Point a, Point b, double x) {
double dx = abs(x - b.x);
double dy = sqrt(b.r * b.r - dx * dx);
if (a.y < b.y) return b.y - dy;
return b.y + dy;
}
double solve() {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
Point a(x1, y1, x2, y2);
cin >> x1 >> y1 >> x2 >> y2;
Point b(x1, y1, x2, y2);
double l, r;
if (a.x < b.x) l = b.x - b.r, r = b.x;
else l = b.x, r = b.x + b.r;
while (abs(l - r) >= eps) {
double L = (2 * l + r) / 3, R = (l + r * 2) / 3;
if (ManDis(a.x, a.y, L, findy(a, b, L)) <= ManDis(a.x, a.y, R, findy(a, b, R))) r = R;
else l = L;
}
return ManDis(a.x, a.y, l, findy(a, b, l));
}
int main() {
int n; cin >> n;
while (n --) printf("%.10lf\n", solve());
return 0;
}
L
#include
using namespace std;
int main() {
int n, m; cin >> n >> m;
int res = 2;
while (n --) {
int x; cin >> x;
res = max(res, (x + m - 1) / m);
}
cout << res << endl;
return 0;
}