QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#377033#5116. ContestszhulexuanWA 0ms37044kbC++143.9kb2024-04-04 21:00:312024-04-04 21:00:32

Judging History

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

  • [2024-04-04 21:00:32]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:37044kb
  • [2024-04-04 21:00:31]
  • 提交

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][b[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: 18480kb

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

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:

94
-1
1
1
1
21
1
151
-1
1
18
-1
27
1
1
1
79
1
1
1
1
-1
1
37
98
1
1
1
29
90
1
1
-1
38
34
59
1
1
1
1
1
1
1
1
16
-1
-1
1
1
193
-1
1
1
139
1
1
76
1
60
1
1
1
4
88
1
1
-1
37
91
1
1
16
146
1
1
1
134
-1
-1
1
27
-1
-1
31
179
1
-1
-1
1
1
1
55
-1
74
23
116
121
1
-1
1
1
2
1
1
1
11
-1
1
-1
1
75
-1
126
-1
1
1
6
1...

result:

wrong answer 8th numbers differ - expected: '153', found: '151'