QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#100303#4647. Wavy Treeeuphria#AC ✓275ms18916kbC++201.4kb2023-04-25 15:28:432023-04-25 15:28:45

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-25 15:28:45]
  • 评测
  • 测评结果:AC
  • 用时:275ms
  • 内存:18916kb
  • [2023-04-25 15:28:43]
  • 提交

answer

//B站关注 艾尔莎 喵
//All OK
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define ios ios::sync_with_stdio(0);cin.tie(nullptr);cout.tie(0);cout << fixed << setprecision(9)
#define INF 1ll*1e10
#define LNF (1ll << 60)
#define PII pair<int, int>
#define PI acos(-1)
#define ll long long
// #define ll __int128 
#define int ll
#define endl '\n'
using namespace std;

const int N = 1e6 + 10;
int n;
ll a[N], b[N]; 

void solve(){
    ll ans = LNF;
    cin >> n;
    for(int i = 1; i <= n; i ++)
        cin >> a[i];
    b[1] = a[1];
    ll res = 0;
    for(int i = 2; i <= n; i ++){
        b[i] = a[i];
        if(i % 2 && b[i - 1] <= b[i])
            res += b[i] - b[i - 1] + 1, b[i] = b[i - 1] - 1;
        else if(i % 2 == 0 && b[i - 1] >= b[i])
            res += b[i - 1] - b[i] + 1, b[i] = b[i - 1] + 1;
    }
    ans = min(ans, res);

    res = 0;
    for(int i = 2; i <= n; i ++){
        b[i] = a[i];
        if(i % 2 && b[i - 1] >= b[i])
            res += b[i - 1] - b[i] + 1, b[i] = b[i - 1] + 1;
        else if(i % 2 == 0 && b[i - 1] <= b[i])
            res += b[i] - b[i - 1] + 1, b[i] = b[i - 1] - 1;
    }
    cout << min(ans, res) << endl;
}   

signed main(){
    ios;

    // init();
    int _ = 1;
    cin >> _;
    for(int __ = 1; __ <= _; __ ++){
        // cout << "Case " << __ << ": ";
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 275ms
memory: 18916kb

input:

1000
100000
421811673 163389757 362117917 679743722 325614744 632629964 980475526 100505548 936347880 928352466 107919751 93875425 790807861 179598820 105172240 143240481 781447901 225995680 495635141 806359893 572331818 169913092 230473678 658931041 285082875 709115692 204427990 675460241 498216193...

output:

7254088195794
10532337049187
42013773340
93984668312079
304864037717
24280801657
27126085272
36045025116
3661199008116
6128224088957
27163006737
4061518843424
13450250708
39002357066
27114173348
4819422928
10116511952
29694865171
300714954028
44450326647
46689505040
19307568489
31476115190
306968957...

result:

ok 1000 lines