8.3总结

· · 闲话

T1 T639126 落落的去的数学问题2

好吧先解释一下这个公式:

\sum_{i=1}^{N-1}\sum_{j=i+1}^N f(A_i, A_j)

两者嵌套的意思就是

for(int i=1;i<n;i++){
    for(int j=i+1;j<=n;j++){
        ans+=(a[i]+a[j])%mod;
    }
}

所以就有了暴力解 ( 此为赛时代码 ) :

#include <bits/stdc++.h>
typedef long long ll;
const int N=3e5+10;
const int mod=1e8;
using namespace std;
ll n,a[N],ans;
int main(){
    ios::sync_with_stdio(0);
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<n;i++){
        for(int j=i+1;j<=n;j++){
            ans+=(a[i]+a[j])%mod;
        }
    }
    cout<<ans;
    return 0;
}

但是会超时,不妨想一下怎么优化 。

先看数据范围 : n<=3e5 , a_i<1e8 所有数的和 <3e13 ,没有超过 long long 。

因为任意两个数都要相加,就先把所有的 2 个数都加上,一层循环搞定 , 然后 sum- 多余的部分 。

但是多余的部分是怎么求的?

因为 a_i<1e8 , a_i+a_j<2e8 , 所以余数一定是 a_i+a_j-1e8 。

减掉就行啦 \sim

于是就有了以下代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 3e5 + 5;
ll n, a[N], sum, cnt;
int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//cout优化
    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];//加上
        int pos = lower_bound(a+ i + 1, a+1+n, 100000000 - a[i]) - a;//二分
        int now = n - pos + 1;
        cnt += now;
    }
    cout << sum * (n-1) - cnt * 100000000;//输出
  return 0;
}

T2 T638485 第K小

这里需要用最小堆来做

赛时代码 0 分就不讲了哈 。

思路:

  • 输入,并将所有数入堆,注意去重 map<ll,bool> mp;
  • 不断从队列中取出当前最小的数,然后加上 a 数组中的数
  • \text{while}$ $($ $\text{q}$ . $\text{size}$ $()$ $)
    • 如果是第 k小的数,输出,并 \text{return 0} 。
    • 枚举 a 数组中的数,将队首和 a 数组入队,注意去重 。

代码实现:

#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);//cout优化
    cin >> n >> m;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    cout << bfs();//输出
    return 0;
}

T3 U591734 磁铁迷宫

深搜广搜都能做 。

问 : 从某一个点出发能访问到的最多的点的数量 ?

求最大的连通块的大小 。

其实很简单,就是要注意细节。

赛时代码就是骗分的。

连通块就不用我讲了吧?

直接上代码:

#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];
int ans = 1;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};//方向数组
int vis[N][N];
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;//是2就跳过,因为动不了
        dfs(xx, yy);
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);//cout优化
    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;
}
\Huge Bye\sim