ABC442补题记录
前言
15分钟就把前四题做完了,一路势如破竹,以为这次一定能切ABCDEF。结果遇见E就卡住了。F题没看,但听说很简单。应该先看一下F才对。
C题
首先记录跟第
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 7;
int n, m, p[N];
signed main() {
ios :: sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= m; i ++) {
int a, b;
cin >> a >> b;
p[a] ++, p[b] ++;
}
for(int i = 1; i <= n; i ++) {
int x = n - p[i] - 1;
if(x < 3) cout << 0;
else cout << (x - 2) * (x - 1) * x / 6;
cout << " ";
}
return 0;
}
D题
赛场上下意识使用树状数组,但其实前缀和数组就能搞定。我们设前缀和数组为
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
typedef long long LL;
LL a[N], S[N];
int n, m;
int main() {
ios :: sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i ++) {
cin >> a[i];
S[i] = S[i - 1] + a[i];
}
while(m --) {
int op, x, l, r;
cin >> op;
if(op == 1) {
cin >> x;
S[x] += a[x + 1] - a[x];
swap(a[x], a[x + 1]);
} else {
cin >> l >> r;
cout << S[r] - S[l - 1] << endl;
}
}
return 0;
}
E题
开始上重头戏了。
这题很容易看出来是极角排序。但比较麻烦的就是一个处理共线的问题。解决方案就是将
在处理完之后,我们将所有点进行极角排序。极角排序就是先按象限一四三二的顺序为第一关键字,叉积为第二关键字(即 两个同一象限的点
进行询问时,记与
代码极其冗长。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 7;
int n, m, posl[N], posr[N];
map<pair<int, int>, int> mp;
LL gcd(LL a, LL b) {
return b == 0 ? a : gcd(b, a % b);
}
struct node {
LL x, y;
int d, id;
}p[N];
bool operator < (const node &a, const node &b) {
if(a.d != b.d) return a.d < b.d;
return a.x * b.y < a.y * b.x;
}
int main() {
ios :: sync_with_stdio(false);
cin.tie(0), cout.tie(0);
mp[{1, 1}] = 1; mp[{1, 0}] = 2; mp[{0, 0}] = 3; mp[{0, 1}] = 4;
cin >> n >> m;
for(int i = 1; i <= n; i ++) {
p[i].id = i;
cin >> p[i].x >> p[i].y;
int px = p[i].x >= 0 ? 1 : 0, py = p[i].y >= 0 ? 1 : 0;
p[i].d = mp[{px, py}];
if(p[i].x == 0 || p[i].y == 0) {
if(p[i].x == 0) p[i].y = 1 * (p[i].y > 0 ? 1 : -1);
if(p[i].y == 0) p[i].x = 1 * (p[i].x > 0 ? 1 : -1);
continue;
}
LL a = gcd(abs(p[i].x), abs(p[i].y));
p[i].x /= a, p[i].y /= a;
}
sort(p + 1, p + 1 + n);
pair<LL, LL> tmp, tmp1;
for(int l = 1; l <= n; ) {
tmp = {p[l].x, p[l].y};
int r;
for(r = l; r <= n; r ++) {
tmp1 = {p[r].x, p[r].y};
if(tmp != tmp1) break;
}
for(int i = l; i < r; i ++)
posl[p[i].id] = l, posr[p[i].id] = r - 1;
l = r;
}
while(m --) {
int a, b;
cin >> a >> b;
if(posl[a] <= posl[b]) cout << posr[b] - posl[a] + 1;
else cout << n - posl[a] + posr[b] + 1;
cout << endl;
}
return 0;
}
F题
赛后自己一发过,有点可惜。
将题目的意思转化一下。有一个
我们设
让我来解释一下这个状态转移方程。首先,前面的
朴素求
#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
int n, S[N][N], f[N][N];
int main() {
ios :: sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++) {
char c;
cin >> c;
if(c == '.') S[i][j] ++;
}
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++)
S[i][j] += S[i - 1][j];
for(int j = 1; j <= n; j ++) {
int mn = 1e9;
for(int i = n; i >= 0; i --) {
mn = min(mn, f[i][j - 1]);
f[i][j] = mn + i + S[n][j] - 2 * S[i][j];
}
}
int ret = 1e9;
for(int i = 0; i <= n; i ++) ret = min(ret, f[i][n]);
cout << ret;
return 0;
}