跳到正文

个人记录

2025.08.03课堂总结

huyunshen约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年8月3日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 6 分钟

版本

  1. v1当前

2025.08.03课堂总结

磁铁吗?有点意思~~

T639126 落落的去的数学问题2

本题想要AC的话是不可暴力的,数据范围写着:n<=3e5,ai<le8n<=3e5, a_i<le8,即所有数的和<3e13<3e13,没有超过longlong。应先将任意两个数的和都加在一起,记为sum,再排序,排完序后二分出有多少个数加上当前的这个数超过了10810^8,符合条件的加到cnt里,最后输出sum-cnt*10810^8。
正解:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e5 + 5;
int n, a[N], sum, cnt;
signed main(){ 
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	sort(a + 1, a + n + 1);//排序
	for(int i = 1; i <= n; i++){
		sum += a[i] * (n - 1);//任意两个数的和都加在一起
		int pos = (lower_bound(a + i + 1, a + n + 1, 100000000 - a[i]) - a);//二分找
		cnt += n - pos + 1;
	}
	cout << sum - cnt * 100000000;//答案
	return 0;
}

T638485 第K⼩

这题我都没写(雾)
老师说,用老登优先队列,在搜索的过程中将最小的数加上a数组中的每一个数并加到队列中,最后map去重。
正解:

C++
#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int N = 15;
ll n, m, ans, a[N];
map<ll, bool> mp;

ll bfs() {
    priority_queue<ll, vector<ll>, greater<ll>> pq; // 最小堆
    for (int i = 1; i <= n; i++) {
        if (mp[a[i]]) continue;
        pq.push(a[i]);
        mp[a[i]] = 1;
    }
    while (!pq.empty()) {
        ans++;
        ll t = pq.top();
        if (ans == m) return t;
        pq.pop();
        for (int i = 1; i <= n; i++) {
            if (mp[t + a[i]]) continue;
            mp[t + a[i]] = 1;
            pq.push(t + a[i]);
        }
    }
    return 0;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    cout << bfs();
    return 0;
}

U591734 磁铁迷宫

本题题就是找最大连通块,磁铁本身的位置标记为1,可以被吸住的标记2,剩余的标记为0,当发现0且没有走过就搜一次,最后输出最大的联通块即可。
正解:

C++
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 1e3 + 10;
int n, m, a[N][N]; // 0表示包括??和周围4个位置都没有磁铁 1表示磁铁 2表示??不
int ans = 1;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int vis[N][N]; // vis[i][j]=color; 第color次搜索,就将搜索过的位置赋值为color
int cnt, color;

void dfs(int x, int y) {
    for (int i = 0; i < 4; i++) {
        int xx = x + dx[i], yy = y + dy[i];
        if (xx < 1 || xx > n || yy < 1 || yy > m || vis[xx][yy] == color || a[xx][yy] == 1) continue;
        vis[xx][yy] = color;
        cnt++;
        if (a[xx][yy] == 2) continue;
        dfs(xx, yy);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            char c;
            cin >> c;
            if (c == '#') a[i][j] = 1;
            else a[i][j] = 0;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (a[i][j] == 1) continue;
            if (a[i - 1][j] == 1 || a[i + 1][j] == 1 || a[i][j - 1] == 1 || a[i][j + 1] == 1) {
                a[i][j] = 2;
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (vis[i][j] == 0 && a[i][j] == 0) { // 从完全没搜索过的地开始搜索
                color++;
                cnt = 1;
                vis[i][j] = color;
                dfs(i, j);
                ans = max(ans, cnt);
            }
        }
    }
    cout << ans;
    return 0;
}

好了,今天的总结就写到这了,感谢您的观看!😊😊😊

原文