QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#329249#4935. Exchange Bottleneckouo#TL 2ms3708kbC++201.2kb2024-02-16 15:03:232024-02-16 15:03:24

Judging History

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

  • [2024-02-16 15:03:24]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:3708kb
  • [2024-02-16 15:03:23]
  • 提交

answer

#include <bits/stdc++.h>
#define F first
#define S second

using namespace std;

int n;
int arr[1000005];
int dp[1000005];
vector<int> gh[1000005];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
int ans;

void bfs() {
    fill(dp+2, dp+n+1, 1e9);
    pq.push(make_pair(dp[1], 1));
    while (!pq.empty()) {
        pair<int, int> now = pq.top();
        pq.pop();
        if (dp[now.S] == now.F) {
            dp[now.S] = now.F;
            for (auto next : gh[now.S]) {
                if (dp[next] > now.F + 1) {
                    dp[next] = now.F + 1;
                    pq.push(make_pair(dp[next], next));
                }
            }
        }
    }
    for (int i = 1; i < n+1; i++) {
        ans = max(ans, dp[i]);
    }
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n;
    for (int i = 1; i < n; i++) {
        cin >> arr[i];
        if (arr[i]) {
            for (int j = i; j > 0; j--) {
                gh[i+1].push_back(j);
                gh[j].push_back(i+1);
            }
        }else {
            gh[i].push_back(i+1);
            gh[i+1].push_back(i);
        }
    }
    bfs();
    cout << ans;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3604kb

input:

5
1 0 1 0

output:

2

result:

ok single line: '2'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3584kb

input:

7
1 1 1 1 1 1

output:

1

result:

ok single line: '1'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3708kb

input:

2
0

output:

1

result:

ok single line: '1'

Test #4:

score: -100
Time Limit Exceeded

input:

90580
1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 ...

output:


result: