QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#756075#9520. Concave HullANIG#WA 1ms3752kbC++143.9kb2024-11-16 18:57:052024-11-16 18:57:05

Judging History

你现在查看的是最新测评结果

  • [2024-11-16 18:57:05]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3752kb
  • [2024-11-16 18:57:05]
  • 提交

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 ;
    }

    sort(P.begin(),P.end());
    vector<pair<int,int>> Tmp;
    int mx=-inf;
    for(auto p:P){
        if(Tmp.size()&&p.first==Tmp.back().first) Tmp.pop_back();
        Tmp.emplace_back(p);
        mx=max(mx,p.second);
    }

    vector<array<int,3>> T;

    auto add=[&](int x,int y,int z)->void{
        if(y==0){
            ans=min(ans,mx*x+z);
            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;
        ans=min(ans,pt[0]*P[pnt].first+pt[1]*P[pnt].second+pt[2]);
    }

    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++;
        }
        //cerr<<"at "<<pt[0]<<' '<<pt[1]<<' '<<pt[2]<<' '<<P[pnt].first<<' '<<P[pnt].second<<endl;
        ans=min(ans,pt[0]*P[pnt].first+pt[1]*P[pnt].second+pt[2]);
    }

    ans=-ans;
    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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3752kb

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: 3660kb

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:

2178418010787347715
1826368028392114576
1651576777857324182
1881464272437127060
2117256139909034098
894016174881844630
2270894814172317098
1998507202705002329
1723334935769435838
1168083776464662517

result:

wrong answer 2nd lines differ - expected: '1826413114144932145', found: '1826368028392114576'