0
点赞
收藏
分享

微信扫一扫

codeforces GR19 E

前端王祖蓝 2022-02-18 阅读 87

题目
题意: 给定数组n,ban掉m对组合。求最大的f(x,y).
  f(x,y): (cntx + cnty) * (x + y) 被ban掉的组合不算数,题目保证至少存在一个合法的(x,y). n = m = 3e5
思路:  非常巧妙。从cnt角度出发,我们可以将所有cnt相同的数放入同一个数组中,从大到小依次枚举,找到一对没有被ban的以后就可以退出了。具体表现为枚举x的次数,再枚举次数<=x的次数的数,当找到一个未被ban的数对后,内存循环break.
时间复杂度: O(nlogn + (n+m)logm).
  map统计数量
  我们可以发现,前三层循环的时间复杂度是O(n)的。为什么呢?假设每个数仅出现一次,恰好是n次。当某个数出现次数+1,另一个数出现次数-1.(总共n个数) 第一层循环的1会变成2,并且j枚举到2. 这样看似是+1,但是你两个数按照cnt枚举只枚举了一次,两个数枚举2次,还是平均一个数枚举一次。按照数学归纳法,时间复杂度均摊是O(n).
  再看最后一层,map判断数对是否存在需要logm,而且最多会被惩罚m次ban的数对。所以和为O(n+m)logm
代码:

// Problem: E. Best Pair
// Contest: Codeforces - Codeforces Global Round 19
// URL: https://codeforces.com/contest/1637/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i) 
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() 四舍五入 ceil() 向上取整 floor() 向下取整
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 3e5+10;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){ return x&(-x);}
template<typename T>void write(T x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
    {
        write(x/10);
    }
    putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
int n,m,k,T;
map<PII,int> has;
map<int,int> mp;
int a[N];
vector<int> va[N];
void solve()
{
   mp.clear(); has.clear();
   read(n); read(m);
   for(int i=1;i<=n;++i) read(a[i]),mp[a[i]]++,va[i].clear();
   while(m--)
   {
   	  int x,y; read(x),read(y);
   	  has[{x,y}]++;
   	  has[{y,x}]++;
   }
   for(auto item:mp)
   {
   	  int x = item.first;
   	  int num = item.second;
   	  va[num].push_back(x);
   }
   for(int i=1;i<=n;++i) sort(va[i].begin(),va[i].end(),greater<int>());
   ll ans = 0;
   for(int i=1;i<=n;++i)
   {
   	  for(auto x:va[i])
   	  {
   	  	 for(int j=1;j<=i;++j)
   	  	 {
   	  	 	for(auto y:va[j])
   	  	 	{
   	  	 		if(x == y || has[{x,y}]) continue;
   	  	 		// cout<<i<<":"<<x<<' '<<j<<":"<<y<<endl;
   	  	 		ans = max(ans,1ll*(i+j)*(x+y));
   	  	 		break;
   	  	 	}
   	  	 }
   	  }
   }
   write(ans); puts("");
}
signed main(void)
{  
   // T = 1;
   // OldTomato; cin>>T;
   read(T);
   while(T--)
   {
   	 solve();
   }
   return 0;
}

举报

相关推荐

0 条评论