周三第十四次课
David_Lu
·
2026-01-02 13:49:07
·
个人记录
P1097 [NOIP 2007 提高组] 统计数字
题意:
有一个map 集合作为桶装入每一个数字,然后用auto 迭代每一个值,存储完之后输出代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
map <int,int> m;
int n, a;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1;i <= n;i++){
cin >> a;
m[a]++;
}
for(auto i = m.begin();i != m.end();i++){
cout<< i -> first<< " "<< i -> second<< '\n';
}
return 0;
}
B3811 [语言月赛 202307] 魔法少女扶苏
题意:
有一个大小为n 行m 列的数字矩阵,可以对这个矩阵施放魔法让矩阵内部的所有数字-1 ,问至少要释放几次魔法让矩阵中有k 个位置满足a_{x, y} \geq \sum \limits _{i = 1}^n a_{i,y} + \sum \limits _{i = 1}^m a_{x,i} 思路:
用数组rol 记录每行的值,用数组col 记录每列的值
循环maxn+1 次(maxn 指矩阵的最大值,因为用了maxn 次魔法后所有数字都会变成负数,每一个元素都可以,因为要从0 开始所以循环maxn+1 次),每一次计算有多少个元素大于等于行的和加上列的和,如果数量大于等于k 就输出代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, m, k;
int a[105][105];
int rol[105], col[105];
int maxn;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n>> m>> k;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
cin >> a[i][j];
rol[i] += a[i][j];
col[j] += a[i][j];
maxn = max(maxn, a[i][j]);
}
}
for(int q = 0;q <= maxn;q++){
int cnt = 0;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
if(a[i][j] - q >= rol[i] + col[j] - q * (n + m)){
cnt++;
}
}
}
if(cnt >= k){
cout << q;
return 0;
}
}
return 0;
}
# P2646 数数zzy
## 题意:
- 给出一个字符串$a$,计算有多少个$zzy$的子序列
## 思路:
1. 用数组$pre$计算从前往后数有多少个$z$,用数组$nex$计算从后往前数有多少个$y$
2. 循环枚举字符串$a$,发现$z$就$ans$加上$pre_{i-1}$与$nex_{i+1}$
## 代码
```cpp
#include<bits/stdc++.h>
#define int long long
using namespace std;
string a;
int nex[1000005], pre[1000005];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> a;
int n = a.size();
a = '#' + a;
for(int i = 1;i <= n;i++){
pre[i] = pre[i - 1];
if(a[i] == 'z'){
pre[i]++;
}
}
for(int i = n;i >= 1;i--){
nex[i] = nex[i + 1];
if(a[i] == 'y'){
nex[i]++;
}
}
int ans = 0;
for(int i = 1;i <= n;i++){
if(a[i] == 'z'){
ans += pre[i - 1] * nex[i + 1];
}
}
cout << ans;
return 0;
}