이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
#include <string.h>
#define N 300000
#define A 300000
#define N_ 23
int min(int a, int b) { return a < b ? a : b; }
long long f(int l, int r) {
return (long long) (r - l + 1) * (r - l + 2) / 2;
}
int aa[N];
int solve(int l, int r) {
static int kk[A + 1], nn[1 << N_], dp[1 << N_];
int n, i, a, b;
memset(kk, 0, (A + 1) * sizeof *kk);
for (i = l; i <= r; i++)
kk[aa[i]]++;
n = 0;
for (a = 0; a <= A; a++)
if (kk[a] > 0)
kk[n++] = kk[a];
memset(dp, 0x3f, (1 << n) * sizeof *dp);
nn[0] = 0;
for (i = 0; i < n; i++)
nn[1 << i] = kk[i];
for (b = 1; b < 1 << n; b++)
nn[b] = nn[b & b - 1] + nn[b & -b];
dp[0] = 0;
for (b = 0; b < 1 << n; b++)
for (i = 0; i < n; i++)
if ((b & 1 << i) == 0)
dp[b | 1 << i] = min(dp[b | 1 << i], dp[b]
+ (f(l, r) - f(l, l + nn[b] - 1) - f(l + nn[b | 1 << i], r)));
return dp[(1 << n) - 1];
}
int main() {
int n, q, i;
scanf("%d%d", &n, &q);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
while (q--) {
int l, r;
scanf("%d%d", &l, &r), l--, r--;
printf("%d\n", solve(l, r));
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
diversity.c: In function 'solve':
diversity.c:32:20: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
32 | nn[b] = nn[b & b - 1] + nn[b & -b];
| ~~^~~
diversity.c: In function 'main':
diversity.c:45:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%d%d", &n, &q);
| ^~~~~~~~~~~~~~~~~~~~~
diversity.c:47:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
diversity.c:51:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%d%d", &l, &r), l--, r--;
| ^~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |