跳到正文

个人记录

lzm错题总结

LiQiRong__juruo约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年8月4日
收录
2025年12月12日
更新
2026年11月19日
篇幅
约 4 分钟

版本

  1. v1当前

T1

RE……

属实没想到单调栈存3e6啊,数组开小辣!

C++
#include <bits/stdc++.h>
using namespace std;
int a[3000001];
int ans[3000001]; //数组啊,能不能放过我这一次……
int n;
stack<int>st;
int main(){
    int n;
    cin >> n;
    for (int i = 1;i <= n;i++){
        cin >> a[i];
    }
    for (int i = 1;i <= n;i++){
        while(st.size() && a[i] > a[st.top()]){
            ans[st.top()] = i;
            st.pop(); 
        }
        st.push(i);
    }
    for (int i = 1;i <= n;i++){
        cout << ans[i]<<' ';
    }
    return 0;
}

T5

朴素TLE40pts代码:

C++
#include <bits/stdc++.h>
using namespace std;
set<int>st; 
int main(){
	int n;
	cin >> n;
	for(int i = 1;i <= n;i++){
		int x;
		cin >> x;
		while(st.count(x)){
			x++;
		}
		st.insert(x);
		cout <<x<<' ';
	} 
}

这题的正解思路是先把桶数组(set)填上2e6

再通过set的lower_bound()二分查找,搜出第一个大于等于a[i]且还没删的整数,将a[i]替换成它,删除这个数

while……

C++
#include <bits/stdc++.h>
using namespace std;
set<int>st; 
int a[2000001];
int main(){
    ios::sync_with_stdio();
    cin.tie();
    cout.tie();
	int n;
	cin >> n;
    for(int i = 1;i <= n;i++){
        cin >> a[i];
    }
    for(int i = 1;i <= 2e6 + 1;i++){
        st.insert(i);
    }
	for(int i = 1;i <= n;i++){
		a[i] = *st.lower_bound(a[i]);
		st.erase(a[i]);
		cout <<a[i]<<' ';
	}
}

T6

AC代码:

C++
#include <bits/stdc++.h>
using namespace std;
int t;
int n,m;
int mx[] = {0,0,-1,-1,-1,1,1,1};
int my[] = {-1,1,-1,1,0,1,-1,0};
int vis[11][11];
int ans = -1e9;
int a[11][11];
void dfs(int x,int y,int sum){
	if(x > n){
		ans = max(sum,ans);
		return ;
	}
	int nx,ny;
	if(y == m)nx = x + 1,ny = 1;
	else nx = x,ny = y + 1;
	if(vis[x][y]){
		dfs(nx,ny,sum);
		return ;
	}
    bool f = 1;
    for(int k = 1;k <= 8;k++){
        if(vis[x + mx[k - 1]][y + my[k - 1]]){
            f = 0;
        }
    }
    if(f){
        vis[x][y] = 1;
        dfs(nx,ny,sum + a[x][y]);
        vis[x][y] = 0;
    }
	dfs(nx,ny,sum);
	return ;
}
int main(){
	cin >> t;
	while(t--){
		cin >> n>>m;
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= m;j++){
				cin >> a[i][j];
                vis[i][j] = 0;
			}
		}
		dfs(1,1,0);
		cout << ans <<'\n';
		ans = -1e9;
	}
}

这题其实跟棋盘问题差不多,只是把行和列的判断转化为了周围8个格子的判断,每个格子有选与不选两种状态,最后输出最大值便可。

(初始化也要注意,ans要变回-1e9)

原文