zoukankan      html  css  js  c++  java
  • 【JZOJ1252】【洛谷P5194】天平【搜索】

    题目大意:

    题目链接:

    JZOJ:https://jzoj.net/senior/#main/show/1252
    洛谷:https://www.luogu.org/problemnew/show/P5194

    nn个砝码,求使用这些砝码能测量不超过mm的最大重量是多少。


    思路:

    n40nleq 40,很明显普通的搜索是过不了的。
    可以考虑采用折半搜索。
    4040个砝码分成两半,搜索出两边各个能测量的价值,然后枚举其中一边的所有可以测量到的重量,将另外一边排序后二分,使得相加不超过mm且尽量大。在所有答案中取minmin即可。
    折半后每边搜索需O(22n)O(2^{frac{2}{n}}),排序O(n logn)O(n logn),枚举+二分O(n logn)O(n logn),所以总的时间复杂度是O(22n+n logn)O(2^{frac{2}{n}}+n logn)


    代码:

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    
    const int MAXN=1100000;
    int n,n1,m,x,a[50],ans,sum1,sum2,w1[MAXN],w2[MAXN];
    
    void dfs1(int x,int s,int maxn)
    {
    	if (s>m) return;
    	if (x>maxn)
    	{
    		w1[++sum1]=s;  //求出所有可以达到的重量
    		return;
    	}
    	dfs1(x+1,s,maxn);
    	dfs1(x+1,s+a[x],maxn);
    }
    
    void dfs2(int x,int s,int maxn)
    {
    	if (s>m) return;
    	if (x>maxn)
    	{
    		w2[++sum2]=s;
    		return;
    	}
    	dfs2(x+1,s,maxn);
    	dfs2(x+1,s+a[x],maxn);
    }
    
    int main()
    {
    	scanf("%d%d",&n1,&m);
    	for (int i=1;i<=n1;i++)
    	{
    		scanf("%d",&x);
    		if (x<=m) a[++n]=x;
    	}
    	dfs1(1,0,n/2);
    	dfs2(n/2+1,0,n);
    	w2[++sum2]=2147483647;
    	sort(w2+1,w2+sum2+1);
    	for (int i=1;i<=sum1;i++)
    	{
    		x=upper_bound(w2+1,w2+sum2+1,m-w1[i])-w2;  //二分
    		ans=max(ans,w1[i]+w2[x-1]);
    	}
    	printf("%d
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    ubuntu一些记录
    unittest添加测试用例方法
    弹出框处理
    无法连接终端
    Python 断言
    Appium_Python_Api文档
    pycharm快捷键
    appium运行时启动失败
    appium运行时每次默认弹出appiumsetting与unlock重装,关闭这两个步骤的方法
    SpringBoot的jar包引用外部properties文件
  • 原文地址:https://www.cnblogs.com/hello-tomorrow/p/11998353.html
Copyright © 2011-2022 走看看