結果

問題 No.1890 Many Sequences Sum Queries
ユーザー trineutron
提出日時 2022-04-04 21:59:02
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 814 bytes
コンパイル時間 1,860 ms
コンパイル使用メモリ 193,868 KB
最終ジャッジ日時 2025-01-28 15:06:10
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 1 WA * 24
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:5:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    5 |         scanf("%d%d", &n, &q);
      |         ~~~~~^~~~~~~~~~~~~~~~
main.cpp:8:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    8 |                 scanf("%d", &a[i]);
      |                 ~~~~~^~~~~~~~~~~~~
main.cpp:20:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   20 |                 scanf("%d", &s);
      |                 ~~~~~^~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

int main() {
	int n, q, len = 0;
	scanf("%d%d", &n, &q);
	int a[100];
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		len += a[i];
	}
	long c_idx[101], c[101];
	c_idx[0] = 0;
	c[0] = 0;
	for (int i = 0; i < n; i++) {
		c_idx[i + 1] = c_idx[i] + a[i];
		c[i + 1] = c[i] + a[i] * (a[i] + 1) / 2;
	}
	for (int i = 0; i < q; i++) {
		int s;
		scanf("%d", &s);
		if (s > c[n]) {
			puts("-1");
			continue;
		}
		int l_idx = std::upper_bound(c, c + n + 1, s) - c;
		int l = c_idx[std::max(0, l_idx - 2)], r = c_idx[l_idx - 1];
		while (r - l > 1) {
			int mid = (l + r) / 2;
			int k = std::upper_bound(c_idx, c_idx + n + 1, mid) - c_idx;
			long d = mid - c_idx[k - 1];
			if (c[k - 1] + d * (d + 1) / 2 >= s) {
				r = mid;
			} else {
				l = mid;
			}
		}
		printf("%d\n", r);
	}
}
0