Submission #2242805


Source Code Expand

#include <algorithm>
#include <cstring>
#include <cstdio>
#define my_abs(x) ((x) < 0 ? -(x) : (x))
typedef long long ll; 
int n, q, a, b; 
ll seg1[200005], seg2[200005], dp[200005], dt; 
inline void modify1(int pos, ll x)
{
	for (; pos <= n; pos += pos & -pos)
		seg1[pos] = std::min(seg1[pos], x); 
}
inline void modify2(int pos, ll x)
{
	for (; pos; pos -= pos & -pos)
		seg2[pos] = std::min(seg2[pos], x); 
}
inline ll query1(int pos)
{
	ll res = 1e18; 
	for (; pos; pos -= pos & -pos)
		res = std::min(res, seg1[pos]); 
	return res; 
}
inline ll query2(int pos)
{
	ll res = 1e18; 
	for (; pos <= n; pos += pos & -pos)
		res = std::min(res, seg2[pos]); 
	return res; 
}
int main()
{
	// freopen("ARC073-F.in", "r", stdin); 
	scanf("%d%d%d%d", &n, &q, &a, &b); 
	memset(dp, 0x3f, sizeof(dp)); 
	memset(seg1, 0x3f, sizeof(seg1)); 
	memset(seg2, 0x3f, sizeof(seg2)); 
	dp[b] = 0; 
	modify1(b, -b); 
	modify2(b, b); 
	while (q--)
	{
		b = a; 
		scanf("%d", &a); 
		ll cur = std::min(query1(a) + a, query2(a) - a) + dt; 
		dt += my_abs(a - b); 
		if (cur - dt < dp[b])
		{
			dp[b] = cur - dt; 
			modify1(b, dp[b] - b); 
			modify2(b, dp[b] + b); 
		}
	}
	ll ans = 1e18; 
	for (int i = 1; i <= n; i++)
		ans = std::min(ans, dp[i]); 
	printf("%lld\n", ans + dt);
	return 0; 
}

Submission Info

Submission Time
Task F - Many Moves
User diamond_duke
Language C++14 (GCC 5.4.1)
Score 900
Code Size 1335 Byte
Status AC
Exec Time 42 ms
Memory 4864 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:35:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d%d", &n, &q, &a, &b); 
                                   ^
./Main.cpp:45:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a); 
                  ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 900 / 900
Status
AC × 4
AC × 34
Set Name Test Cases
Sample example0, example1, example2, example3
All example0, example1, example2, example3, flower0, flower1, flower2, flower3, flower4, flower5, flower6, flower7, flower8, flower9, maxrand0, maxrand1, maxrand2, maxrand3, maxrand4, maxrand5, maxrand6, maxrand7, maxrand8, maxrand9, smallrand0, smallrand1, smallrand2, smallrand3, smallrand4, smallrand5, smallrand6, smallrand7, smallrand8, smallrand9
Case Name Status Exec Time Memory
example0 AC 2 ms 4864 KB
example1 AC 2 ms 4864 KB
example2 AC 2 ms 4864 KB
example3 AC 2 ms 4864 KB
flower0 AC 39 ms 4864 KB
flower1 AC 40 ms 4864 KB
flower2 AC 39 ms 4864 KB
flower3 AC 39 ms 4864 KB
flower4 AC 39 ms 4864 KB
flower5 AC 39 ms 4864 KB
flower6 AC 39 ms 4864 KB
flower7 AC 39 ms 4864 KB
flower8 AC 40 ms 4864 KB
flower9 AC 39 ms 4864 KB
maxrand0 AC 41 ms 4864 KB
maxrand1 AC 41 ms 4864 KB
maxrand2 AC 41 ms 4864 KB
maxrand3 AC 41 ms 4864 KB
maxrand4 AC 41 ms 4864 KB
maxrand5 AC 41 ms 4864 KB
maxrand6 AC 42 ms 4864 KB
maxrand7 AC 42 ms 4864 KB
maxrand8 AC 41 ms 4864 KB
maxrand9 AC 41 ms 4864 KB
smallrand0 AC 2 ms 4864 KB
smallrand1 AC 2 ms 4864 KB
smallrand2 AC 2 ms 4864 KB
smallrand3 AC 2 ms 4864 KB
smallrand4 AC 2 ms 4864 KB
smallrand5 AC 2 ms 4864 KB
smallrand6 AC 2 ms 4864 KB
smallrand7 AC 2 ms 4864 KB
smallrand8 AC 2 ms 4864 KB
smallrand9 AC 2 ms 4864 KB