Codeforces Round #537 (Div. 2)C Creative Snap (分治)
題目連結:http://codeforces.com/contest/1111/problem/C
#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<ll,ll>
#define mk(x,y) make_pair(x,y)
const int maxn=1e5+8;
const int ub=1e6;
ll A,B;
int n,k;
ll h[maxn];
ll tmp[31];
/*
題目意思不再描述,感覺如果不是空間限制就是個線段樹的題目,
但是空間開不了這麼大,一種做法是動態開點,
還有一種就是分治處理,分治的樣子和線段樹差不多,
詳見程式碼。
*/
ll f(int nn,int s,int t,ll add){
if(nn==0){
int pos1=lower_bound(h,h+k,h[s])-h;
int pos2=upper_bound(h,h+k,h[s])-h;
return 1LL*(pos2-pos1)*B;
}
int pos=upper_bound(h+s,h+t,tmp[nn-1]+add)-h-s;///
///while(pos+1<t&&h[pos]==h[pos+1]) pos++;
if(pos==t-s){
ll val=f(nn-1,s,t,add)+A;
val=min(val,1LL*(t-s)*B*tmp[nn]);
return val;
}else if(pos==0){
ll val=f(nn-1,s,t,add+tmp[nn-1])+A;
val=min(val,1LL*(t-s)*B*tmp[nn]);
return val;
}
else return min(1LL*(t-s)*B*tmp[nn],f(nn-1,s,s+pos,add)+f(nn-1,s+pos,t,add+tmp[nn-1]));
}
int main(){
mst(h,0);
tmp[0]=1LL;rep(i,1,31) tmp[i]=tmp[i-1]*2LL;
cin>>n>>k>>A>>B;
rep(i,0,k) cin>>h[i];
sort(h,h+k);///
if(k==0) cout<<A<<'\n';
else cout<<f(n,0,k,0LL)<<'\n';
return 0;
}
相關文章
- Codeforces Round #537 (Div. 2)解題報告
- Codeforces Round 972 (Div. 2) C
- Codeforces Round 973 (Div. 2) C
- Codeforces Round 982 (Div. 2)(A~C)
- Codeforces Round 986 (Div. 2) A-C
- Codeforces Round #537 (Div. 2)B. Average Superhero Gang Power (貪心+暴力)
- Codeforces Round 977 (Div. 2)(B-C2)
- 【Codeforces Round #437 (Div. 2) C】 Ordering Pizza
- Codeforces Round #541 (Div. 2)
- Codeforces Round 940 (Div. 2)
- Codeforces Round 934 (Div. 2)
- Codeforces Round 932 (Div. 2)
- Codeforces Round 948 (Div. 2)
- Codeforces Round #639 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round 987 (Div. 2)
- Codeforces Round 976 (Div. 2)
- Codeforces Round 975 (Div. 2)
- Codeforces Round 986 (Div. 2)
- Codeforces Round 973 (Div. 2)
- Codeforces Round 969 (Div. 2)
- Codeforces Round 873 (Div. 2)
- Codeforces Round 967 (Div. 2)
- Codeforces Round 965 (Div. 2)
- Codeforces Round 963 (Div. 2)
- Codeforces Round 979 (Div. 2)
- Codeforces Round 972 (Div. 2)
- Codeforces Round 949 (Div. 2)
- Codeforces Round 955 (Div. 2)
- Codeforces Round 951 (Div. 2)
- Codeforces Round 961 (Div. 2)
- Codeforces Round 958 (Div. 2)
- Codeforces Round 960 (Div. 2)
- Codeforces Round 953 (Div. 2)
- Codeforces Round 982 (Div. 2)