Submission #68729928
Source Code Expand
Copy
#include <stdio.h>#include <stdlib.h>#include <inttypes.h>int cmp(const void* x, const void* y) {int a = *(const int*)x, b = *(const int*)y;return a < b ? -1 : a > b;}int N, Q;int A[312345];int B[312345];int64_t sum[312345];int main(void) {int i;if (scanf("%d%d", &N, &Q) != 2) return 1;for (i = 0; i < N; i++) {if (scanf("%d", &A[i]) != 1) return 1;}
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
int cmp(const void* x, const void* y) {
int a = *(const int*)x, b = *(const int*)y;
return a < b ? -1 : a > b;
}
int N, Q;
int A[312345];
int B[312345];
int64_t sum[312345];
int main(void) {
int i;
if (scanf("%d%d", &N, &Q) != 2) return 1;
for (i = 0; i < N; i++) {
if (scanf("%d", &A[i]) != 1) return 1;
}
for (i = 0; i < Q; i++) {
if (scanf("%d", &B[i]) != 1) return 1;
}
qsort(A, N, sizeof(*A), cmp);
sum[0] = A[0];
for (i = 1; i < N; i++) {
sum[i] = sum[i - 1] + A[i];
}
for (i = 0; i < Q; i++) {
if(A[N - 1] < B[i]) {
puts("-1");
} else {
int l = -1, ge = N;
int64_t meow;
while (l + 1 < ge) {
int m = l + (ge - l) / 2;
if (A[m] < B[i]) l = m; else ge = m;
}
meow = 0;
if (l >= 0) meow += sum[l];
meow += (int64_t)(B[i] - 1) * (N - ge);
printf("%" PRId64 "\n", meow + 1);
}
}
return 0;
}
/*
どの種類も b-1 個以下しかなければ、敗北する
b 個以上ある種類があれば、勝利できる
b-1 個以下しかない種類 → 全部選ぶ
b 個以上ある種類 → b-1 個選ぶ
これで全部足してx 以下 → b 個以上ある種類を作らずにすむ
otherwise → もう1個以上足さなければならず、b 個以上ある種類ができる
*/
Submission Info
| Submission Time | |
|---|---|
| Task | C - Flush |
| User | mikecat |
| Language | C (gcc 12.2.0) |
| Score | 350 |
| Code Size | 1381 Byte |
| Status | AC |
| Exec Time | 148 ms |
| Memory | 8836 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 350 / 350 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00-sample-01.txt, 00-sample-02.txt |
| All | 00-sample-01.txt, 00-sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00-sample-01.txt | AC | 1 ms | 1592 KiB |
| 00-sample-02.txt | AC | 1 ms | 1700 KiB |
| 01-01.txt | AC | 6 ms | 1848 KiB |
| 01-02.txt | AC | 6 ms | 1724 KiB |
| 01-03.txt | AC | 7 ms | 1788 KiB |
| 01-04.txt | AC | 148 ms | 8804 KiB |
| 01-05.txt | AC | 66 ms | 6852 KiB |
| 01-06.txt | AC | 104 ms | 8792 KiB |
| 01-07.txt | AC | 118 ms | 8836 KiB |
| 01-08.txt | AC | 110 ms | 8728 KiB |
| 01-09.txt | AC | 138 ms | 8272 KiB |