QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#297798 | #6627. Line Town | chy12321 | 0 | 1ms | 9764kb | C++14 | 1.4kb | 2024-01-05 10:46:08 | 2024-01-05 10:46:08 |
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 ? h < rhs.h : p < rhs.p;
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) return 0;
if (a[cur].h * (o == 1 ? -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[i][0] = BIT::query(a[i].p - 1), c[i][1] = BIT::query(n) - BIT::query(a[i].p);
}
memset(f, 0x3f, sizeof(0x3f));
cout << (dp(n, 1) < inf ? dp(n, 1) : -1);
return 0;
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 9640kb
input:
10 1 1 1 1 1 -1 -1 -1 1 -1
output:
0
result:
wrong answer 1st numbers differ - expected: '-1', found: '0'
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: 0
Wrong Answer
time: 1ms
memory: 9764kb
input:
10 3 10 5 -9 7 2 -6 1 8 0
output:
0
result:
wrong answer 1st numbers differ - expected: '-1', found: '0'
Subtask #6:
score: 0
Skipped
Dependency #5:
0%
Subtask #7:
score: 0
Skipped
Dependency #3:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%