Submission #24374813


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

int n;
long long x[200005], y[200005], za[200005], zb[200005], wa[200005], wb[200005], ans = LLONG_MAX;
vector<tuple<long long, long long, long long>> va, vb;

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%lld", x + i);
	for (int i = 0; i < n; i++)
		y[i + 1] = y[i] + x[i];
	zb[0] = x[0];
	for (int i = 1; i < n; i++) {
		za[i] = za[i - 1] + max(0LL, x[i] - x[i - 1]);
		zb[i] = zb[i - 1] + min(0LL, x[i] - x[i - 1]);
	}
	for (int i = 0; i < n; i++) {
		wa[i + 1] = wa[i] + za[i];
		wb[i + 1] = wb[i] + zb[i];
	}
	va.emplace_back(LLONG_MIN, -n, -wa[n]);
	for (int i = n - 1; i >= 0; i--)
		va.emplace_back(-za[i], n - i * 2, wa[n] - wa[i] * 2);
	va.emplace_back(LLONG_MAX, LLONG_MAX, LLONG_MAX);
	vb.emplace_back(LLONG_MIN, -n, wb[n]);
	for (int i = n - 1; i >= 0; i--)
		vb.emplace_back(zb[i], n - i * 2, -wb[n] + wb[i] * 2);
	vb.emplace_back(LLONG_MAX, LLONG_MAX, LLONG_MAX);
	for (int ia = 0, ib = 0; ; ) {
		long long from = max(get<0>(va[ia]), get<0>(vb[ib]));
		if (from == LLONG_MAX)
			break;
		long long to = min(get<0>(va[ia + 1]), get<0>(vb[ib + 1]));
		long long ta = get<1>(va[ia]) + get<1>(vb[ib]);
		long long tb = get<2>(va[ia]) + get<2>(vb[ib]);
		if (from != LLONG_MIN)
			ans = min(ans, ta * from + tb);
		if (to != LLONG_MAX)
			ans = min(ans, ta * to + tb);
		if (to == get<0>(va[ia + 1]))
			ia++;
		else
			ib++;
	}
	printf("%lld\n", ans);
}

Submission Info

Submission Time
Task D - Inc, Dec - Decomposition
User nhho
Language C++ (GCC 9.2.1)
Score 700
Code Size 1487 Byte
Status AC
Exec Time 57 ms
Memory 26496 KiB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:10:7: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   10 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
./Main.cpp:12:8: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   12 |   scanf("%lld", x + i);
      |   ~~~~~^~~~~~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 3
AC × 40
Set Name Test Cases
Sample 01_sample_01.txt, 01_sample_02.txt, 01_sample_03.txt
All 01_sample_01.txt, 01_sample_02.txt, 01_sample_03.txt, 02_small_01.txt, 02_small_02.txt, 02_small_03.txt, 02_small_04.txt, 02_small_05.txt, 02_small_06.txt, 02_small_07.txt, 02_small_08.txt, 02_small_09.txt, 02_small_10.txt, 03_rand_01.txt, 03_rand_02.txt, 03_rand_03.txt, 03_rand_04.txt, 03_rand_05.txt, 03_rand_06.txt, 03_rand_07.txt, 03_rand_08.txt, 03_rand_09.txt, 03_rand_10.txt, 03_rand_11.txt, 03_rand_12.txt, 03_rand_13.txt, 03_rand_14.txt, 03_rand_15.txt, 03_rand_16.txt, 03_rand_17.txt, 03_rand_18.txt, 03_rand_19.txt, 03_rand_20.txt, 04_handmade_01.txt, 04_handmade_02.txt, 04_handmade_03.txt, 04_handmade_04.txt, 04_handmade_05.txt, 04_handmade_06.txt, 04_handmade_07.txt
Case Name Status Exec Time Memory
01_sample_01.txt AC 1 ms 3600 KiB
01_sample_02.txt AC 6 ms 3656 KiB
01_sample_03.txt AC 1 ms 3684 KiB
02_small_01.txt AC 2 ms 3696 KiB
02_small_02.txt AC 2 ms 3616 KiB
02_small_03.txt AC 3 ms 3692 KiB
02_small_04.txt AC 2 ms 3760 KiB
02_small_05.txt AC 2 ms 3796 KiB
02_small_06.txt AC 2 ms 3748 KiB
02_small_07.txt AC 4 ms 3704 KiB
02_small_08.txt AC 2 ms 3768 KiB
02_small_09.txt AC 3 ms 3656 KiB
02_small_10.txt AC 2 ms 3768 KiB
03_rand_01.txt AC 7 ms 4744 KiB
03_rand_02.txt AC 35 ms 14228 KiB
03_rand_03.txt AC 29 ms 14296 KiB
03_rand_04.txt AC 5 ms 4032 KiB
03_rand_05.txt AC 32 ms 16652 KiB
03_rand_06.txt AC 7 ms 4880 KiB
03_rand_07.txt AC 33 ms 13768 KiB
03_rand_08.txt AC 44 ms 21888 KiB
03_rand_09.txt AC 46 ms 23476 KiB
03_rand_10.txt AC 49 ms 24132 KiB
03_rand_11.txt AC 12 ms 6148 KiB
03_rand_12.txt AC 21 ms 9692 KiB
03_rand_13.txt AC 33 ms 14188 KiB
03_rand_14.txt AC 15 ms 8124 KiB
03_rand_15.txt AC 28 ms 14148 KiB
03_rand_16.txt AC 38 ms 21936 KiB
03_rand_17.txt AC 15 ms 7872 KiB
03_rand_18.txt AC 56 ms 24800 KiB
03_rand_19.txt AC 5 ms 4000 KiB
03_rand_20.txt AC 57 ms 25416 KiB
04_handmade_01.txt AC 52 ms 26452 KiB
04_handmade_02.txt AC 52 ms 26444 KiB
04_handmade_03.txt AC 43 ms 26352 KiB
04_handmade_04.txt AC 52 ms 26496 KiB
04_handmade_05.txt AC 52 ms 26380 KiB
04_handmade_06.txt AC 49 ms 26376 KiB
04_handmade_07.txt AC 49 ms 26356 KiB