当前位置:网站首页>CF1637E Best Pair
CF1637E Best Pair
2022-07-05 05:31:00 【solemntee】

The question : Give you a multiple set , seek ( x + y ) × ( c n t x + c n t y ) (x+y)\times(cnt_x+cnt_y) (x+y)×(cntx+cnty) The maximum of , among x ! = y x!=y x!=y And there are m m m Yes ( x , y ) (x,y) (x,y) You can't choose
Enumerate first ( x , y ) (x,y) (x,y) yes O ( N 2 ) O(N^2) O(N2) Of , Let's consider enumerating ( c n t x , c n t y ) (cntx,cnty) (cntx,cnty), Because it's different c n t x cntx cntx yes O ( N ) O(\sqrt N) O(N) Of , Let's enumerate ( c n t x , c n t y ) (cnt_x,cnt_y) (cntx,cnty). For the number of occurrences c n t x cnt_x cntx The number can be selected from large to small greedily .
Build a priority queue , initial p u s h ( 1 , 1 ) push(1,1) push(1,1), Express c n t x cnt_x cntx The sum of the largest numbers in the set c n t y cnt_y cnty The largest number in the set .( This process and that contour + The process of priority queue is very similar ), If ( 1 , 1 ) (1,1) (1,1) Illegal continue to put ( 1 , 2 ) and ( 2 , 1 ) (1,2) and (2,1) (1,2) and (2,1) Repeat this operation until the first legal , Namely c n t x cnt_x cntx and c n t y cnt_y cnty The maximum of . In this way, we can O ( N + M l o g ( M ) ) O(N+Mlog(M)) O(N+Mlog(M)) complete .
In fact, it feels very simple and freehand , There may be some details in writing .
D If the question is too simple, there is no water .
#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct node
{
int x,y;
int val;
bool operator<(const node &ths)const
{
return val<ths.val;
}
};
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
vector<int>a(n+1);
map<int,int>mp;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
mp[a[i]]++;
}
set<pair<int,int>>bad;
for(int i=1;i<=m;i++)
{
int tu,tv;
scanf("%d%d",&tu,&tv);
if(tu>tv)swap(tu,tv);
bad.insert({
tu,tv});
}
for(int i=1;i<=n;i++)bad.insert({
a[i],a[i]});
int N=sqrt(n);
vector<pair<int,int>>huge;
vector<vector<int>>small(N+1);
for(auto [fi,se]:mp)
{
if(se<N)small[se].push_back(fi);
else huge.push_back({
fi,se});
}
for(int i=1;i<N;i++)sort(small[i].begin(),small[i].end(),greater<int>());
ll ans=-1e18;
for(int size=2;size<=2*N-2;size++)
{
for(int sizex=1;sizex<min(size,N);sizex++)
{
int sizey=size-sizex;
if(sizey<N&&sizex<=sizey)
{
int mark1=0,mark2=0;
set<pair<int,int>>vis;
priority_queue<node>q;
if(mark1<small[sizex].size()&&mark2<small[sizey].size())
q.push({
mark1,mark2,small[sizex][mark1]+small[sizey][mark2]});
vis.insert({
mark1,mark2});
while(!q.empty())
{
auto [x,y,val]=q.top();
int tx=small[sizex][x],ty=small[sizey][y];
if(tx>ty)swap(tx,ty);
if(bad.count({
tx,ty}))
{
if(x+1<small[sizex].size()&&vis.count({
x+1,y})==0)
{
q.push({
x+1,y,small[sizex][x+1]+small[sizey][y]});
vis.insert({
x+1,y});
}
if(y+1<small[sizey].size()&&vis.count({
x,y+1})==0)
{
q.push({
x,y+1,small[sizex][x]+small[sizey][y+1]});
vis.insert({
x,y+1});
}
}
else
{
ans=max(ans,1LL*size*val);
break;
}
q.pop();
}
}
}
}
for(int i=0;i<huge.size();i++)
{
for(int j=i+1;j<huge.size();j++)
{
auto [num1,cnt1]=huge[i];
auto [num2,cnt2]=huge[j];
if(bad.count({
num1,num2})==0)ans=max(ans,1LL*(1LL*num1+num2)*(1LL*cnt1+cnt2));
}
}
for(int i=0;i<huge.size();i++)
{
auto [NUM,cnt]=huge[i];
for(int j=1;j<N;j++)
{
for(auto num:small[j])
{
if(bad.count({
min(num,NUM),max(num,NUM)})==0)
{
ans=max(ans,1LL*(1LL*NUM+num)*(1LL*cnt+j));
break;
}
}
}
}
printf("%lld\n",ans);
}
return 0;
}
边栏推荐
- 每日一题-搜索二维矩阵ps二维数组的查找
- Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
- Reader writer model
- PC寄存器
- What is the agile proportion of PMP Exam? Dispel doubts
- 发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
- How can the Solon framework easily obtain the response time of each request?
- 【实战技能】非技术背景经理的技术管理
- A preliminary study of sdei - see the essence through transactions
- To be continued] [UE4 notes] L4 object editing
猜你喜欢
![[interval problem] 435 Non overlapping interval](/img/a3/2911ee72635b93b6430c2efd05ec9a.jpg)
[interval problem] 435 Non overlapping interval

Pointnet++的改进

第六章 数据流建模—课后习题

【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors

Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail

个人开发的渗透测试工具Satania v1.2更新

object serialization
![[merge array] 88 merge two ordered arrays](/img/e9/a73d9f22eead8e68c1e45c27ff6e6c.jpg)
[merge array] 88 merge two ordered arrays

Palindrome (csp-s-2021-palin) solution

lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
随机推荐
服务熔断 Hystrix
On-off and on-off of quality system construction
How many checks does kubedm series-01-preflight have
剑指 Offer 05. 替换空格
注解与反射
National teacher qualification examination in the first half of 2022
A misunderstanding about the console window
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
Web APIs DOM node
kubeadm系列-02-kubelet的配置和启动
Haut OJ 1241: League activities of class XXX
记录QT内存泄漏的一种问题和解决方案
Sword finger offer 05 Replace spaces
Control Unit 控制部件
发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
Haut OJ 1352: string of choice
剑指 Offer 04. 二维数组中的查找
游戏商城毕业设计
Use of room database
SDEI初探-透过事务看本质