提出 #3989646


ソースコード 拡げる

#include <bits/stdc++.h>

using namespace std;

#define FORE(i, a) for (auto i = a.begin(); i != a.end(); ++i)
#define REPU(i, a, b) for (int i = (a); i < (b); ++i)
#define REPD(i, a, b) for (int i = (a); i > (b); --i)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ALL(a) a.begin(), a.end()
#define UNIQUE(a) a.erase(unique(ALL(a)), a.end())

vector<string> split(const string &s, char c) {
	vector<string> v; stringstream ss(s); string x;
	while (getline(ss, x, c)) v.push_back(x);
	return v;
}
#define DEBUG(args...) { vector<string> _v = split(#args, ','); err(_v.begin(), args); }
void err(vector<string>::iterator it) {}
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
	cerr << "[DEBUG] " << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << '\n';
	err(++it, args...);
}

typedef long long ll;
const int MOD = 1000000007;

template<class T, class U> inline T tmin(T a, U b) { return (a < b) ? a : b; }
template<class T, class U> inline T tmax(T a, U b) { return (a > b) ? a : b; }
template<class T, class U> inline void amax(T &a, U b) { if (b > a) a = b; }
template<class T, class U> inline void amin(T &a, U b) { if (b < a) a = b; }
template<class T> T gcd(T a, T b) { while (b != 0) { T c = a; a = b; b = c % b; } return a; }

const int N = 100005;
int a[N];
ll dp[N][2], ac[N];

int main(int argc, char *argv[]) {
	ios_base::sync_with_stdio(false);

	int n, q; cin >> n >> q;
	dp[0][0] = dp[0][1] = 0;
	ac[0] = 0;
	REPU(i, 0, n) {
		cin >> a[i];
		dp[i + 1][0] = dp[i][0];
		dp[i + 1][1] = dp[i][1];
		dp[i + 1][i & 1] += a[i];
		ac[i + 1] = ac[i] + a[i];
	}

	while (q--) {
		int x; cin >> x;
		int idx = lower_bound(a, a + n, x) - a;
		int rem = n - idx;
		ll ans = 0;
		int lb = 0, rb = rem + 1;
		while (rb - lb > 1) {
			int m = (lb + rb) >> 1;
			// get m cards
			int v = a[idx + m - 1];
			int u = x - v + x;
			int j = lower_bound(a, a + n, u) - a;
			if (idx + m - j <= n - (idx + m)) lb = m;
			else rb = m;
		}
		ans = ac[n] - ac[idx + lb];
		//DEBUG(idx, lb);
		if (lb > 0) {
			int v = a[idx + lb - 1];
			int u = x - v + x;
			int j = lower_bound(a, a + n, u) - a;
			--j;
			int rev = n - (idx + lb);
			amin(j, idx + lb - 1 - rev);
			//DEBUG(j);
			if (j >= 0) {
				if ((n - j + 1) & 1) j--;
				if (j >= 0) {
					ans += dp[j + 1][j & 1];
				}
			}
		}
		else {
			int v = idx - (n - idx);
			if (v > 0) {
				ans += dp[v][(v - 1) & 1];
			}
		}

		printf("%lld\n", ans);
	}

	return 0;
}

提出情報

提出日時
問題 D - Nearest Card Game
ユーザ rantd
言語 C++14 (GCC 5.4.1)
得点 500
コード長 2581 Byte
結果 AC
実行時間 177 ms
メモリ 4480 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 2
AC × 26
セット名 テストケース
Sample sample-01.txt, sample-02.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, sample-01.txt, sample-02.txt
ケース名 結果 実行時間 メモリ
01.txt AC 3 ms 384 KiB
02.txt AC 3 ms 384 KiB
03.txt AC 3 ms 384 KiB
04.txt AC 2 ms 384 KiB
05.txt AC 3 ms 384 KiB
06.txt AC 2 ms 256 KiB
07.txt AC 24 ms 1280 KiB
08.txt AC 171 ms 4480 KiB
09.txt AC 82 ms 1536 KiB
10.txt AC 82 ms 1536 KiB
11.txt AC 177 ms 4480 KiB
12.txt AC 176 ms 4480 KiB
13.txt AC 76 ms 4480 KiB
14.txt AC 111 ms 4480 KiB
15.txt AC 100 ms 4480 KiB
16.txt AC 67 ms 4480 KiB
17.txt AC 164 ms 4480 KiB
18.txt AC 85 ms 4480 KiB
19.txt AC 153 ms 4480 KiB
20.txt AC 158 ms 4224 KiB
21.txt AC 159 ms 4224 KiB
22.txt AC 125 ms 4480 KiB
23.txt AC 128 ms 4480 KiB
24.txt AC 117 ms 4480 KiB
sample-01.txt AC 1 ms 256 KiB
sample-02.txt AC 1 ms 256 KiB