8.3

· · 个人记录

T639126 落落的去的数学问题2

错误代码:直接暴力

#include<bits/stdc++.h>
using namespace std;
int n;
long long a[300004],s[300005],sum;
int main(){
    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++){
            sum += (a[i] + a[j]) % 100000000;
        }
    }
    cout << sum;
    return 0;
}

正解:\ 分析:先看数据范围max(na[i])<=1e13 可以long long存储\ 因为任意两个数都要相加, 就先把所有的两个数都加上,用一层循环搞定:sum+= a[i] (n - 1);\ 求出了所有的两个数加上的和后, 可以直接用上面的代码中的sum-多余的部分\ 多余部分怎么求:由于a[i] < 1e8,a[i] + a[j] < 1e8 ,所以余数一定是a[i] + a[j] - 1e8\ 又因为是任意两个数的和, 所以与顺序无关, sort排序即可\ 再使用二分优化,对于每个a[i],二分查找有多少大于等于1e8的数字的数量,cnt+=这个数量

#include<bits/stdc++.h>
using namespace std;
long long n;
long long a[300004],s[300005],sum,cnt,m = 100000000;
int 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);
        long long pos = (lower_bound(a + 1 + i,a + n + 1,m - a[i]) - a);//>=1e8-a[i]的数字数量
        cnt += n-pos+1;
    }
    cout << sum - cnt * m;//cnt表示一共有cnt对a[i] + a[j]超过了1e8,sum已经算进去了a[i] + a[j],但是只要算余数,就把1e8减掉 
    return 0;
}

T638485 第K小

分析:\ 先想暴力:第1小的数字k1, 是a数组中最小的\ 第2小的数字k2,先让k1和a数组中其他的数字相加, 在从产生的所有数字中找出最小的就是k2\ 第3小的数字k3, 先让k2和a数组中其他的数字相加, 在从产生的所有数字中找出最小的就是k3

 #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;//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.size())
    {
        ans ++;
        ll t = pq.top();//队首是目前最小的数
        if (ans == m)return t;//如果是第k小的数
        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()
 {
    cin >> n >> m;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    cout << bfs();
    return 0;
 }

U591734 磁铁迷宫 定义1个int数组:\ a[i][j] == 1本身是磁铁\ a[i][j] == 2周围有磁铁\ a[i][j] == 0本身不是磁铁且周围没有磁铁\ 再定义一个vis[i][j] = color,将搜索过的位置赋值为color\ 然后搜索就是一个连通块问题了

#include<bits/stdc++.h>
using namespace std;
int h,w; 
int a[1005][1005];
int vis[1004][1005];
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,1,-1};
int max1 = 1,step = 1; //max1 = 1是为了考虑a数组全是2的情况
struct node{
    int x,y;
};
int color,cnt;
void dfs(int x,int y){



    for(int i = 0;i < 4;i++){
        int xx = x + dx[i];
        int yy = y + dy[i];
        if(xx < 1 or xx > h or yy < 1 or yy > w or vis[xx][yy] == color or a[xx][yy] == 1) continue;

        vis[xx][yy] = color;

        cnt++;
        if(a[xx][yy] == 0) dfs(xx,yy);//不会重复访问2
    }

}
int main(){
    cin >> h >> w;
    for(int i = 1;i <= h;i++){
        for(int j = 1;j <= w;j++){
            char x;
            cin >> x;
            if(x == '#'){

                a[i][j] = 1;
            }else a[i][j] = 0;

        }
    }
    for(int i = 1;i <= h;i++){
        for(int j = 1;j <= w;j++){
            if(a[i][j] == 1) continue;
            if(a[i][j + 1] == 1 or a[i][j - 1] == 1 or a[i + 1][j] == 1 or a[i - 1][j] == 1) a[i][j] = 2; 
        }
    }
    for(int i = 1;i <= h;i++){
        for(int j = 1;j <= w;j++){
            //memset(vis,0,sizeof(vis));
            if(vis[i][j] == 0 and a[i][j] == 0){
                color++;
                cnt = 1;
                vis[i][j] = color;
                dfs(i,j);

                max1 = max(cnt,max1);

            }
        }
    } 
    cout << max1;
    return 0;
}