zoukankan      html  css  js  c++  java
  • CF1554B Cobb

    B. Cobb

    这个题比较巧妙。

    首先我们考虑 (n) 比较大的时候,由于 (a_ileq n),答案一定是 (n^2) 级别的。随着 (n) 不断减小的过程,答案被更新的概率不断减小,所以我们可以用一个时间复杂度不严格为 (O(n^2)) 的算法通过此题。

    其实这个剪枝很好想,我们枚举 (i) 时,从大向小枚举,再枚举 (i*j<ans) 时((ans) 为当前答案),不再枚举即可。

    //Don't act like a loser.
    //This code is written by huayucaiji
    //You can only use the code for studying or finding mistakes
    //Or,you'll be punished by Sakyamuni!!!
    #include<bits/stdc++.h>
    #define int long long
    using namespace std;
    
    int read() {
    	char ch=getchar();
    	int f=1,x=0;
    	while(ch<'0'||ch>'9') {
    		if(ch=='-')
    			f=-1;
    		ch=getchar();
    	}
    	while(ch>='0'&&ch<='9') {
    		x=x*10+ch-'0';
    		ch=getchar();
    	}
    	return f*x;
    }
    
    const int MAXN=1e5+10;
    
    int n,k,ans;
    int a[MAXN];
    
    signed main() {
    	int t=read();
    	while(t--) {
    		cin>>n>>k;
    		ans=-1e18;
    		for(int i=1;i<=n;i++) {
    			a[i]=read();
    		}
    		
    		for(int i=n;i;i--) {
    			for(int j=i-1;j;j--) {
    				if(i*j<=ans) {
    					break;
    				}
    				ans=max(ans,i*j-k*(a[i]|a[j]));
    			}
    		}
    		cout<<ans<<endl;
    	}
    
    	return 0;
    }
    
  • 相关阅读:
    Eclipse 安装C++
    工厂模式
    程序员7年和我的7点感想――我的程序人生
    Java中的==和equals区别
    编程之美1
    Java_Ant详解
    我用电脑说爱你
    Oracle分页的SQL语句
    使用js获取父窗口iframe的高度
    Oracle PLSQL中 左连接和右连接用法
  • 原文地址:https://www.cnblogs.com/huayucaiji/p/CF1554B.html
Copyright © 2011-2022 走看看