P1097 [NOIP 2007 提高组] 统计数字
题意:
思路:
- 有一个map集合作为桶装入每一个数字,然后用auto迭代每一个值,存储完之后输出
代码
C++#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个位置满足ax,y≥i=1∑nai,y+i=1∑max,i
思路:
- 用数组rol记录每行的值,用数组col记录每列的值
- 循环maxn+1次(maxn指矩阵的最大值,因为用了maxn次魔法后所有数字都会变成负数,每一个元素都可以,因为要从0开始所以循环maxn+1次),每一次计算有多少个元素大于等于行的和加上列的和,如果数量大于等于k就输出
代码
C++#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的子序列
思路:
- 用数组pre计算从前往后数有多少个z,用数组nex计算从后往前数有多少个y
- 循环枚举字符串a,发现z就ans加上prei−1与nexi+1
代码
C++#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;
}