QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#358832 | #6570. Who Watches the Watchmen? | Delay_for_five_minutes | WA | 0ms | 3856kb | C++20 | 6.4kb | 2024-03-20 01:45:38 | 2024-03-20 01:45:38 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int INF = 1e7;
const int N = 505 ;
int n ;
array<int,3> p[505] , t[505];
int e[N][N];
int w[N*2],s[N*2],pre[N*2],mat[N*2],cur,pn;
bool vis[N*2];
void bfs(int x){
memset(vis,0,sizeof(vis));
memset(s,63,sizeof(s));
memset(pre,0,sizeof(pre));
cur=pn=0,mat[0]=x;
while(1){
x=mat[cur];
int dis=INF;
vis[cur]=1;
for(int i=n+1;i<=2*n;++i){
if(vis[i])continue;
int tmp=w[i]+w[x]-e[x][i-n];
if(tmp<s[i])s[i]=tmp,pre[i]=cur;
if(s[i]<dis)dis=s[i],pn=i;
}
w[mat[0]]-=dis,w[0]+=dis;
for(int i=n+1;i<=2*n;++i){
if(vis[i])w[i]+=dis,w[mat[i]]-=dis;
else s[i]-=dis;
}
cur=pn;
if(!mat[cur])break;
}
while(cur){
mat[cur]=mat[pre[cur]];
cur=pre[cur];
}
}
int KM(){
for(int i=1;i<=n;++i)bfs(i);
int res=0;
for(int i=1;i<=n;++i)if(mat[i+n])res+=e[mat[i+n]][i];
return res;
}
int sgn(int x) {
if(x > 0) return 1;
else if(x == 0) return 0;
return -1;
}
bool check(int i,int j) { ///i指向j
array<int,3> delta = {p[j][0] - p[i][0] , p[j][1] - p[i][1] , p[j][2] - p[i][2]} ;
if(sgn(delta[0]) == sgn(t[i][0]) & sgn(delta[1]) == sgn(t[i][1]) && sgn(delta[2]) == sgn(t[i][2]) ) {
if(1LL * delta[0] * t[i][1] == 1LL * delta[1] * t[i][0] && 1LL * delta[1] * t[i][2] == 1LL * delta[2] * t[i][1]) return 1;
return 0;
}
return 0;
}
bool gx(int i,int j) {
array<int,3> delta = {p[j][0] - p[i][0] , p[i][1] - p[j][1] , p[i][2] - p[j][2]} ;
if(1LL * delta[0] * t[i][1] == 1LL * delta[1] * t[i][0] && 1LL * delta[1] * t[i][2] == 1LL * delta[2] * t[i][1]) return 1;
return 0;
}
bool par(int i,int j) {
if(1LL * t[i][0] * t[j][1] == 1LL * t[i][1] * t[j][0] && 1LL * t[i][1] * t[j][2] == 1LL * t[i][2] * t[j][1]) return 1;
return 0;
}
typedef pair<ll,ll> pll ;
pair<pll,pll> solve(array<int,3> u1,array<int,3> u2) {
ll a = 1LL * u1[2] * (-u2[0]) + 1LL * u2[2] * u1[0] ; /// a/b
ll b = 1LL * u1[1] * (-u2[0]) + 1LL * u2[1] * u1[0] ;
ll g = __gcd(a , b) ; a /= g;b /= g;
if(b < 0) a *= -1 , b *= -1;
ll c = 1LL * u1[2] * (-u2[1]) + 1LL * u2[2] * u1[1] ; /// c/d
ll d = 1LL * u1[0] * (-u2[1]) + 1LL * u2[0] * u1[1] ;
g = __gcd(c , d) ; c /= g;d /= g;
if(d < 0) c *= -1 , d *= -1;
return {(pll){c,d},(pll){a,b}} ;
}
int get_ans(int i,int j,int k) {
if(gx(i , j) && gx(k , j)) return 2;
if(gx(i , j) || gx(k , j)) return 1;
if(par(i , k)) return 1;
pair<pll,pll> a1 = solve({t[i][0] , t[k][0] , p[k][0] - p[j][0]} , {t[i][1] , t[k][1] , p[k][1] - p[j][1]}) ;
if(a1.first.first < 0 || a1.second.first < 0) return 1;
pair<pll,pll> a2 = solve({t[i][0] , t[k][0] , p[k][0] - p[j][0]} , {t[i][2] , t[k][2] , p[k][2] - p[j][2]}) ;
if(a2.first != a1.first || a2.second != a1.second) return 1;
return 0;
}
int id[505];
void work() {
int ans = 1e7;
for(int i = 1;i <= n;i++) id[i] = i;
sort(id + 1 , id + n + 1 , [&](int x,int y) {
if(p[x][0] != p[y][0]) return p[x][0] < p[y][0] ;
if(p[x][1] != p[y][1]) return p[x][1] < p[y][1] ;
return p[x][2] < p[y][2];
});
if(n % 2 == 0) {
int ans = 0;
for(int i = 1;i <= n;i += 2) {
ans += (2 - check(id[i] , id[i + 1]) - check(id[i + 1] , id[i])) ;
}
cout << ans << '\n' ;
return ;
}
for(int i = 1;i <= n;i++) {
vector<int> v;
for(int j = 1;j <= n;j++) {
if(j != i ) v.push_back(j) ;
}
vector<int> pre(v.size()) , suf(v.size());
for(int i = 0;i + 1 < v.size();i += 2) {
if(i) pre[i + 1] = pre[i - 1] ;
pre[i + 1] += 2 - (check(id[v[i]] , id[v[i + 1]]) + check(id[v[i + 1]] , id[v[i]]) );
}
for(int i = v.size() - 1;i - 1 >= 0;i -= 2) {
if(i != v.size() - 1) suf[i - 1] = suf[i + 1];
suf[i - 1] += 2 - (check(id[v[i]] , id[v[i - 1]]) + check(id[v[i - 1]] , id[v[i]])) ;
}
int sol = 1e7;
for(int j = 0;j < v.size();j++) {
int cur1 = 0 , cur2 = 0;
if(j % 2 == 1) continue ;
for(int k = j + 1;k < v.size();k++) {
cur1 += 1 - check(id[v[k - 1]] , id[v[k]]) ;
cur2 += 1 - check(id[v[k]] , id[v[k - 1]]) ;
// printf("%d %d %d\n",j,k,v.size()) ;
if((v.size() - k) % 2 == 0) continue ;
// printf("here\n") ;
int ad = 0;
if(j > 0) ad += pre[j - 1] ;
if(k < v.size() - 1) ad += suf[k + 1];
sol = min(sol , cur1 + get_ans(i , id[v[j]] , id[v[k]]) + ad) ;
sol = min(sol , cur2 + get_ans(i , id[v[k]] , id[v[k]]) + ad) ;
// printf("%d %d %d : %d\n",i,j,k , sol) ;
}
}
ans = min(ans , sol) ;
}
cout << (ans + 1000) << '\n' ;
return ;
}
int main() {
cin >> n;
for(int i = 1;i <= n;i++) {
for(int j = 0;j < 3;j++) cin >> p[i][j];
for(int j = 0;j < 3;j++) cin >> t[i][j];
}
if(n == 1) {
cout << -1 << '\n' ; return 0;
}
if(n == 2) {
int ans = check(1 , 2) + check(2 , 1);
cout << (2-ans) << '\n' ; return 0;
}
for(int i = n;i >= 1;i--) {
for(int j = 0;j < 3;j++) p[i][j] -= p[1][j] ;
}
bool ff = 1;
for(int i = 3;i <= n;i++) {
if(!(1LL * p[i][0] * p[2][1] == 1LL * p[i][1] * p[2][0] && 1LL * p[i][1] * p[2][2] == 1LL * p[i][2] * p[2][1])) {ff = 0;break ;}
}
if(ff) {
work() ; return 0;
}
for(int i = 1;i <= n;i++) {
map<array<int,3> , vector<pii> > mp;
e[i][i] = 1e7;
for(int j = 1;j <= n;j++) {
if(i == j) continue ;
array<int,3> ar = {p[j][0] - p[i][0] , p[j][1] - p[i][1] , p[j][2] - p[i][2]} ;
int g = abs(ar[0]) ; g = __gcd(g , abs(ar[1])) ; g = __gcd(g , abs(ar[2])) ;
ar[0] /= g ; ar[1] /= g ; ar[2] /= g;
mp[ar].push_back({g , j}) ;
}
for(auto &[u,v] : mp) {
sort(v.begin() , v.end()) ;
e[i][v[0].second] = 1 - check(i , v[0].second) ;
for(int j = 1;j < v.size();j++) e[i][v[j].second] = 1e7;
}
for(int j = 1;j <= n;j++ ) { e[i][j] *= -1 ; }
// printf("\n") ;
}
cout << -KM() << '\n' ;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3856kb
input:
7 0 0 0 1 0 0 1 0 0 -1 0 0 2 0 0 1 0 0 3 0 0 1 0 0 4 0 0 1 0 0 5 0 0 1 0 0 6 0 0 -1 0 0
output:
1002
result:
ok single line: '1002'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
4 66 45 10 73 39 36 95 14 26 47 84 59 14 66 89 89 36 78 16 27 94 79 24 24
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3564kb
input:
3 0 0 0 1 0 0 1 1 1 1 0 0 2 2 2 1 0 0
output:
1002
result:
ok single line: '1002'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
3 0 0 0 1 1 1 1 1 1 1 0 0 2 2 2 1 0 0
output:
1001
result:
ok single line: '1001'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
3 0 0 0 1 0 0 1 1 1 1 0 0 2 2 2 -1 -1 -1
output:
1001
result:
ok single line: '1001'
Test #6:
score: -100
Wrong Answer
time: 0ms
memory: 3604kb
input:
3 0 0 0 1 0 0 1 1 1 1 2 2 2 2 2 -1 -1 -1
output:
1001
result:
wrong answer 1st lines differ - expected: '1000', found: '1001'