Codeforces 731F Video Cards

题意:给定n个数字,你可以从中选出一个数A(不能对该数进行修改操作),并对其它数减小至该数的倍数,统计总和。问总和最大是多少?

题解:排序后枚举每个数作为选出的数A,再枚举其他数, sum += a[i]-a[i]%A;复杂度为O(n^2), 爆炸。

   显然应该降至O(nlogn). 枚举每个数a[i]做为A,再枚举A的倍数j即可, sum += j*(lower_bound(a, a+n, j+a[i])-lower_bound(a, a+n, j));

    那么此时复杂度为O(nlognlogn).

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <vector>
 6 #define ll long long
 7 using namespace std;
 8 const int N = 2e5+10;
 9 ll a[N];
10 bool vis[N];
11 int main(){
12     int n; cin >> n;
13     for(int i = 0; i < n; i++) cin >> a[i];
14     sort(a, a+n);
15     ll ans = a[n-1];
16     for(int i = 0; i < n; i++) if(!vis[ a[i] ]){
17         ll tmp = 0;
18         for(ll j = a[i]; j <= a[n-1]; j += a[i])
19             vis[j] = true,
20             tmp += j*(lower_bound(a, a+n, j+a[i])-lower_bound(a, a+n, j));
21         if(tmp > ans) ans = tmp;
22     }
23     cout << ans << endl;
24     return 0;
25 }
原文地址:https://www.cnblogs.com/dirge/p/5967749.html