# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
28707 | 탕탕탕! 핑거팁 니 맘을 겨눌게~ (#68) | 포도주 시음 (FXCUP2_wine) | C11 | 0 ms | 1116 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
#define LEFT_CHILD(x) (2*x + 1)
#define RIGHT_CHILD(x) (2*x + 2)
#define PARENT(x) ((x-1)/2)
#define SWAP(a,b) {int t; t = a; a=b; b=t;}
void HeapSort(int *base, int n);
void InitHeap(int *base, int n);
void MakeHeap(int *base, int n);
void HeapSort(int *base, int n)
{
int on = n;
InitHeap(base, n);
n--;
SWAP(base[0], base[n]);
while (n>1)
{
MakeHeap(base, n);
n--;
SWAP(base[0], base[n]);
}
}
void InitHeap(int *base, int n)
{
int pa = 0;
int now;
int i;
for (i = 1; i<n; i++)
{
now = i;
while (now>0)
{
pa = PARENT(now);
if (base[now]>base[pa])
{
SWAP(base[now], base[pa]);
now = pa;
}
else
{
break;
}
}
}
}
int FindMaxIndex(int *base, int n, int now);
void MakeHeap(int *base, int n)
{
int now = 0;
int mp = 0;
while (LEFT_CHILD(now) < n)
{
int mp = FindMaxIndex(base, n, now);
if (mp == now)
{
break;
}
SWAP(base[mp], base[now]);
now = mp;
}
}
int FindMaxIndex(int *base, int n, int now)
{
int lc = LEFT_CHILD(now);
int rc = RIGHT_CHILD(now);
if (rc >= n)
{
if (base[now]<base[lc])
{
return lc;
}
return now;
}
if (base[lc]<base[rc])
{
return rc;
}
return lc;
}
int main() {
int N, K, i, first = 0, end = 1;
int *wine;
long long int flavor = 0;
scanf("%d %d", &N, &K);
wine = (int*)malloc(sizeof(int)*N + 1);
for (i = 0; i < N; i++)
scanf("%d", &wine[i]);
HeapSort(wine, N);
for (i = 0; i < K; i = i + 2) {
if (i == 0) {
flavor = wine[N - end];
end++;
continue;
}
flavor += wine[N - end] - wine[0 + first];
end++;
first++;
}
printf("%lld", flavor);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |