题解:AT_agc076_e [AGC076E] Squared Norm Maximization
Zhao_daodao · · 题解
AT_agc076_e
这个出题人真牛。
没有题解那我写一下,使用了 AI 辅助生成。有错误请指出。
因为
设最优解
设
考虑
v_k \in S
对于已经在最优集合
由于
利用向量展开式
消去等式两边的
v_k \notin S
对于不在最优集合
由于
利用向量展开式
消去共有项,有:
不难证明,如果有一个向量
所以,对于最优解,不存在
发现这个东西有 2 倍的差距,可以在这里面做文章。
以下对于是否选择一个向量
枚举模长
下证
此时判定等价于:
-
若
i \in S :根据推导,必有P \cdot v_i \geq 2|v_i|^2 。因为
\lambda \leq 2 ,故P \cdot v_i \geq 2|v_i|^2 \geq \lambda |v_i|^2 。因此,
i 必然被选中,i \in S' 。 -
若
i \notin S :根据推导,必有P \cdot v_i \leq |v_i|^2 。因为
\lambda \geq 1 ,故{P} \cdot {v}_i \leq 1|{v}_i|^2 \leq \lambda |{v}_i|^2 。因此,
i 必然不被算法选中,i \notin S' 。
证毕。
所以,只需要模长落在
此时可以倍增模长,即对于
现在假设固定了模长
则判定可以改写为
即
由于
- 若
|v_i| > R ,该不等式无解,说明在半径R 下v_i 绝不会被选中。 - 若
|v_i| \leq R ,设\alpha_i = \arccos\left(\frac{|v_i|}{R}\right) ,则\theta_u 的取值范围为:\theta_u \in [\theta_{v_i} - \alpha_i, \theta_{v_i} + \alpha_i] 。
此时可以枚举
总复杂度
Code
参考了官方代码。代码挺好写的。我错了,饶了我吧。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN=2.5e5+5;
const double pi=acos(-1);
inline int pf(int x){return x*x;}
struct Pair{
int x,y;Pair(int a=0,int b=0):x(a),y(b){}
inline friend Pair operator +(Pair A,Pair B){return Pair(A.x+B.x,A.y+B.y);}
inline friend Pair operator -(Pair A,Pair B){return Pair(A.x-B.x,A.y-B.y);}
inline friend Pair operator *(Pair A,int k){return Pair(A.x*k,A.y*k);}
inline int get(){return pf(x)+pf(y);}
};
int n;Pair a[MAXN];
double len[MAXN],f[MAXN];
struct node{
double f;Pair now;int opt;
inline friend bool operator <(node A,node B){
return A.f<B.f;
}
};vector<node>vec;
Pair now;int all;
inline void upd(Pair d,int k){
now=now+d*k;all+=d.get()*k;
}
inline int get(){
return now.get()-all*3;
}
inline double in(double x){
if(x<(-pi))x+=2*pi;
if(x>=pi)x-=2*pi;
return x;
}
inline void solve(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].x>>a[i].y;
f[i]=atan2(a[i].y,a[i].x);
len[i]=sqrt(a[i].get());
}
int ans=0,inf=4e9;double r=1;
while(r<=inf){
now=Pair(0,0);all=0;vec.clear();
for(int i=1;i<=n;i++){
if(len[i]<=r){
double the=acos(len[i]/r);
double l=in(f[i]-the),r=in(f[i]+the);
if(l>r)upd(a[i],1);
vec.push_back({l,a[i],1});
vec.push_back({r,a[i],-1});
}
}
sort(vec.begin(),vec.end());
ans=max<int>(ans,get());
for(auto it:vec){
upd(it.now,it.opt);
ans=max<int>(ans,get());
}
r*=1.9;
}
cout<<ans<<"\n";
}
signed main(){
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
int T;cin>>T;while(T--)solve();
}