QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#756188 | #9520. Concave Hull | ANIG# | WA | 1ms | 3656kb | C++14 | 5.1kb | 2024-11-16 19:19:17 | 2024-11-16 19:19:24 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
using E=long long;
const int inf=8e18;
struct vec{
int x,y;
vec(int a=0,int b=0){
x=a,y=b;
}
};
vec operator + (const vec &X,const vec &Y){
return vec(X.x+Y.x,X.y+Y.y);
}
vec operator - (const vec &X,const vec &Y){
return vec(X.x-Y.x,X.y-Y.y);
}
int cross(const vec &X,const vec &Y){
return X.x*Y.y-X.y*Y.x;
}
int sgn(int x){
if(x<0) return -1;
if(x>0) return 1;
return 0;
}
int area(const vec &X,const vec &Y,const vec &Z){
return abs(X.x*Y.y+Y.x*Z.y+Z.x*X.y-X.x*Z.y-Y.x*X.y-Z.x*Y.y);
}
bool cmp(const vec &X,const vec &Y){
if(sgn(X.x-Y.x)) return X.x<Y.x;
return X.y<Y.y;
}
void solve(){
int n;
cin>>n;
vector<vec> a(n);
for(int i=0; i<n; i++){
cin>>a[i].x>>a[i].y;
}
sort(a.begin(),a.end(),cmp);
vector<int> z;
for(int i=0; i<n; i++){
while(z.size()>1&&cross(a[z.back()]-a[z[z.size()-2]],a[i]-a[z[z.size()-2]])<=0) z.pop_back();
z.emplace_back(i);
}
int k=z.size();
for(int i=n-2; i>=0; i--){
while(z.size()>k&&cross(a[z.back()]-a[z[z.size()-2]],a[i]-a[z[z.size()-2]])<=0) z.pop_back();
z.emplace_back(i);
}
if(n>1) z.pop_back();
vector<int> used(n);
for(auto p:z){
used[p]=1;
//cerr<<"convex "<<a[p].x<<' '<<a[p].y<<endl;
}
int ans=inf;
vector<pair<int,int>> P;
for(int i=0; i<n; i++){
if(!used[i]){
P.emplace_back(a[i].x,a[i].y);
}
}
if(!P.size()){
cout<<-1<<'\n';
return ;
}
auto getpos=[&](int k1,int b1,int k2,int b2)->int{
return (b2-b1)/(k1-k2);
};
sort(P.begin(),P.end());
vector<pair<int,int>> Tmp;
int mx=-inf;
for(auto p:P){
if(Tmp.size()==0||p.second<Tmp.back().second){
while(Tmp.size()>1&&getpos(p.first,p.second,Tmp[Tmp.size()-2].first,Tmp[Tmp.size()-2].second)<
getpos(Tmp.back().first,Tmp.back().second,Tmp[Tmp.size()-2].first,Tmp[Tmp.size()-2].second)) Tmp.pop_back();
Tmp.emplace_back(p);
}
mx=max(mx,p.second);
}
swap(Tmp,P);
vector<array<int,3>> T;
auto add=[&](int x,int y,int z)->void{
if(y==0){
ans=min(ans,mx*x+z);
//cerr<<ans<<endl;
return ;
}
T.emplace_back(array<int,3>({x,y,z}));
};
for(int i=0; i+1<z.size(); i++){
add(a[z[i]].y-a[z[i+1]].y,
a[z[i+1]].x-a[z[i]].x,
a[z[i]].x*a[z[i+1]].y-a[z[i+1]].x*a[z[i]].y);
}
add(a[z.back()].y-a[z[0]].y,
a[z[0]].x-a[z.back()].x,
a[z.back()].x*a[z[0]].y-a[z[0]].x*a[z.back()].y);
sort(P.begin(),P.end(),greater<>());
vector<array<int,3>> T1,T2;
for(auto &pt:T){
if(pt[1]>0){
T1.emplace_back(pt);
}
else T2.emplace_back(pt);
}
sort(T1.begin(),T1.end(),[&](auto &X,auto &Y){ return X[0]*Y[1]<X[1]*Y[0]; });
int pnt=0;
for(auto &pt:T1){
while(pnt+1<P.size()&&pt[0]*P[pnt].first+pt[1]*P[pnt].second
>pt[0]*P[pnt+1].first+pt[1]*P[pnt+1].second){
pnt++;
}
//cerr<<"at "<<pt[0]<<' '<<pt[1]<<' '<<pt[2]<<' '<<P[pnt].first<<' '<<P[pnt].second<<endl;
//cerr<<pt[0]*P[pnt].first+pt[1]*P[pnt].second+pt[2]<<endl;
ans=min(ans,pt[0]*P[pnt].first+pt[1]*P[pnt].second+pt[2]);
}
swap(Tmp,P);
Tmp.clear();
sort(P.begin(),P.end(),greater<>());
for(auto p:P){
if(Tmp.size()==0||p.second>Tmp.back().second){
while(Tmp.size()>1&&getpos(p.first,p.second,Tmp[Tmp.size()-2].first,Tmp[Tmp.size()-2].second)<
getpos(Tmp.back().first,Tmp.back().second,Tmp[Tmp.size()-2].first,Tmp[Tmp.size()-2].second)) Tmp.pop_back();
Tmp.emplace_back(p);
}
mx=max(mx,p.second);
}
swap(Tmp,P);
sort(T2.begin(),T2.end(),[&](auto &X,auto &Y){ return X[0]*Y[1]>X[1]*Y[0]; });
pnt=0;
for(auto &pt:T2){
while(pnt+1<P.size()&&pt[0]*P[pnt].first+pt[1]*P[pnt].second
>pt[0]*P[pnt+1].first+pt[1]*P[pnt+1].second){
pnt++;
}
/*
for(auto x:P){
//cerr<<x.first<<','<<x.second<<' ';
cerr<<pt[0]*x.first+pt[1]*x.second+pt[2]<<' ';
}
cerr<<endl;*/
//cerr<<"at "<<pt[0]<<' '<<pt[1]<<' '<<pt[2]<<' '<<P[pnt].first<<' '<<P[pnt].second<<endl;
//cerr<<pt[0]*P[pnt].first+pt[1]*P[pnt].second+pt[2]<<endl;
ans=min(ans,pt[0]*P[pnt].first+pt[1]*P[pnt].second+pt[2]);
}
ans=-ans;
cerr<<ans<<'\n';
for(int i=2; i<z.size(); i++){
ans+=area(a[z[i]],a[z[i-1]],a[z[0]]);
}
cout<<ans<<'\n';
}
/*
1
6
-2 0
1 -2
5 2
0 4
1 2
3 1
convex -2 0
convex 1 -2
convex 5 2
convex 0 4
at 4 -2 8 1 2
at -4 4 12 1 2
at -2 -5 20 1 2
at 2 3 4 1 2
36
1
4
0 0
1 0
0 1
1 1
*/
signed main(){
cin.tie(0),cout.tie(0)->sync_with_stdio(false);
int T;
cin>>T;
while(T--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3636kb
input:
2 6 -2 0 1 -2 5 2 0 4 1 2 3 1 4 0 0 1 0 0 1 1 1
output:
40 -1
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 3656kb
input:
10 243 -494423502 -591557038 -493438474 -648991734 -493289308 -656152126 -491185085 -661710614 -489063449 -666925265 -464265894 -709944049 -447472922 -737242534 -415977509 -773788538 -394263365 -797285016 -382728841 -807396819 -373481975 -814685302 -368242265 -818267002 -344482838 -833805545 -279398...
output:
2177958054552734781 1826413114144932145 1651687576234220014 1882491265383424105 2116990315949962171 894016174881844630 2271174337159857750 1998438479800218363 1727300161677395529 1167526672278426376
result:
wrong answer 1st lines differ - expected: '2178418010787347715', found: '2177958054552734781'