zoukankan      html  css  js  c++  java
  • 题解 P1633 【二进制】

    题目链接:Link

    Problem

    Solution

    看一眼数据范围,再看一眼“高性能”,不难发现直接大力dp即可。
    如果倒着做会比较麻烦,直接从 $ f(0,0,0,0,0)=0 $ 开始,使用刷表法。

    Code

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn=35;
    typedef long long LL;
    const LL oo=0x3f3f3f3f3f3f3f3fll;
    int T,L,a,b,c;
    LL f[maxn][maxn][maxn][maxn][2];
    inline void CM(LL &a,const LL &b) { a=min(a,b); }
    int read()
    {
    	int x; scanf("%d",&x);
    	int stk[80],cnt=0;
    	while(x) stk[cnt++]=x&1,x>>=1;
    	L=max(L,cnt);
    	x=0;
    	for(int i=0;i<cnt;i++) x+=stk[i];
    	return x;
    }
    int main()
    {
    	#ifdef local
    	freopen("pro.in","r",stdin);
    	#endif
    	scanf("%d",&T);
    	memset(f,0x3f,sizeof(f));
    	f[0][0][0][0][0]=0;
    	for(int i=0;i<32;i++)
    		for(int a=0;a<32;a++)
    			for(int b=0;b<32;b++)
    				for(int c=0;c<32;c++)
    				{
    					LL v=f[i][a][b][c][0];
    					CM(f[i+1][a][b][c][0],v);
    					CM(f[i+1][a+1][b][c+1][0],v+(1<<i));
    					CM(f[i+1][a][b+1][c+1][0],v+(1<<i));
    					CM(f[i+1][a+1][b+1][c][1],v+(1<<i+1));
    					v=f[i][a][b][c][1];
    					CM(f[i+1][a][b][c+1][0],v);
    					CM(f[i+1][a+1][b][c][1],v+(1<<i));
    					CM(f[i+1][a][b+1][c][1],v+(1<<i));
    					CM(f[i+1][a+1][b+1][c+1][1],v+(1<<i+1));
    				}
    	while(T-->0)
    	{
    		L=0;
    		a=read(); b=read(); c=read();
    		printf("%lld
    ",f[L][a][b][c][0]==oo?-1:f[L][a][b][c][0]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    12.20个人任务总结
    典型用户和用户场景描述
    12.19冲刺总结
    12月18日任务总结
    构建之法阅读笔记02
    构建之法阅读笔记03
    构建之法阅读笔记01
    软件工程个人作业01
    内容提供者
    Pull解析Xml
  • 原文地址:https://www.cnblogs.com/happyZYM/p/11748882.html
Copyright © 2011-2022 走看看