QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#691810#5137. TowerIsacBieberTL 4766ms5060kbC++232.1kb2024-10-31 13:04:292024-10-31 13:04:29

Judging History

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

  • [2024-10-31 13:04:29]
  • 评测
  • 测评结果:TL
  • 用时:4766ms
  • 内存:5060kb
  • [2024-10-31 13:04:29]
  • 提交

answer

#include <bits/stdc++.h>
// #include<bits/extc++.h>
#define endl '\n'
#define eps 1e-7
#define pb push_back
#define eb emplace_back
#define debug(x) cerr<<#x<<": "<<x<<'\n';
// using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair <ll,ll>;
using pii = pair <int,int>;
using ull = unsigned long long;
const int N = 5e2 + 5, MOD = 1e9 + 7;
mt19937_64 rnd (chrono::steady_clock::now().time_since_epoch().count());
int n, m, a[N], f[N][N];
int g(int idx,int x)
{
    if(a[idx]<x) return x - a[idx];
    else if(a[idx]==x) return 0;
    else
    {
        int t = a[idx], res = abs(t-x);
        int cnt = 0;
        while(1)
        {
            t >>= 1, cnt ++;
            if(t!=0) res = min(res,abs(t-x)+cnt);
            else break;
        } 
        return res;
    }
}
int cal(int x)
{
    for(int i=0;i<=n;i++)
        for(int j=0;j<=m;j++)
            f[i][j] = 1e9;
    f[0][0] = 0;
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<=m;j++)
        {
            if(j) f[i][j] = f[i-1][j-1];
            f[i][j] = min(f[i][j],f[i-1][j]+g(i,x));
        }
    }
    return f[n][m];
}
void isac()
{
    cin>>n>>m;
    vector <int> v;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        int x = a[i];
        while(x)
        {
            for(int j=max(1,x-10);j<=x+10;j++) v.eb(j);
            x >>= 1;
        }
    } 
    sort(v.begin(),v.end());
    v.resize(unique(v.begin(),v.end())-v.begin());
    int l = 0, r = v.size()-1;
    while(l+600<r)
    {
        int lmid = l + (r-l+1)/3, rmid = r - (r-l+1)/3;
        if(cal(v[lmid])>cal(v[rmid])) l = lmid;
        else r = rmid;
    }
    int ans = 1e9;
    for(int i=l;i<=r;i++) ans = min(ans,cal(v[i]));
    cout<<ans<<'\n';    
}
int main(void)
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int T = 1;
    cin >> T;
    while (T--) isac();
    return 0;
}
/*
    Zena youth together.
    Such a pretty name.
    There will be a long story.
    Time will tell all.
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3664kb

input:

3
2 0
2 6
5 0
1 2 3 4 5
5 3
1 2 3 4 5

output:

2
4
1

result:

ok 3 number(s): "2 4 1"

Test #2:

score: 0
Accepted
time: 114ms
memory: 4700kb

input:

10
272 118
11 14 49 94 71 62 46 45 74 22 15 36 7 37 27 35 96 85 75 78 76 64 23 59 17 35 71 28 96 82 5 66 2 48 57 31 88 10 61 73 79 23 19 52 39 76 48 98 5 39 48 51 90 90 60 27 47 24 24 56 48 27 39 21 38 18 20 9 62 83 47 15 51 22 73 74 7 80 64 60 86 74 59 7 84 38 99 31 42 60 52 41 63 88 59 90 77 40 68...

output:

454
3
436
108
570
636
994
227
656
50

result:

ok 10 numbers

Test #3:

score: 0
Accepted
time: 102ms
memory: 4756kb

input:

10
133 89
20 70 6 45 4 72 38 7 18 1 82 39 69 85 5 36 1 62 30 47 68 55 7 41 7 42 7 61 11 82 2 80 80 93 29 30 42 58 73 26 99 67 60 94 61 46 47 54 44 50 35 88 61 17 23 97 90 28 16 47 75 35 28 14 42 63 26 40 95 58 26 25 26 83 93 56 17 27 7 90 91 28 53 49 47 84 55 52 11 34 14 74 40 65 84 32 99 46 1 21 31...

output:

88
1361
128
246
29
83
3
677
96
382

result:

ok 10 numbers

Test #4:

score: 0
Accepted
time: 574ms
memory: 4744kb

input:

10
500 50
67 93 11 58 54 40 37 3 92 96 91 20 46 5 21 43 3 2 7 47 27 81 14 53 86 21 46 51 86 22 42 14 52 38 42 25 34 29 84 42 43 96 11 100 27 60 48 15 13 69 58 16 14 58 17 94 8 71 39 38 25 37 100 58 99 56 65 84 94 63 25 34 13 73 83 83 69 60 70 15 15 90 7 11 88 69 13 26 99 28 16 97 32 40 76 62 41 5 9 ...

output:

1608
169
144
983
1087
1317
882
75
32
1259

result:

ok 10 numbers

Test #5:

score: 0
Accepted
time: 4766ms
memory: 5060kb

input:

10
500 414
503 505 103 380 946 153 952 386 890 306 522 147 499 784 643 121 264 344 549 72 299 314 45 688 97 747 442 528 752 830 335 78 159 218 748 331 259 375 479 883 202 402 595 738 430 184 874 762 864 743 733 209 821 616 868 543 314 161 100 638 439 943 732 962 243 776 803 423 749 367 731 594 993 9...

output:

338
2653
2857
809
1626
332
1328
490
1044
854

result:

ok 10 numbers

Test #6:

score: -100
Time Limit Exceeded

input:

10
500 497
4249 4173 5055 5942 4090 1754 1523 5273 758 3522 8827 3775 6331 2729 8699 8797 5977 5435 6320 9188 5889 6562 9809 7595 6104 7480 2116 8715 2823 1990 425 5811 3347 2618 6119 3948 7172 7095 8017 5943 2036 87 6661 2099 885 3035 3745 8362 3919 2675 1314 8123 7254 328 106 2546 7540 9303 4333 9...

output:


result: