当前位置:网站首页>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;
}
边栏推荐
- 剑指 Offer 09. 用两个栈实现队列
- Under the national teacher qualification certificate in the first half of 2022
- YOLOv5添加注意力機制
- [allocation problem] 135 Distribute candy
- [turn to] MySQL operation practice (III): table connection
- FVP和Juno平台的Memory Layout介绍
- Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
- EOJ 2021.10 E. XOR tree
- Introduction to memory layout of FVP and Juno platforms
- High precision subtraction
猜你喜欢
远程升级怕截胡?详解FOTA安全升级
To be continued] [UE4 notes] L4 object editing
个人开发的渗透测试工具Satania v1.2更新
Romance of programmers on Valentine's Day
YOLOv5-Shufflenetv2
YOLOv5添加注意力机制
To the distance we have been looking for -- film review of "flying house journey"
Improvement of pointnet++
Service fusing hystrix
Palindrome (csp-s-2021-palin) solution
随机推荐
剑指 Offer 53 - II. 0~n-1中缺失的数字
Sword finger offer 05 Replace spaces
Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
Detailed explanation of expression (csp-j 2021 expr) topic
个人开发的渗透测试工具Satania v1.2更新
Educational Codeforces Round 107 (Rated for Div. 2) E. Colorings and Dominoes
object serialization
kubeadm系列-00-overview
Kubedm series-00-overview
Configuration and startup of kubedm series-02-kubelet
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
EOJ 2021.10 E. XOR tree
Developing desktop applications with electron
SAP-修改系统表数据的方法
Haut OJ 1352: string of choice
记录QT内存泄漏的一种问题和解决方案
Haut OJ 1357: lunch question (I) -- high precision multiplication
Haut OJ 1401: praise energy
Fragment addition failed error lookup
[to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column