JZOJ 5264. 【NOIP2017模拟8.12A组】化学
生活随笔
收集整理的這篇文章主要介紹了
JZOJ 5264. 【NOIP2017模拟8.12A组】化学
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Description
Input
Output
Sample Input
3 10
1 2 10
Sample Output
5
Data Constraint
Hint
Solution
看到數據范圍(n≤40),就不由自主地想到折半搜索。
于是把 n 先折半,每邊 20 ,設兩段的總花費分別為 x,y ,則顯然滿足:
x+y≤m前半段 O(220) 枚舉每個位置選不選(即順便算出當前的 x),
那么后半段的 y 就需要滿足:
y≤m?x那么我們可以將所有的 x 加入一個數組中,并從小到大排序。
在搜到一個 y 的時候,就在數組中二分查找,直接統計數量即可。
總時間復雜度為 O(2N+N?log?N) 。
Code
#include<cstdio> #include<algorithm> using namespace std; typedef long long LL; const int mo=1e7+7; int n,n1; LL m,ans; LL a[41],f[mo]; inline LL read() {LL X=0,w=1; char ch=0;while(ch<'0' || ch>'9') {if(ch=='-') w=-1;ch=getchar();}while(ch>='0' && ch<='9') X=(X<<3)+(X<<1)+ch-'0',ch=getchar();return X*w; } inline void dfs1(int x,LL y) {if(y>m) return;if(x>n1){f[++f[0]]=y;return;}dfs1(x+1,y+a[x]);dfs1(x+1,y); } inline void dfs2(int x,LL y) {if(y>m) return;if(x>n){int l=1,r=f[0];while(l<r){int mid=(l+r)>>1;if(f[mid]<=m-y) l=mid+1; else r=mid;}if(f[l]>m-y) l--;ans+=l;return;}dfs2(x+1,y+a[x]);dfs2(x+1,y); } int main() {n=read(),m=read(),n1=n>>1;for(int i=1;i<=n;i++) a[i]=read();dfs1(1,0);sort(f+1,f+1+f[0]);dfs2(n1+1,0);printf("%lld",ans);return 0; }總結
以上是生活随笔為你收集整理的JZOJ 5264. 【NOIP2017模拟8.12A组】化学的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JZOJ 5274. 数组
- 下一篇: JZOJ 5281. 【NOIP提高组模