codeforces 1183F 离散化枚举 约数定理

codeforces1183F 有技巧的暴力

传送门:https://codeforces.com/contest/1183/problem/F

题意:

给你n个数,要你从中选出最多三个数,使得三个数x,y,z互不相等,x,y,z之和最大是多少

题解:

n到了2e5,并且有q组数据,所以我们这里需要有技巧的枚举

因为最多只能选取三个数

我们就可以分类讨论

选取一个数 那么这个数一定是最大的那个数

选取两个数 那么这个两个数互不为约数

选取三个数和选取两个数同理

我们将数组排序离散化后,从大到小的选,根据约数定理,我们两重循环和三重循环其实并不会跑满

只会跑到很小的个数就会终止

代码:

#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********
")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]
"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]
"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]
"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
int a[maxn];
int main() {
#ifndef ONLINE_JUDGE
    FIN
#endif
    int T;
    scanf("%d", &T);
    while(T--) {
        int n;
        scanf("%d", &n);
        int ans = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            ans = max(ans, a[i]);
        }
        sort(a + 1, a + n + 1);
        n = unique(a + 1, a + 1 + n) - a - 1;
        for (int i = n; i >= 1; --i) {
            for (int j = i - 1; j >= 1; --j) {
                if(a[i] % a[j] != 0) {
                    ans = max(ans, a[i] + a[j]);
                    break;
                }
            }
        }
        for (int i = n; i >= 1; --i) {
            for (int j = i - 1; j >= 1; --j) {
                if(a[i] % a[j] != 0) {
                    for (int k = j - 1; k >= 1; --k) {
                        if(a[i] % a[k] != 0 && a[j] % a[k] != 0) {
                            ans = max(ans, a[i] + a[j] + a[k]);
                            break;
                        }
                    }
                    break;
                }
            }
        }
        printf("%d
", ans);
         
    }
    return 0;
}
每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
原文地址:https://www.cnblogs.com/buerdepepeqi/p/11177486.html