当前位置:网站首页>【小码匠自习室】AGC023-A :为啥总是N连发?为啥总遇到大神?
【小码匠自习室】AGC023-A :为啥总是N连发?为啥总遇到大神?
2022-08-08 13:52:00 【小码匠】
碎碎念
- 老码农惯用小伎俩,每学一个知识点,总是N连发刷题,美其名曰:要慢下来,慢下来,慢下来
- 又遇到大神的代码,大神连刷2天,就为了执行时长从15ms -> 8ms,估计对性能有洁癖
- AC执行最长时长:1807ms
- 大神时长:8ms
- 意外惊喜:tourist大神也有不会做的题目,这次比赛的E题,大神也没搞定,排名第三,真是罕见
- 看大神的代码犹如沐浴春风,望尘莫及,简洁的令人生畏。
标签
- 累积和、map
题目地址
- A - Zero-Sum Ranges
- https://atcoder.jp/contests/agc023/tasks/agc023_a
题目描述
有一个整数序列A,其长度为N。
求 A 的和为 0 的非空连续子序列的个数。注意我们是在统计取出子序列的方式。也就是说,即使有的两个子序列的内容相同,如果它们取自不同的位置,也当成不同序列.
约束条件
- 1 \leq N \leq 2 \times 10^5
- -10^9 \leq A_i \leq 10^9
- 所有输入值都是整数.
输入
输入格式如下:
N
A1 A2 ...... AN
输出
求和为 0 的 A 的非空连续子序列的个数.
输入示例 1
6
1 3 -4 2 2 -2
输出示例 1
3
There are three contiguous subsequences whose sums are 0: (1,3,-4), (-4,2,2) and (2,-2).
输入示例 2
7
1 -1 1 -1 1 -1 1
输出示例 2
12
In this case, some subsequences that have the same contents but are taken from different positions are counted individually. For example, three occurrences of (1, -1) are counted.
输入示例 3
5
1 -2 3 -4 5
输出示例 3
0
There are no contiguous subsequences whose sums are 0.
题解
小码匠题解
- 楼下是大神:tourist题解,代码的每一个细节都值得学习
void coder_solution() {
// 提升cin、cout效率
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
unordered_map<long long , long long > b;
vector<long long> a(n);
cin >> a[0];
b[0] = 0;
b[a[0]]++;
for(int i = 1; i < n; ++i) {
cin >> a[i];
a[i] += a[i - 1];
b[a[i]]++;
}
long long ans = 0;
for(int i = 0; i < n; ++i) {
ans += b[a[i]];
if(a[i] != 0) {
ans--;
}
b[a[i]]--;
}
cout << ans;
}
参考题解(tourist)
void best_solution() {
// 提升cin、cout效率
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
map<long long, int> mp;
mp[0] = 1;
long long s = 0;
long long ans = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
s += x;
ans += mp[s];
mp[s]++;
}
cout << ans << '\n';
}
参考题解
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main() {
int N; cin >> N;
vector<long long> a(N);
for (int i = 0; i < N; ++i) cin >> a[i];
// 累積和と連想配列
vector<long long> s(N+1, 0);
map<long long, long long> nums;
for (int i = 0; i < N; ++i) s[i+1] = s[i] + a[i];
for (int i = 0; i < N+1; ++i) nums[s[i]]++;
// 集計処理
long long res = 0;
for (auto it : nums) {
long long num = it.second; // it.first が it.second 個ある
res += num * (num - 1) / 2;
}
cout << res << endl;
}
又遇大神
#pragma GCC optimize ("O3")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
if (ci - owa > 0) {
memcpy(cn, owa, CL);
ci -= CM;
fread(cn + CL, 1, CM, stdin);
}
int pn = 1;
if (*ci == '-') {
pn = -pn;
ci++;
}
ll tmp = *(ll*)ci;
int dig = ((tmp & ma0) ^ ma0) ? 68 - __builtin_ctzll((tmp & ma0) ^ ma0) : 0;
tmp = tmp << dig & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += (64 - dig >> 3);
while ((ct = *ci++) >= '0') tmp = tmp * 10 + ct - '0';
return pn * tmp;
}
ll A[200001];
void pakuri_sort(int N, ll A[]) {
const int b = 8;
ll tmp[200001];
rep(k, 4) {
int kazu[1 << b] = {}, kazu2[1 << b] = {};
rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++;
rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i];
for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i];
k++;
rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++;
rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i];
for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i];
}
}
int main() {
//cin.tie(0);
//ios::sync_with_stdio(false);
int N = getint();
rep1(i, N) {
A[i] = getint() + A[i - 1];
}
pakuri_sort(N + 1, A);
ll kotae = 0;
ll mae = 1e18;
int kazu = 0;
rep(i, N + 1) {
if (mae != A[i]) {
mae = A[i];
kazu = 0;
}
kotae += kazu++;
}
printf("%lld\n", kotae);
Would you please return 0;
}
边栏推荐
- (8) FlinkSQL custom UDF
- OrderedDict构建函数模块的不常见写法
- MySQL:索引(1)原理与底层结构
- KD-SCFNet:通过知识蒸馏实现更准确、更高效的显着目标检测(ECCV2022)
- 医学图像数据增强-归一化
- R语言patchwork包将多个ggplot2可视化结果组合起来、使用plot_annotation函数以及tag_level参数为组合图添加自定义编码序列(字符向量列表)
- 【个人总结】2022.8.7周结
- qtwebapp库的编译及简单使用
- Tensorflow and Keras for machine learning, deep learning
- 【干货】交换机的接口类型完全实物了解
猜你喜欢
随机推荐
TCP补充
机器学习+深度学习笔记(持续更新~)
浅学一下二叉树链式存储结构的遍历
Qt 在循环中超时跳出
[C language] In-depth analysis of data storage in memory
Ingress:比Service更强大的服务暴露与负载均衡
Tsinghua | GLM-130B: An Open Bilingual Pre-training Model
直接选择排序
poj3744 Scout YYF I
PHP中使用XML-RPC构造Web Service简单入门
跟我一起了解云耀云服务器HECS【华为云至简致远】
Code Casual Recording Notes_Dynamic Programming_322 Change Exchange
KMP Media Group South Africa implemented a DMS (Document Management System) to digitize the process, employees can again focus on their actual tasks, providing efficiency
【电路基础2】电容
Qt操作Sqlite类封装,及命令行导入csv文件到Sqlite数据库
flink知识
R语言ggplot2可视化:使用ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、使用tab_add_hline函数为表头添加横线并自定义线条宽度
【干货】交换机的接口类型完全实物了解
Harvard University smashes the field: DALL-E 2 is just a "glue monster", and the generation accuracy rate is only 22%
华为云云数据库RDS MySQL 版初试探【华为云至简致远】









