new file: P2048.cpp
This commit is contained in:
1 parent
e7784095e0
commit
9ec0003de0
2 files changed
+129
No files matched your search
@@ -0,0 +1,72 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
const int MAXN = 500005;
|
||||
const int LOG = 20;
|
||||
int n, k, L, R;
|
||||
long long pre[MAXN];
|
||||
int lg2[MAXN];
|
||||
int st[LOG][MAXN];
|
||||
int query(int l, int r) {
|
||||
int len = r - l + 1;
|
||||
int t = lg2[len];
|
||||
int x = st[t][l];
|
||||
int y = st[t][r - (1 << t) + 1];
|
||||
return pre[x] <= pre[y] ? x : y;
|
||||
}
|
||||
struct Node {
|
||||
long long val;
|
||||
int r, ql, qr, pos;
|
||||
bool operator<(const Node &other) const { return val < other.val; }
|
||||
};
|
||||
int main() {
|
||||
scanf("%d %d %d %d", &n, &k, &L, &R);
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
int x;
|
||||
scanf("%d", &x);
|
||||
pre[i] = pre[i - 1] + x;
|
||||
}
|
||||
int m = n + 1;
|
||||
lg2[1] = 0;
|
||||
for (int i = 2; i <= m; ++i) {
|
||||
lg2[i] = lg2[i / 2] + 1;
|
||||
}
|
||||
for (int i = 0; i < m; ++i) {
|
||||
st[0][i] = i;
|
||||
}
|
||||
for (int j = 1; (1 << j) <= m; ++j) {
|
||||
for (int i = 0; i + (1 << j) <= m; ++i) {
|
||||
int x = st[j - 1][i];
|
||||
int y = st[j - 1][i + (1 << (j - 1))];
|
||||
st[j][i] = (pre[x] <= pre[y] ? x : y);
|
||||
}
|
||||
}
|
||||
priority_queue<Node> pq;
|
||||
for (int r = 1; r <= n; ++r) {
|
||||
int ql = max(0, r - R);
|
||||
int qr = r - L;
|
||||
if (ql <= qr) {
|
||||
int pos = query(ql, qr);
|
||||
pq.push({pre[r] - pre[pos], r, ql, qr, pos});
|
||||
}
|
||||
}
|
||||
long long ans = 0;
|
||||
for (int cnt = 0; cnt < k && !pq.empty(); ++cnt) {
|
||||
Node cur = pq.top();
|
||||
pq.pop();
|
||||
ans += cur.val;
|
||||
int r = cur.r;
|
||||
int ql = cur.ql;
|
||||
int qr = cur.qr;
|
||||
int pos = cur.pos;
|
||||
if (ql <= pos - 1) {
|
||||
int np = query(ql, pos - 1);
|
||||
pq.push({pre[r] - pre[np], r, ql, pos - 1, np});
|
||||
}
|
||||
if (pos + 1 <= qr) {
|
||||
int np = query(pos + 1, qr);
|
||||
pq.push({pre[r] - pre[np], r, pos + 1, qr, np});
|
||||
}
|
||||
}
|
||||
printf("%lld\n", ans);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
# P2048 [NOI2010] 超级钢琴
|
||||
|
||||
## 题目描述
|
||||
|
||||
小 Z 是一个小有名气的钢琴家,最近 C 博士送给了小 Z 一架超级钢琴,小 Z 希望能够用这架钢琴创作出世界上最美妙的音乐。
|
||||
|
||||
这架超级钢琴可以弹奏出 $n$ 个音符,编号为 $1$ 至 $n$。第 $i$ 个音符的美妙度为 $A_i$,其中 $A_i$ 可正可负。
|
||||
|
||||
一个“超级和弦”由若干个编号连续的音符组成,包含的音符个数不少于 $L$ 且不多于 $R$。我们定义超级和弦的美妙度为其包含的所有音符的美妙度之和。两个超级和弦被认为是相同的,当且仅当这两个超级和弦所包含的音符集合是相同的。
|
||||
|
||||
小 Z 决定创作一首由 $k$ 个超级和弦组成的乐曲,为了使得乐曲更加动听,小 Z 要求该乐曲由 $k$ 个不同的超级和弦组成。我们定义一首乐曲的美妙度为其所包含的所有超级和弦的美妙度之和。小 Z 想知道他能够创作出来的乐曲美妙度最大值是多少。
|
||||
|
||||
## 输入格式
|
||||
|
||||
输入第一行包含四个正整数 $n, k, L, R$。其中 $n$ 为音符的个数,$k$ 为乐曲所包含的超级和弦个数,$L$ 和 $R$ 分别是超级和弦所包含音符个数的下限和上限。
|
||||
|
||||
接下来 $n$ 行,每行包含一个整数 $A_i$,表示按编号从小到大每个音符的美妙度。
|
||||
|
||||
## 输出格式
|
||||
|
||||
输出只有一个整数,表示乐曲美妙度的最大值。
|
||||
|
||||
## 输入输出样例 #1
|
||||
|
||||
### 输入 #1
|
||||
|
||||
```
|
||||
4 3 2 3
|
||||
3
|
||||
2
|
||||
-6
|
||||
8
|
||||
```
|
||||
|
||||
### 输出 #1
|
||||
|
||||
```
|
||||
11
|
||||
```
|
||||
|
||||
## 说明/提示
|
||||
|
||||
### 样例解释
|
||||
|
||||
共有 $5$ 种不同的超级和弦:
|
||||
|
||||
1. 音符 $1 \sim 2$,美妙度为 $3+2=5$;
|
||||
2. 音符 $2 \sim 3$,美妙度为 $2+(-6)=-4$;
|
||||
3. 音符 $3 \sim 4$,美妙度为 $(-6)+8=2$;
|
||||
4. 音符 $1 \sim 3$,美妙度为 $3+2+(-6)=-1$;
|
||||
5. 音符 $2 \sim 4$,美妙度为 $2+(-6)+8=4$。
|
||||
|
||||
最优方案为:乐曲由和弦 $1,3,5$ 组成,美妙度为 $5+2+4=11$。
|
||||
|
||||

|
||||
|
||||
所有数据满足:$-1000 \leq A_i \leq 1000$,$1 \leq L \leq R \leq n$ 且保证一定存在满足要求的乐曲。
|
||||
Reference in new issue
Block a user