atcoder beginner contest 255
A - You should output ARC, though this is ABC.
代码:
#include <bits/stdc++.h>
#define int long long
int _ = 0, Case = 1;
using namespace std;
#define all(v) begin(v),end(v)
#define nline '\n'
int a[3][3];
void solve(int Case) {
int x, y;
cin >> x >> y;
cin >> a[1][1] >> a[1][2] >> a[2][1] >> a[2][2];
cout << a[x][y] << nline;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
// for (cin>>_, Case = 1; Case <= _; Case++)
solve(Case);
return 0;
}
B - Light It Up
思路:
暴力二分check
代码:
#include <bits/stdc++.h>
#define int long long
int _ = 0, Case = 1;
using namespace std;
#define all(v) begin(v),end(v)
#define nline '\n'
const int N = 1010;
using PII = pair<int, int> ;
PII p[N];
int a[N];
bool vis[N];
int n, k;
double dist(int x1, int x2, int y1, int y2) {
double d1 = (x1 - x2);
double d2 = (y1 - y2);
d1 *= d1;
d2 *= d2;
return sqrt(d1 + d2);
}
bool check(double mid) {
for (int i = 1; i <= n; i++) vis[i] = 0;
for (int i = 1; i <= k; i++) {
vis[a[i]] = 1;
}
for (int i = 1; i <= k; i++) {
int pos = a[i];
vis[pos] = 1;
auto [x, y] = p[pos];
//cout << x << ' ' << y << nline;
for (int j = 1; j <= n; j++) {
auto [x1, y1] = p[j];
if (!vis[j] and dist(x, x1, y, y1) <= mid) {
vis[j] = true;
}
}
}
for (int i = 1; i <= n; i++) if (!vis[i]) return false;
return true;
}
void solve(int Case) {
cin >> n >> k;
for (int i = 1; i <= k; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
auto &[x, y] = p[i];
cin >> x >> y;
}
double l = 0, r = 1e9;
for (int i = 0; i < 100; i++) {
double mid = (l + r) / 2 ;
if (check(mid)) r = mid;
else l = mid;
}
printf("%lf\n", r);
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
// for (cin>>_, Case = 1; Case <= _; Case++)
solve(Case);
return 0;
}
C - ±1 Operation 1
思路:
计算出与他相邻的两项即可
代码:
#include <bits/stdc++.h>
#define int long long
int _ = 0, Case = 1;
using namespace std;
#define all(v) begin(v),end(v)
#define nline '\n'
void solve(int Case) {
int x, a, d, n;
cin >> x >> a >> d >> n;
int c = x - a;
if (d == 0) {
cout << abs(x - a) << nline;
return;
}
int t = abs(c / d);
if (t >= n - 1) {
int t1 = a + d * (n - 1);
cout << min(abs(x - a), abs(x - t1)) << nline;
} else {
int y = a + d * t;
t++;
int y1 = a + d * t;
cout << min(abs(x - y), abs(x - y1)) << nline;
}
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
// for (cin>>_, Case = 1; Case <= _; Case++)
solve(Case);
return 0;
}
D - ±1 Operation 2
思路:
排序,然后预处理出两个数组,表示先i个数字都变成第i个数字所需要的最少次数,和后n-i+1个数字变成第i个数字的最少次数,然后二分
代码:
#include <bits/stdc++.h>
#define int long long
int _ = 0, Case = 1;
using namespace std;
#define all(v) begin(v),end(v)
#define nline '\n'
const int N = 1000010;
int a[N];
int d[N], d1[N];
void solve(int Case) {
int n;
cin >> n;
int q;
cin >> q;
int sum = 0;
for (int i = 1; i <= n; i++) cin >> a[i], sum += a[i];
sort(a + 1, a + 1 + n);
d[1] = 0;
for (int i = 2; i <= n + 1; i++) {
int x = a[i] - a[i - 1];
d[i] = d[i - 1] + x * (i - 1);
}
d1[n] = 0;
for (int i = n - 1; i >= 1; i--) {
int x = a[i + 1] - a[i];
d1[i] = d1[i + 1] + x * (n - i );
}
for (; q--;) {
int x;
cin >> x;
int t = upper_bound(a + 1 , a + 1 + n, x) - a - 1;
if (a[1] > x) {
cout << (a[1] - x)*n + d1[1] << nline;
} else if (a[n] < x) {
cout << (x - a[n])*n + d[n] << nline;
} else {
int t1 = (x - a[t]) * t + d[t];
t++;
int t2 = d1[t] + (a[t] - x) * (n - t + 1);
cout << t1 + t2 << nline;
}
}
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
// for (cin>>_, Case = 1; Case <= _; Case++)
solve(Case);
return 0;
}
E - Lucky Numbers
思路:
首先可以观察到只要数组中的一个元素确实那么其他元素都可以确定,将当前数组变成另一个好的数组,奇数项和偶数项加的差值互为相反数,
可以n*m枚举,然后可以得到开头元素的大小,表示开头为x的这个序列的贡献
代码:
#include <bits/stdc++.h>
#define int long long
int _ = 0, Case = 1;
using namespace std;
#define all(v) begin(v),end(v)
#define nline '\n'
const int N = 1000010;
int a[N], s[N], t[N];
void solve(int Case) {
int n, m;
cin >> n >> m;
for (int i = 1; i < n; i++) cin >> s[i];
for (int i = 1; i <= m; i++) cin >> t[i];
for (int i = 2; i <= n; i++) {
a[i] = s[i - 1] - a[i-1];
}
// for (int i = 1; i <= n; i++) cout << a[i] << ' ';
//cout << nline;
map<int, int> mp;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int x = a[i] - t[j];
if (i & 1) mp[x]++;
else mp[-x]++;
}
}
int res = 0;
for (auto[x, y] : mp) {
res = max(res, y);
}
cout << res << nline;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
// for (cin>>_, Case = 1; Case <= _; Case++)
solve(Case);
return 0;
}
F - Pre-order and In-order
经典题
代码:
#include <bits/stdc++.h>
#define int long long
int _ = 0, Case = 1;
using namespace std;
#define all(v) begin(v),end(v)
#define nline '\n'
const int N = 200010;
int a[N], b[N];
int l[N], r[N], p[N];
bool ok = true;
int build(int pl, int pr, int il, int ir) {
if (pl > pr) return 0;
int root = a[pl];
int k = p[root];
if (k > ir or k < il) {
ok = false;
return 0;
}
int len = k - il;
l[root] = build(pl + 1, pl + len, il, k - 1);
r[root] = build(pl + len + 1, pr, k + 1, ir);
return root;
}
int n;
void solve(int Case) {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i], p[b[i]] = i;
int root = build(1, n, 1, n);
if (ok and root == 1) {
for (int i = 1; i <= n; i++) {
cout << l[i] << ' ' << r[i] << nline;
}
} else cout << -1 << nline;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
// for (cin>>_, Case = 1; Case <= _; Case++)
solve(Case);
return 0;
}