D. Arpa and a list of numbers Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017)

http://codeforces.com/contest/851/problem/D

 

分区间操作

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <time.h>
 6 #include <string>
 7 #include <set>
 8 #include <map>
 9 #include <list>
10 #include <stack>
11 #include <queue>
12 #include <vector>
13 #include <bitset>
14 #include <ext/rope>
15 #include <algorithm>
16 #include <iostream>
17 using namespace std;
18 #define ll long long
19 #define minv 1e-6
20 #define inf 1e18
21 #define pi 3.1415926536
22 #define E  2.7182818284
23 const ll mod=1e9+7;//998244353
24 const int maxn=2e6+10;
25 
26 bool vis[maxn]={0};
27 ll tot[maxn]={0},sum[maxn]={0};
28 
29 int main()
30 {
31     int n,x,y,z,i,j,pos;
32     ll v,r=inf,a;
33     scanf("%d%d%d",&n,&x,&y);
34     z=x/y;
35     for (i=1;i<=n;i++)
36     {
37         scanf("%lld",&a);
38         tot[a]++;
39         sum[a]+=a;
40     }
41     for (i=1;i<=2e6;i++)
42         tot[i]+=tot[i-1],sum[i]+=sum[i-1];
43     for (i=2;i<=1e6;i++)
44         if (!vis[i])
45         {
46             v=0;
47             //(j-i,j)
48             for (j=i;j<=2e6;j+=i)
49             {
50                 pos=max(j-z,j-i+1);//[pos,j]
51                 v+= (ll)(tot[pos-1]-tot[j-i])*x + ( (ll)(tot[j]-tot[pos-1])*j-(ll)(sum[j]-sum[pos-1]) )*y;
52                 vis[j]=1;
53             }
54             r=min(r,v);
55         }
56     cout<<r;
57     return 0;
58 }

 

posted @ 2018-08-22 22:46  congmingyige  阅读(172)  评论(0编辑  收藏  举报