ABC330 A-E 题解
1.ABC330 A-E 题解
2.P1967 [NOIP2013 提高组] 货车运输 题解ABC330题解
AtCoder Beginner Contest 330
A - Counting Passes
思路:
枚举一遍,当前数大于
代码:
#include<iostream> #define int long long using namespace std; int n, l, ans; int x; signed main() { cin >> n >> l; for(int i=1; i<=n; i++) { cin >> x; if(x >= l) { ans ++; } } cout << ans; return 0; }
B - Minimize Abs 1
思路:
枚举一遍,当前数在
代码:
#include<iostream> #define int long long using namespace std; int n, l, r; int x; signed main() { cin >> n >> l >> r; for(int i=1; i<=n; i++) { cin >> x; if(x <= l) { cout << l << " "; continue; } if(x >= r) { cout << r << " "; continue; } cout << x << " "; } return 0; }
C - Minimize Abs 2
思路:
枚举
其中
为当前的 下剩余 的大小, 为当前的第二个数
特判:当
代码:
#include<iostream> #include<cmath> #define int long long using namespace std; int abss(int x) { return x > -x ? x : -x; } int d, ans = 1e18; int t, l, r, mid; signed main() { cin >> d; for(int i=0; i*i<d; i++) { t = d - i*i; l = 1; r = sqrt(t) + 1; while(l <= r) { mid = (l + r) >> 1; if(mid * mid < t) { l = mid + 1; } else if(mid * mid > t) { r = mid - 1; } else { cout << 0; return 0; } ans = min(ans, abss(t - mid * mid)); } } cout << ans; return 0; }
D - Counting Ls
思路:
由于元组的无序性,仅顺序不同的元组会被视为同一个元组,所以我们只统计每个
只统计第2、3点在第1点右方、下方的方案,忽略左方、上方的点
设:
则第
-
当第二个点位于第
列上,第三个点位于第 行上时:
第二个点的方案数 第三个点的方案数即
(col[j] - 1) * c[i][j+1]
-
当第二个点位于
,第三个点位于第 列上时:
每个第二个点所对应的第三个点的方案数总和即
b[i][j+1]
将它们加起来,最后输出即可
代码:
#include<iostream> #define int long long using namespace std; int n, ans; bool a[2010][2010]; int row[2010], col[2010]; int b[2010][2010]; //第i行第j列每列的o后缀和 int c[2010][2010]; //第i行第j列的o后缀和 signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; char ch; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { cin >> ch; a[i][j] = (ch == 'o'); row[i] += a[i][j]; } } for(int j=1; j<=n; j++) { for(int i=1; i<=n; i++) { col[j] += a[i][j]; } } for(int i=1; i<=n; i++) { for(int j=n; j>=1; j--) { b[i][j] = b[i][j+1]; c[i][j] = c[i][j+1]; if(a[i][j]) { b[i][j] += col[j] - 1; c[i][j] += 1; } } } for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(a[i][j]) { ans += (col[j] - 1) * c[i][j+1]; ans += b[i][j+1]; } } } cout << ans; return 0; }
E - Mex and Update
思路:
因为
所以
有序数组,无需插入,删除,
在输入时
cnt[y]--;
,如果cnt[y] < 0
,那么s.erase(y);
cnt[a[x]]++;
,如果cnt[a[x]] >= 0
,那么s.insert(a[x]);
a[x]=y;
再输出*s.begin()
即可
代码:
#include<iostream> #include<set> #define int long long using namespace std; const int N = 200010; int n, m; int cnt[N], a[N]; set<int> s; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i=0; i<=n; i++) { s.insert(i); } for(int i=1; i<=n; i++) { cin >> a[i]; if(a[i] > N-10) { continue; } cnt[a[i]]--; s.erase(a[i]); } while (m--) { int x, y; cin >> x >> y; if(y <= 2e5) { cnt[y]--; if(cnt[y] < 0) { s.erase(y); } } if(a[x] <= 2e5) { cnt[a[x]]++; if(cnt[a[x]] >= 0) { s.insert(a[x]); } } a[x] = y; cout << *s.begin() << '\n'; } return 0; }
本文作者:gctiruct
本文链接:https://www.cnblogs.com/gctiruct/p/17858097.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步