[bzoj4800][Ceoi2015]Ice Hockey World Championship

来自FallDream的博客,未经允许,请勿转载,谢谢。



有n个物品,m块钱,给定每个物品的价格,求买物品的方案数

n<=40 m<=10^18

考虑双向宽搜,然后得到两个大小为2^20的数组,排序之后两个指针推一推计算答案即可。

排序最好用基数排序

#include<iostream>
#include<cstdio>
#include<cstring>
#define MN 40
#define MM 1050000
#define N 32767
#define ll unsigned long long
using namespace std;
inline ll read()
{
    ll x = 0 , f = 1; char ch = getchar();
    while(ch < ‘0‘ || ch > ‘9‘){ if(ch == ‘-‘) f = -1;  ch = getchar();}
    while(ch >= ‘0‘ && ch <= ‘9‘){x = x * 10 + ch - ‘0‘;ch = getchar();}
    return x * f;
}

int n,lim,tot1=0,tot2=0,v[4][N+5];
ll a[MN+5],s[MM+5],s2[MM+5],m,sa[MM+5],ans=0;

void Dfs2(int x,ll y)
{
    if(x==lim){s2[++tot2]=y;return;}
    if(y+a[x]<=m) Dfs2(x-1,y+a[x]);
    Dfs2(x-1,y);
}

void Dfs1(int x,ll y)
{
    if(x>lim){s[++tot1]=y;return;}
    if(y+a[x]<=m)Dfs1(x+1,y+a[x]);
    Dfs1(x+1,y);
}

void Sort(ll*s,int n)
{
    memset(v,0,sizeof(v));
//    for(int i=1;i<=n;++i) cout<<s[i]<<" ";puts("");
    for(int i=1;i<=n;++i)
        for(ll j=s[i],i=0;i<4;j>>=15,++i) ++v[i][j&N];
    for(int r=0;r<4;++r) for(int i=1;i<=N;++i) v[r][i]+=v[r][i-1];
    for(ll r=0,j=N;r<4;++r,j<<=15)
    {
        for(register int i=n;i;--i) sa[v[r][(s[i]&j)>>(r*15)]--]=s[i];
        for(register int i=1;i<=n;++i) s[i]=sa[i];
    }
}

int main()
{
    n=read();lim=n>>1;m=read();
    for(int i=1;i<=n;++i) a[i]=read();
    Dfs1(1,0);Dfs2(n,0);
    Sort(s,tot1);Sort(s2,tot2);
    for(int i=tot1,j=1;i;--i)
    {
        while(j<tot2&&s2[j+1]+s[i]<=m) ++j;
        if(s2[j]+s[i]<=m) ans+=j;
    }
    printf("%lld\n",ans);
    return 0;
}
时间: 2024-08-04 19:37:07

[bzoj4800][Ceoi2015]Ice Hockey World Championship的相关文章

【bzoj4800】: [Ceoi2015]Ice Hockey World Championship dfs

[bzoj4800]: [Ceoi2015]Ice Hockey World Championship N<=40所以如果直接dfs背包会TLE 考虑Meet-in-the-middle 如果把N个物品分成前后A B两段分别背包 分别在A B中可行的方案的花费记录在a b中 答案就是a[i]+b[j]<=M的个数 把a b排序 然后序列就是单调的了 两个指针扫一遍就好了 1 #include <cstdlib> 2 #include <cstring> 3 #inclu

【BZOJ4800】 [Ceoi2015]Ice Hockey World Championship

4800: [Ceoi2015]Ice Hockey World Championship Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 68  Solved: 30[Submit][Status][Discuss] Description 有n个物品,m块钱,给定每个物品的价格,求买物品的方案数. Input 第一行两个数n,m代表物品数量及钱数 第二行n个数,代表每个物品的价格 n<=40,m<=10^18 Output 一行一个数表示购买

【bzoj4800】[Ceoi2015]Ice Hockey World Championship 折半搜索

题目描述 有n个物品,m块钱,给定每个物品的价格,求买物品的方案数. 输入 第一行两个数n,m代表物品数量及钱数 第二行n个数,代表每个物品的价格 n<=40,m<=10^18 输出 一行一个数表示购买的方案数 (想怎么买就怎么买,当然不买也算一种) 样例输入 5 1000 100 1500 500 500 1000 样例输出 8 题解 裸的折半搜索meet-in-the-middle 由于直接爆搜肯定会TLE,考虑把整个序列分成左右两部分,对于每部分求出它所有可以消耗钱数的方案.然后考虑左右

[Ceoi2015]Ice Hockey World Championship

有n个物品,m块钱,给定每个物品的价格,求买物品的方案数. n<=40,m<=10^18 水题,meet in the middle裸题,随便搞搞,统计答案二分一下就没了 #include<cstdio> #include<algorithm> int n,sum;long long m,f[1500001],a[41],ans; void dfs(int x,long long now){ if(x>n/2){f[++sum]=now;return ;} dfs

搜索专讲

搜索专讲 Tags:搜索 https://www.zybuluo.com/xzyxzy/note/1058215 前言 做一个专题肯定是要花点时间的 但是哇,搜索怎么这么多内容?!WTF?! 好吧慢慢刷,待四五月份左右出pdf或ppt的讲义吧 先把题目放上,大家愿意的和我一起做吧 题目 李老师给了一个包 广搜 [x] ?POJ1426-Find The Multiple https://vjudge.net/problem/POJ-1426 [x] POJ2251-Dungeon Master

英语新闻常用词汇与短语

英语新闻常用词汇与短语 经济篇 accumulated deficit 累计赤字 active trade balance 贸易顺差 adverse trade balance 贸易逆差 aid 援助 allocation of funds 资金分配 allotment 拨款 allowance/grant/subsidy 补贴,补助金,津贴 amortization 摊销,摊还,分期偿付 annuity 年金 article 物品,商品 assigned 过户 autarchy 闭关自守 av

Google CFO 的辞职信

After nearly 7 years as CFO, I will be retiring from Google to spend more time with my family.  Yeah, I know you've heard that line before.  We give a lot to our jobs.  I certainly did.  And while I am not looking for sympathy, I want to share my tho

Google CFO 辞职信

After nearly 7 years as CFO, I will be retiring from Google to spend more time with my family.  Yeah, I know you've heard that line before.  We give a lot to our jobs.  I certainly did.  And while I am not looking for sympathy, I want to share my tho

You never know what&#39;s going to happen

In the absence of the center of the Marcel Goc and striker Kris Letang, two injured Friday continued. Andrew Ebbett was called on the squad to take the place of GDC, and Bortuzzo walked in letang.notes: Penguins in Rowley won four games in a row, the