QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#639278 | #9458. Triangulation | Afterlife | TL | 331ms | 114492kb | C++17 | 4.1kb | 2024-10-13 18:41:19 | 2024-10-13 18:41:20 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
int T;
const int N=18;
int w[N][N];
struct P {
int x,y;
int id;
};
bool operator <(const P &a,const P &b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
P operator -(const P &a,const P &b)
{
return {a.x-b.x,a.y-b.y};
}
int det(const P &a,const P &b)
{
return a.x*b.y-a.y*b.x;
}
int n;
vector<P> p;
int ok[N][N][N];
vector<P> conv(vector<P> v)
{
vector<P> c;
sort(v.begin(),v.end());
for(auto p:v)
{
while(c.size()>1&&det(p-c.back(),p-c.end()[-2])>=0)
c.pop_back();
c.push_back(p);
}
int m=c.size();
reverse(v.begin(),v.end());
for(auto p:v)
{
while(c.size()>m&&det(p-c.back(),p-c.end()[-2])>=0)
c.pop_back();
c.push_back(p);
}
c.pop_back();
return c;
}
using pii=pair<int,int>;
pii merge(const pii &a,const pii &b)
{
if(a.first<b.first)
return a;
else if(a.first>b.first)
return b;
else
return {a.first,a.second+b.second};
}
pii f[1<<N][N];
int ed,v[1<<N][N];
pii dfs(int S,int l)
{
if(S==ed)
return {0,1};
if(v[S][l])
return f[S][l];
v[S][l]=1;
pii &ret=f[S][l];
ret.first=1e18;
vector<int> vs;
for(int i=0;i<n;i++)
if(S>>i&1)
vs.push_back(i);
int e=0;
for(int i=1;i+1<n;i++)
{
while(e+1<vs.size()&&vs[e+1]<=i)
e++;
if(i==l)
continue;
if(i<l)
{
int T=S|(1<<i)|(1<<l);
T>>=i+1;
if(__builtin_ctz(T)!=(l-i-1))
continue;
}
if(S>>i&1)
{
int j=vs[e-1],k=vs[e+1];
if(ok[j][i][k]==-1)
{
auto W=dfs(S^(1<<i),i);
W.first+=w[j][k];
ret=merge(ret,W);
}
}
else
{
int j=vs[e],k=vs[e+1];
if(ok[j][i][k]==1)
{
auto W=dfs(S^(1<<i),i);
W.first+=w[j][i]+w[i][k];
ret=merge(ret,W);
}
}
}
return ret;
}
signed main()
{
cin>>T;
while(T--)
{
cin>>n;
p.resize(n);
int z=0;
int cs=rand()%1000,sn=rand()%1000;
for(auto &[x,y,id]:p)
{
id=z++;
cin>>x>>y;
int u=cs*x-sn*y,v=sn*x+cs*y;
x=u,y=v;
}
sort(p.begin(),p.end());
vector<int> pos(n);
for(int i=0;i<n;i++)
pos[p[i].id]=i;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>w[pos[i]][pos[j]];
for(int i=0;i<n;i++)
p[i].id=i;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
for(int k=j+1;k<n;k++)
{
auto z=conv({p[i],p[j],p[k]});
ok[i][j][k]=1;
for(int l=0;l<n;l++)
{
auto u=p[l];
bool in=1;
for(int j=0;j<3;j++)
if(det(z[(j+1)%3]-z[j],u-z[j])<=0)
in=0;
if(in)
ok[i][j][k]=0;
}
if(!ok[i][j][k])
continue;
if(det(p[j]-p[i],p[k]-p[i])>0)
ok[i][j][k]=-1;
}
for(int S=0;S<(1<<n);S++)
for(int i=0;i<n;i++)
v[S][i]=0;
auto c=conv(p);
auto m=max_element(c.begin(),c.end())-c.begin();
ed=0;
int st=0;
for(int i=0;i<=m;i++)
st|=1<<c[i].id;
for(int j=m;j<c.size();j++)
ed|=1<<c[j].id;
ed|=1<<c[0].id;
auto ans=dfs(st,0);
for(int i=0;i+1<=m;i++)
ans.first+=w[c[i].id][c[i+1].id];
cout<<ans.first<<" "<<ans.second<<"\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5616kb
input:
2 4 0 0 1 1 1 0 0 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 4 0 0 3 0 1 3 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0
output:
5 2 6 1
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 331ms
memory: 114492kb
input:
68 3 454 364 117 336 271 84 0 6 2 6 0 10 2 10 0 4 454 364 117 336 271 84 274 296 0 3 2 10 3 0 6 4 2 6 0 5 10 4 5 0 4 603 817 230 695 230 303 604 183 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 454 364 117 336 271 84 274 296 220 225 0 1 7 3 2 1 0 4 4 2 7 4 0 1 5 3 4 1 0 0 2 2 5 0 0 5 666667 788675 333173 78858...
output:
18 1 30 1 0 2 27 1 38 2 35 5 54 1 44 2 18 14 69 1 12 1 88 42 59 1 23 8 180 150 80 1 23 2 0 780 30 12 504 4550 30 4 19656 26888 29 6 26700 168942 24 88 22770 1098904 21 28 30816 7281984 24 27 15327 49789428 24 4 16338 342305320 21 48 42615 2410168190 22 360 44928 17309628327 1240448 1 2613579 1 19532...
result:
ok 68 lines
Extra Test:
score: -3
Extra Test Failed : Time Limit Exceeded on 2
input:
70 18 523990 661228 542094 867454 715348 957693 724847 921955 185285 504782 340889 164109 548808 628313 528132 176875 403401 165509 733176 362440 62653 306280 841647 146408 169951 295342 186442 591918 405268 31651 207390 724432 622724 775650 495011 800641 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
output:
0 89323676 0 130331234 0 144675335 0 94323563 0 213829766 0 118195574 0 170444382 0 181328906 0 123390508 0 294695268 0 138491802 0 350786299 0 318187474 0 150111020 0 103700636 0 269957872 0 206602029 0 176150490 0 56950772 0 285570660 0 201339302 0 152004614 0 340341158 0 84076794 0 54258411