总结9
fangjiayuan · · 个人记录
T703101 小梦的V图
错误代码:
#include<bits/stdc++.h>
using namespace std;
int n, a[500005], maxxf[500005], maxxt[500005], ans;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
maxxf[i] = max(maxxf[i - 1], a[i]);
}
for(int i = n; i >= 1; i--){
maxxt[i] = max(maxxt[i + 1], a[i]);
}
for(int i = 2; i < n; i++){
if(maxxf[i - 1] > a[i] && maxxt[i + 1] > a[i]){
ans = max(ans, maxxf[i - 1] + a[i] + maxxt[i + 1]);
}
}
cout << ans;
return 0;
}
错误原因:
1,ans初始值要定为-3000000000
2,要开 long long
3,在求最小值时maxx[n]应该提前赋值为a[n]。
正确代码
#include<bits/stdc++.h>
using namespace std;
long long n, a[500005], maxxf[500005], maxxt[500005], ans = -3e9;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
maxxf[1] = a[1];
for(int i = 2; i <= n; i++){
cin >> a[i];
maxxf[i] = max(maxxf[i - 1], a[i]);
}
maxxt[n] = a[n];
for(int i = n - 1; i >= 1; i--){
maxxt[i] = max(maxxt[i + 1], a[i]);
}
for(int i = 2; i < n; i++){
if(maxxf[i - 1] > a[i] && maxxt[i + 1] > a[i]){
ans = max(ans, maxxf[i - 1] + a[i] + maxxt[i + 1]);
}
}
if(ans == -3e9){
cout << 0;
return 0;
}
cout << ans;
return 0;
}
数组乘积
错误代码
#include<bits/stdc++.h>
using namespace std;
long long n, a[100005], sum = 1, cnt;
const long long mod = 1e9 + 7;
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
if(a[i] == 0){
cnt++;
continue;
}
sum *= a[i];
}
for(int i = 1; i <= n; i++){
if(a[i] == 0){
if(cnt == 1){
cout << sum % mod << " ";
}
else{
cout << 0 << " ";
}
continue;
}
if(cnt){
cout << 0 << " ";
continue;
}
cout << (sum / a[i]) % mod << " ";
}
return 0;
}
错误原因:暴力代码
正确代码
#include<bits/stdc++.h>
using namespace std;
long long n, a[100005], maxx[100005], maxx2[100005];
const long long mod = 1e9 + 7;
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
maxx[0] = maxx2[n + 1] = 1;
maxx[1] = a[1];
maxx2[n] = a[n];
for(int i = 2; i <= n; i++){
maxx[i] = maxx[i - 1] * a[i];
maxx[i] %= mod;
}
for(int i = n - 1; i >= 1; i--){
maxx2[i] = maxx2[i + 1] * a[i];
maxx2[i] %= mod;
}
for(int i = 1; i <= n; i++){
cout << maxx[i - 1] * maxx2[i + 1] % mod << " ";
}
return 0;
}
思路:先用前缀数组预处理出1到a[i]的乘积,和后缀a[i]到n的乘积,再在每次到i时用前缀i - 1乘后缀i + 1。
矩阵乘积
错误代码
#include<bits/stdc++.h>
using namespace std;
long long n, m, a[1005][1005], sum = 1, cnt;
const long long mod = 1e9 + 7;
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> a[i][j];
if(a[i][j] == 0){
cnt++;
continue;
}
sum *= a[i][j];
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(a[i][j] == 0){
if(cnt == 1){
cout << sum % mod << " ";
}
else{
cout << 0 << " ";
}
continue;
}
if(cnt){
cout << 0 << " ";
continue;
}
cout << (sum / a[i][j]) % mod << " ";
}
cout << '\n';
}
return 0;
}
错误原因:暴力
正确代码
#include<bits/stdc++.h>
using namespace std;
long long n, a[1000005], maxx[1000005], maxx2[1000005], m;
const long long mod = 1e9 + 7;
int main(){
cin >> n >> m;
for(int i = 1; i <= n * m; i++){
cin >> a[i];
}
maxx[0] = maxx2[n * m + 1] = 1;
maxx[1] = a[1];
maxx2[n * m] = a[n * m];
for(int i = 2; i <= n * m; i++){//二维转一维
maxx[i] = maxx[i - 1] * a[i];
maxx[i] %= mod;
}
for(int i = n * m - 1; i >= 1; i--){
maxx2[i] = maxx2[i + 1] * a[i];
maxx2[i] %= mod;
}
for(int i = 1; i <= n * m; i++){//再二维输出
cout << maxx[i - 1] * maxx2[i + 1] % mod << " ";
if(i % m == 0){
cout << '\n';
}
}
return 0;
}
思路:先将二维转一维再用前缀数组预处理出1到a[i]的乘积,和后缀a[i]到n的乘积,再在每次到i时用前缀i - 1乘后缀i + 1。
T703157 三个最大子数组和
正确代码
#include<bits/stdc++.h>
using namespace std;
long long n, k, a[20005], pos1, pos2, pos3, ans;
struct P{
long long sum, id;
}pre[20005], last[20005];
//pre:预处理出[1, i]之间最大为k的区间和,i为左端点, last:预处理出[i,n]之间最大为k的区间和,i为左端点
int main(){
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i];
a[i] += a[i - 1];
}
for(int i = 1; i <= n - k + 1; i++){
long long now = a[i + k - 1] - a[i - 1];
if(now > pre[i - 1].sum){
pre[i] = {now, i};
}
else{
pre[i] = pre[i - 1];
}
}
for(int i = n - k + 1; i >= 1; i--){
long long now = a[i + k - 1] - a[i - 1];
if(now > last[i + 1].sum){
last[i] = {now, i};
}
else{
last[i] = last[i + 1];
}
}
for(int i = k + 1; i <= n - k * 2; i++){
long long all = a[i + k - 1] - a[i - 1] + pre[i - k].sum + last[i + k].sum;
if(all > ans){
ans = all;
pos1 = pre[i - k].id;
pos2 = i;
pos3 = last[i + k].id;
}
}
cout << pos1 << " " << pos2 << " " << pos3;
return 0;
}
思路:预处理出[1, i]之间最大为k的区间和和[i,n]之间最大为k的区间和, 再枚举中间数,就可以求出最大值了