QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#297809 | #6627. Line Town | chy12321 | 0 | 3ms | 16464kb | C++14 | 1.5kb | 2024-01-05 11:02:46 | 2024-01-05 11:02:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int N = 5e5 + 10;
constexpr ll inf = 0x3f3f3f3f3f3f3f3fll;
int n;
ll f[N][2], c[N][2];
struct Node {
int h, p;
bool operator<(const Node &rhs) const {
if (abs(h) == abs(rhs.h)) return h == rhs.h ? p < rhs.p : h < rhs.h;
return abs(h) < abs(rhs.h);
}
} a[N];
ll dp(int cur, int o) {
if (f[cur][o] != inf) return f[cur][o];
if (!cur || !a[cur].h) return 0;
if (a[cur].h * (o ? 1 : -1) <= 0) f[cur][o] = min(f[cur][o], c[cur][0] + dp(cur - 1, o ^ 1));
if (a[cur].h * (((cur & 1) ^ o) ? -1 : 1) >= 0) f[cur][o] = min(f[cur][o], c[cur][1] + dp(cur - 1, o));
return f[cur][o];
}
namespace BIT {
int c[N];
void fix(int x) {for (int i = x; i <= n; i += i & (-i)) c[i]++;}
int query(int x) {int res = 0; for (int i = x; i; i -= i & (-i)) res += c[i]; return res;}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(nullptr), cout.tie(nullptr);
cin >> n; for (int i = 1; i <= n; i++) a[i].p = i, cin >> a[i].h;
for (int i = 1; i <= n; i += 2) a[i].h = -a[i].h;
sort(a + 1, a + n + 1); int j = 1;
for (int i = 1; i <= n; i++) {
while(abs(a[j].h) < abs(a[i].h)) BIT::fix(a[j++].p);
c[a[i].p][0] = BIT::query(a[i].p - 1), c[a[i].p][1] = BIT::query(n) - BIT::query(a[i].p);
}
memset(f, 0x3f, sizeof(f));
cout << (dp(n, 0) < inf ? dp(n, 0) : -1);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 3
Accepted
time: 0ms
memory: 16412kb
input:
10 1 1 1 1 1 -1 -1 -1 1 -1
output:
-1
result:
ok 1 number(s): "-1"
Test #2:
score: -3
Wrong Answer
time: 3ms
memory: 16304kb
input:
10 1 1 1 1 1 1 -1 1 1 -1
output:
-1
result:
wrong answer 1st numbers differ - expected: '3', found: '-1'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Wrong Answer
Test #60:
score: 4
Accepted
time: 2ms
memory: 16448kb
input:
10 3 10 5 -9 7 2 -6 1 8 0
output:
-1
result:
ok 1 number(s): "-1"
Test #61:
score: -4
Wrong Answer
time: 2ms
memory: 16464kb
input:
10 -9 0 -1 7 5 10 6 3 2 -8
output:
14
result:
wrong answer 1st numbers differ - expected: '13', found: '14'
Subtask #6:
score: 0
Skipped
Dependency #5:
0%
Subtask #7:
score: 0
Skipped
Dependency #3:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%