当前位置:网站首页>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;
}
边栏推荐
- [speed pointer] 142 circular linked list II
- 每日一题-搜索二维矩阵ps二维数组的查找
- 卷积神经网络简介
- 挂起等待锁 vs 自旋锁(两者的使用场合)
- The number of enclaves
- SDEI初探-透过事务看本质
- Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
- 26、 File system API (device sharing between applications; directory and file API)
- Acwing 4300. Two operations
- Using HashMap to realize simple cache
猜你喜欢
SAP method of modifying system table data
【实战技能】非技术背景经理的技术管理
Pointnet++ learning
A misunderstanding about the console window
游戏商城毕业设计
Reader writer model
Sword finger offer 53 - ii Missing numbers from 0 to n-1
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
浅谈JVM(面试常考)
Sword finger offer 09 Implementing queues with two stacks
随机推荐
注解与反射
Acwing 4301. Truncated sequence
剑指 Offer 09. 用两个栈实现队列
MySQL数据库(一)
二十六、文件系统API(设备在应用间的共享;目录和文件API)
How many checks does kubedm series-01-preflight have
【Jailhouse 文章】Look Mum, no VM Exits
挂起等待锁 vs 自旋锁(两者的使用场合)
Haut OJ 1321: mode problem of choice sister
Graduation project of game mall
SDEI初探-透过事务看本质
The present is a gift from heaven -- a film review of the journey of the soul
[speed pointer] 142 circular linked list II
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
Haut OJ 1243: simple mathematical problems
Haut OJ 1357: lunch question (I) -- high precision multiplication
Fragment addition failed error lookup
第六章 数据流建模—课后习题
Cluster script of data warehouse project
How can the Solon framework easily obtain the response time of each request?