QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#377028 | #5116. Contests | zhulexuan | WA | 0ms | 36988kb | C++14 | 3.9kb | 2024-04-04 20:54:04 | 2024-04-04 20:54:05 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf INT_MAX
#define fr(i,l,r) for (i=(l); i<=(r); i++)
#define rfr(i,l,r) for (i=(l); i>=(r); i--)
template<typename T>inline void read(T &n){
T w=1; n=0; char ch=getchar();
while (!isdigit(ch) && ch!=EOF){ if (ch=='-') w=-1; ch=getchar(); }
while (isdigit(ch) && ch!=EOF){ n=(n<<3)+(n<<1)+(ch&15); ch=getchar(); }
n*=w;
}
template<typename T>inline void write(T x){
if (x==0){ putchar('0'); return ; }
T tmp;
if (x>0) tmp=x;
else tmp=-x;
if (x<0) putchar('-');
char F[105];
long long cnt=0;
while (tmp){
F[++cnt]=tmp%10+48;
tmp/=10;
}
while (cnt) putchar(F[cnt--]);
}
#define Min(x,y) x = min(x,y)
#define Max(x,y) x = max(x,y)
//基础配置=================================================================================
const ll N = 100005 , M = 6;
ll n,m,ans=0,x,y,l,r,s,z;
ll a[N];
ll b[M][N];
ll pos[M][N];
struct infor{
ll t[M];
};
infor f[21][N];
void solve(ll id){
// printf("\n\n\nsolve : %lld\n",id);
ll i,j;
ll to[M][N];
fr(i,1,m)
fr(j,1,n) to[i][j] = pos[id][j];
fr(i,1,m)
rfr(j,n-1,1) Min(to[i][j],to[i][j+1]);
// to[n] = pos[id][n];
// rfr(i,n-1,1)
// to[i] = min(pos[id][i],to[i+1]);
// fr(i,1,n) printf("to[%lld] = %lld\n",i,to[i]);
fr(i,1,n){
ll v = inf;
fr(j,1,m){
ll x = to[j][pos[j][i]];
Min(v,x);
}
// printf("v = %lld\n",v);
f[0][i].t[id] = b[id][v];
}
}
ll calc(ll x,ll y){
if (x==y) return 0;
ll i,j,p;
ll Lm = inf;
ll t[M],tt[M];
fr(i,1,m) t[i] = x;
fr(i,1,m)
if (pos[i][t[i]]<=pos[i][y]) return 1;
ll k;
for (k=0; (1<<k)<=n; k++); k--;
ll ans = 0;
rfr(i,k,0){
// printf("\n i = %lld\n",i);
fr(j,1,m) tt[j] = t[j];
fr(j,1,m){
ll x = t[j];
fr(p,1,m){
ll y = f[i][x].t[p];
if (pos[p][y]<=pos[p][tt[p]]) tt[p] = y;
}
}
// fr(j,1,m) printf("tt[%lld] = %lld\n",j,tt[j]);
bool flag = false;
fr(j,1,m)
if (pos[j][tt[j]]<=pos[j][y]) flag = true;
fr(j,1,m)
if (pos[j][tt[j]]==pos[j][y]) Min( Lm , ans+(1<<i) );
if (!flag){
fr(j,1,m) t[j] = tt[j];
ans |= 1<<i;
}
// printf("ans = %lld\n",ans);
// fr(j,1,m) printf("t[%lld] = %lld\n",j,t[j]);
}
if (ans>n) return -1;
return min(Lm,ans+2);
}
int main(){
// freopen("a.in","r",stdin);
// freopen(".out","w",stdout);
ll i,j,k,p;
read(n); read(m);
fr(i,1,m)
fr(j,1,n) read(b[i][j]);
fr(i,1,m)
fr(j,1,n) pos[i][b[i][j]] = j;
fr(i,1,m) solve(i);
// fr(i,1,n){
// printf("f[0][%lld] : ",i);
// fr(j,1,m) printf("%lld ",f[0][i].t[j]);
// putchar('\n');
// }
for (i=1; (1<<i)<=n; i++){
fr(j,1,n) f[i][j] = f[i-1][j];
fr(j,1,n){
bool flag = false;
if (i==1 && j==4) flag = true;
fr(k,1,m){
ll x = f[i-1][j].t[k];
// printf("\nk = %lld , x = %lld\n",k,x);
fr(p,1,m){
y = f[i-1][x].t[p];
if (pos[p][y]<=pos[p][f[i][j].t[p]]) f[i][j].t[p] = y;
}
}
}
}
// fr(i,0,2){
// printf("\ni = %lld\n",i);
// fr(j,1,n){
// printf("f[%lld] : ",j);
// fr(k,1,m) printf("%lld ",f[i][j].t[k]);
// putchar('\n');
// }
// }
ll qt;
read(qt);
while (qt--){
read(x); read(y);
ll ans = calc(x,y);
write(ans); putchar('\n');
}
return 0;
}
//g++ a.cpp -o a -Wall -Wl,--stack=512000000
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 18472kb
input:
6 2 1 3 2 5 4 6 2 1 4 3 6 5 4 1 4 5 3 6 1 5 2
output:
1 2 5 3
result:
ok 4 number(s): "1 2 5 3"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 36988kb
input:
998 5 1 2 6 3 4 5 7 11 8 9 14 12 13 10 15 16 18 19 20 17 23 22 21 26 24 25 27 29 30 28 32 31 34 33 37 38 39 35 40 36 41 43 42 46 44 48 45 47 51 49 50 56 52 57 53 54 55 62 58 59 63 64 60 65 66 61 67 69 70 68 71 73 74 75 72 78 77 79 82 76 81 85 84 80 86 87 83 88 90 91 89 92 96 98 94 95 97 101 93 103 1...
output:
109 -1 1 1 1 31 1 182 -1 1 21 -1 31 1 1 1 108 1 1 1 1 -1 1 42 130 1 1 1 34 105 1 1 -1 45 42 68 1 1 1 1 1 1 1 1 18 -1 -1 1 1 240 -1 1 1 170 1 1 88 1 68 1 1 1 4 110 1 1 -1 47 107 1 1 19 178 1 1 1 168 -1 -1 1 35 -1 -1 39 223 1 -1 -1 1 1 1 72 -1 86 31 138 145 1 -1 1 1 2 1 1 1 16 -1 1 -1 1 96 -1 150 -1 1...
result:
wrong answer 1st numbers differ - expected: '94', found: '109'