QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#132688 | #6739. Teleport | de_sousa# | WA | 48ms | 57636kb | C++17 | 4.2kb | 2023-07-31 02:46:55 | 2023-07-31 02:46:56 |
Judging History
answer
#pragma GCC optimize("-O3","unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x),end(x)
#define trav(a, b) for (auto a : b)
#define lld long long
using i64 = long long;
struct DisjointSet {
const int N;
vector<int> par;
vector<int> sz;
vector<int> mx;
DisjointSet(int _n) : N(_n) {
par = sz = mx = vector<int> (N);
for (int i = 0; i < N; i++) {
par[i] = i;
sz[i] = 1;
mx[i] = i;
}
}
int root(int x) {
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
void join(int a, int b) {
a = root(a);
b = root(b);
if (a == b) return;
if (sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b];
if(mx[a]==-1||mx[b]==-1)mx[a]=-1;
else mx[a] = max(mx[a], mx[b]);
par[b] = a;
}
void sn(int a)
{
mx[root(a)]=-1;
}
int f(int a)
{
return mx[root(a)];
}
};
void solve() {
int n,m,k;
cin>>n>>k;
m=n;
vector<string> a(n);
for(auto&i:a){
cin>>i;
}
vector<array<int,2>> Q;
Q.push_back({0,0});
vector<vector<int>> distance(n,vector<int>(m,-1));
vector<vector<int>> visited(n,vector<int>(m,0));
distance[0][0]=0;
vector<int> rep(n+m);
vector<vector<int>> next(n+m,vector<int>(n,-1));
vector<vector<int>> prev(n+m,vector<int>(n,-1));
vector<DisjointSet> dsu(n+m,DisjointSet(n));
auto id=[&](int x,int y)
{
int val=x-y+m-1;
assert(val>=0);
return val;
};
for(int i=0;i<n;i++){
rep[id(i,0)]=i;
}
for(int j=0;j<m;j++){
rep[id(0,j)]=j;
}
for(int i=0;i<n;i++){
int tmp=-1;
for(int j=0;true;j++){
if(i+j>=n)break;
if(a[i+j][j]=='*'){
visited[i+j][j]=true;
continue;
}
if(tmp!=-1)next[id(i,0)][tmp]=j;
prev[id(i,0)][j]=tmp;
tmp=j;
}
}
for(int i=1;i<m;i++){
int tmp=-1;
for(int j=0;true;j++){
if(i+j>=n)break;
if(a[j][i+j]=='*'){
visited[j][i+j]=true;
continue;
}
if(tmp!=-1)next[id(0,i)][tmp]=j;
prev[id(0,i)][j]=tmp;
tmp=j;
}
}
auto remove=[&](int x,int y)
{
int tmpid=id(x,y);
int tmp=(x+y-rep[tmpid])/2;
if(prev[tmpid][tmp]!=-1)
next[tmpid][prev[tmpid][tmp]]=next[tmpid][tmp];
if(next[tmpid][tmp]!=-1)
prev[tmpid][next[tmpid][tmp]]=prev[tmpid][tmp];
if(next[tmpid][tmp]==-1)dsu[tmpid].sn(tmp);
else dsu[tmpid].join(tmp,next[tmpid][tmp]);
visited[x][y]=1;
};
remove(0,0);
int Qi=0;
while(Qi<Q.size()){
auto[i,j]=Q[Qi];
// cout<<i<<' '<<j<<' '<<distance[i][j]<<'\n';
if(i==n-1&&j==m-1){
cout<<distance[i][j]<<'\n';
return;
}
if(i>0&&!visited[i-1][j]){
distance[i-1][j]=1+distance[i][j];
Q.push_back({i-1,j});
remove(i-1,j);
}
if(i<n-1&&!visited[i+1][j]){
distance[i+1][j]=1+distance[i][j];
Q.push_back({i+1,j});
remove(i+1,j);
}
if(j>0&&!visited[i][j-1]){
distance[i][j-1]=1+distance[i][j];
Q.push_back({i,j-1});
remove(i,j-1);
}
if(j<m-1&&!visited[i][j+1]){
distance[i][j+1]=1+distance[i][j];
Q.push_back({i,j+1});
remove(i,j+1);
}
{
int tmpid=id(i,j);
int iter=(i+j-rep[tmpid])/2;
int saveval=(i+j-rep[tmpid])/2;
iter=dsu[tmpid].f(iter);
while(iter!=-1&&iter-saveval<=k/2){
distance[i+iter-saveval][j+iter-saveval]=1+distance[i][j];
Q.push_back({i+iter-saveval,j+iter-saveval});
int tmpiter=next[tmpid][iter];
remove(i+iter-saveval,j+iter-saveval);
iter=tmpiter;
}
}
if(j<m-1){
int tmpid=id(j+1,i);
int iter=(j+1+i-rep[tmpid])/2;
int saveval=(j+1+i-rep[tmpid])/2;
iter=dsu[tmpid].f(iter);
while(iter!=-1&&iter-saveval<=(k-1)/2){
distance[j+1+iter-saveval][i+iter-saveval]=1+distance[i][j];
Q.push_back({j+1+iter-saveval,i+iter-saveval});
int tmpiter=next[tmpid][iter];
remove(j+1+iter-saveval,i+iter-saveval);
iter=tmpiter;
}
}
Qi++;
}
cout<<distance[n-1][m-1]<<'\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tt = 1;
// cin >> tt;
while (tt--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3488kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3528kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: -100
Wrong Answer
time: 48ms
memory: 57636kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
546
result:
wrong answer 1st numbers differ - expected: '540', found: '546'