UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#148315#1300. 奋斗的小强littlefox1003088ms87896kbC++112.6kb2022-06-11 16:47:342022-06-11 16:47:35

answer

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <stack>
#include <cmath>
#include <set>
#include <cstdlib>
#include <unordered_map>
#include <random>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
#define mp make_pair
#define lson tr[now].ls
#define rson tr[now].rs
#define pb push_back
#define debug() cout<<"qwq"<<endl
#define mem(i,a) memset(i,a,sizeof(i))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
#define i128 __int128
#define pii pair<int,int>
const db eps=1e-10;
const ll INF = 0x3f3f3f3f;
const int siev =1000000+5;
const int inf = 0x3f3f3f3f;
const int DMAX = 2000000 + 100;
const ll MOD = 32768;
const ll LMOD = 1000000007;
const int hmod = 202010923;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
template<class T> inline void print(T x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    int a[20];
    int cnt=0;
    do{
        a[++cnt]=x%10;
        x/=10;
    }while(x>0);
    for(int i=cnt;i>=1;i--){
        putchar(a[i]+'0');
    }
    putchar('\n');
    // puts("");
}
template<class T> inline void read(T &x){
    x=0;
    T f=1;
    char ch=getchar();
    while(ch<'0' || ch>'9'){
        if(ch=='-'){
            f=-1;
        }
        ch=getchar();
    }
    while(ch<='9' && ch>='0'){
        x=x*10+(ch-'0');
        ch=getchar();
    }
    x*=f;
}
vector<int> g[DMAX];
int q;
bool vst[DMAX];
int dis[DMAX];
void Dij(int S){
    for(int i=0;i<=1048576;i++){
        dis[i]=1e9;
        vst[i]=0;
    }
    dis[S]=0,vst[S]=1;
    priority_queue<pair<int,int> > q;
    q.push(mp(0,S));
    while(!q.empty()){
        int u=q.top().second;
        q.pop();
        vst[u]=0;
        for(auto v:g[u]){
            if(dis[v]>dis[u]+1){
                dis[v]=dis[u]+1;
                if(!vst[v]){
                    vst[v]=1;
                    q.push(mp(-dis[v],v));
                }
            }
        }
    }
}
int main(){
    read(q);
    for(int i=0;i<=1048576;i++){
        if(i!=0){
            g[i].pb(i-1);
        }
        if(i!=1048576){
            g[i].pb(i+1);
        }
        if(i*2<=1048576){
            g[i].pb(i*2);
        }
    }
    Dij(0);
    int x;
    while(q--){
        read(x);
        x=abs(x);
        cout<<dis[x]<<endl;
    }
    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 282ms
memory: 87892kb

input:

8
0 1 2 3 4 5 6 7

output:

0
1
2
3
3
4
4
5

result:

ok 8 lines

Test #2:

score: 10
Accepted
time: 339ms
memory: 87896kb

input:

100000
660545 651171 -241918 31365 709966 -487671 883217 646708 182939 841848 560067 525729 74106 74...

output:

25
26
23
21
29
25
27
26
26
26
26
25
22
29
25
24
25
28
28
22
27
22
25
28
25
21
25
27
25
23
19
26
22
2...

result:

ok 100000 lines

Test #3:

score: 10
Accepted
time: 305ms
memory: 87892kb

input:

10
0 1 2 3 4 5 6 7 8 9

output:

0
1
2
3
3
4
4
5
4
5

result:

ok 10 lines

Test #4:

score: 10
Accepted
time: 286ms
memory: 87896kb

input:

74
59 31 73 45 79 24 10 41 66 93 43 88 4 28 30 41 13 4 70 10 58 61 34 100 79 17 36 98 27 13 68 11 34...

output:

9
7
9
9
9
6
5
8
8
10
9
9
3
7
7
8
6
3
9
5
9
9
7
9
9
6
7
9
8
6
8
6
7
8
8
8
7
8
9
9
8
10
8
8
9
9
9
2
9
...

result:

ok 74 lines

Test #5:

score: 10
Accepted
time: 286ms
memory: 87896kb

input:

37
26 70 16 95 30 2 18 96 6 5 52 99 89 24 6 83 53 67 17 38 39 45 2 98 72 29 38 59 78 98 95 5 10 32 4...

output:

7
9
5
9
7
2
6
8
4
4
8
10
10
6
4
10
9
9
6
8
8
9
2
9
8
8
8
9
9
9
9
4
5
6
8
9
7

result:

ok 37 lines

Test #6:

score: 10
Accepted
time: 283ms
memory: 87896kb

input:

99
43 100 69 13 61 58 95 9 96 69 14 31 7 63 43 66 83 53 68 22 96 13 72 2 91 32 39 58 17 91 41 80 36 ...

output:

9
9
9
6
9
9
9
5
8
9
6
7
5
8
9
8
10
9
8
7
8
6
8
2
10
6
8
9
6
10
8
8
7
5
9
10
8
6
9
6
10
9
4
8
6
5
6
9...

result:

ok 99 lines

Test #7:

score: 10
Accepted
time: 313ms
memory: 87892kb

input:

73488
-933898 341781 241193 -874295 762473 464497 -833124 64418 -734026 202427 -917893 -892473 -6540...

output:

25
26
25
29
28
27
28
21
29
25
26
28
24
23
28
25
25
23
27
24
22
21
26
26
29
26
26
24
27
22
27
22
24
2...

result:

ok 73488 lines

Test #8:

score: 10
Accepted
time: 315ms
memory: 87896kb

input:

84885
387808 -578917 980921 979284 772225 -637485 736541 146989 41416 -339437 551256 -878301 449017 ...

output:

24
28
26
27
25
28
28
24
20
26
27
29
26
30
26
10
24
25
23
24
25
28
25
21
19
26
28
28
26
25
25
26
26
2...

result:

ok 84885 lines

Test #9:

score: 10
Accepted
time: 325ms
memory: 87896kb

input:

97397
949937 -746601 -373256 710443 928751 684001 926646 646414 345781 140533 188381 723083 -744303 ...

output:

27
28
24
29
27
25
27
26
27
24
23
27
27
26
24
24
28
28
28
26
26
26
27
21
25
28
24
26
26
30
28
26
23
2...

result:

ok 97397 lines

Test #10:

score: 10
Accepted
time: 354ms
memory: 87892kb

input:

71066
533076 285249 30021 -331757 891975 981036 974471 789793 -2023 888713 432916 276647 606652 4368...

output:

25
25
22
24
28
26
27
26
15
27
26
25
25
28
28
26
26
26
26
24
26
21
28
26
23
27
21
27
28
27
23
27
23
2...

result:

ok 71066 lines