Submission #2697551


Source Code Expand

#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<numeric>
#include<limits>
#include<bitset>
#include<functional>
#include<type_traits>
#include<queue>
#include<stack>
#include<array>
#include<random>
#include<utility>

#include<boost/multi_array.hpp>
#include<boost/variant.hpp>
#include<boost/optional.hpp>
#include<boost/optional/optional_io.hpp>
#include<boost/range/irange.hpp>
#include<boost/range/algorithm.hpp>
#include<boost/range/adaptors.hpp>
#include<boost/range/size.hpp>
#include<boost/multiprecision/cpp_int.hpp>

#include<cstdlib>
#include<ctime>

#define FOREACH_STRUCT2(arg0,arg1,rng)\
for(auto const& p:rng)\
for(bool f = true;f;)\
for(auto const& arg0=std::get<0>(p); f;)\
for(auto const& arg1=std::get<1>(p); f; f=false)

#define FOREACH_STRUCT3(arg0, arg1, arg2, rng)\
for(auto const& p:rng)\
for(bool f=true;f;)\
for(auto const& arg0=std::get<0>(p);f;)\
for(auto const& arg1=std::get<1>(p);f;)\
for(auto const& arg2=std::get<2>(p);f;f=false)

#define DEFINE_STRUCT2(arg0, arg1, stc)\
auto const& arg0=std::get<0>(stc);\
auto const& arg1=std::get<1>(stc)

#define DEFINE_STRUCT3(arg0, arg1, arg2, stc)\
DEFINE_STRUCT2(arg0, arg1, stc);\
auto const& arg2=std::get<2>(stc)

#define DUAL_REP(arg0,arg1, fmax, smax)\
for(int arg0:boost::irange<int>(0,fmax))\
for(int arg1:boost::irange<int>(0,smax))
#define TRIPLE_REP(arg0,arg1,arg2,fmax,smax,tmax)\
DUAL_REP(arg0,arg1,fmax,smax)\
for(int arg2:boost::irange<int>(0,tmax))

namespace lib
{
	using namespace boost::range;
	template<class T>constexpr T pow(T base, std::size_t p)
	{
		if (p == 0)
		{
			return T(1);
		}
		else if (p == 1)
		{
			return base;
		}
		else if (p == 2)
		{
			return base * base;
		}
		else if (p % 2 == 0)
		{
			return pow(pow(base, p / 2), 2);
		}
		else
		{
			return pow(pow(base, p / 2), 2)*base;
		}
	}

	template<class T>constexpr T gcd(T a, T b)
	{
		return a > b ? gcd(b, a) : a == T() ? b : gcd(b%a, a);
	}

	constexpr int intlog2(std::int64_t x)
	{
		std::int64_t j = 2;
		for (auto i = 0; i < 63; ++i)
		{
			if (j > x)
			{
				return int(i);
			}
			j <<= 1;
		}
		return 63;
	}

	template<class T>using p_queue = std::priority_queue<T, std::vector<T>, std::greater<>>;

	struct union_find_light
	{
		std::vector<int> upper;
		union_find_light(std::size_t size):upper(size, -1) {}

		int group(int index)
		{
			if (upper[index] == -1)
			{
				return index;
			}
			else
			{
				auto ret = group(upper[index]);
				upper[index] = ret;
				return ret;
			}
		}

		bool merge(int x, int y)
		{
			auto gx = group(x);
			auto gy = group(y);
			if (gx != gy)
			{
				upper[gx] = gy;
				return true;
			}
			return false;
		}

		auto get()
		{
			std::map<int, std::set<int>> ret;
			for (int i = 0; i < upper.size(); ++i)
			{
				ret[group(i)].emplace(i);
			}
			return ret;
		}

		auto tops()
		{
			std::set<int> ret;
			for (int i = 0; i < upper.size(); ++i)
			{
				ret.emplace(group(i));
			}
			return ret;
		}

		bool is_same_group(int x, int y)
		{
			return group(x) == group(y);
		}
	};

	template<class Selector>auto make_compare(Selector selector)
	{
		return [=](auto const& lhs, auto const& rhs) {return selector(lhs) < selector(rhs); };
	}

	template<class Iterator>auto make_range(Iterator first, Iterator last)
	{
		struct return_type
		{
			Iterator first;
			Iterator last;
			auto begin()const
			{
				return first;
			}
			auto end()const
			{
				return last;
			}
		};
		return return_type{ first, last };
	}

	template<class T>auto factorial_array(std::size_t max)
	{
		std::vector<T> ret(max + 1);
		ret[0] = 1;
		for (auto i : boost::irange<std::size_t>(1, max + 1))
		{
			ret[i] = ret[i - 1] * T(i);
		}
		return ret;
	}

	auto const first = [](auto p) {return p.first; };
	auto const second = [](auto p) {return p.second; };

	auto maxf = [](auto a, auto b) {return std::max(a, b); };
	auto minf = [](auto a, auto b) {return std::min(a, b); };

	template<class T>using dual_array = boost::multi_array<T, 2>;
	template<class T>using optional_dual_array = boost::multi_array<boost::optional<T>, 2>;
	namespace detail
	{
		template<class T>struct graph_t
		{
			typedef std::vector<std::map<int, T>> type;
		};
		template<>struct graph_t<void>
		{
			typedef std::vector<std::set<int>> type;
		};

	}
	template<class T = void>using graph = typename detail::graph_t<T>::type;
	template<class T>constexpr T min_value = std::numeric_limits<T>::min();
	template<class T>constexpr T max_value = std::numeric_limits<T>::max();
	
}

namespace out
{
	namespace detail
	{
		template<class>struct void_out
		{
			typedef void type;
		};
		template<class T>auto write(std::ostream& ost, T const& val)
			->typename void_out<decltype(ost << val)>::type
		{
			ost << val;
		}
		template<class T, class... Ts>void write(std::ostream& ost, T const& val, Ts const&... nexts)
		{
			write(ost, val);
			ost << " ";
			write(ost, nexts...);
		}
		template<class T, std::size_t... Is>void write_tuple(std::ostream& ost, T const& tuple, std::index_sequence<Is...>)
		{
			write(ost, std::get<Is>(tuple)...);
		}
		template<class... Ts>void write(std::ostream& ost, std::tuple<Ts...>const& tuple)
		{
			write_tuple(ost, tuple, std::make_index_sequence<sizeof...(Ts)>());
		}
		template<class T0, class T1>void write(std::ostream& ost, std::pair<T0, T1>const& pair)
		{
			write(ost, pair.first, pair.second);
		}
		template<class T>void write(std::ostream& ost, boost::optional<T>const& opt)
		{
			if (opt)
			{
				write(ost, *opt);
			}
			else
			{
				write(ost, "--");
			}
		}
		template<class T>void write(std::ostream& ost, std::vector<T>const& vec)
		{
			auto size = vec.size();
			if (size == 0)
			{
				return;
			}
			write(ost, vec[0]);
			for (auto i : boost::irange<std::size_t>(1, size))
			{
				ost << " ";
				write(ost, vec[i]);
			}
		}
		template<class T>void write(std::ostream& ost,lib::dual_array<T> const& ar)
		{
			auto a = ar.shape()[0];
			auto b = ar.shape()[1];
			write(ost, ar[0][0]);
			for (auto i : boost::irange<std::size_t>(1, b))
			{
				write(ost, " ", ar[0][i]);
			}
			for (auto i : boost::irange<std::size_t>(1, a))
			{
				ost << std::endl;
				write(ost, ar[i][0]);
				for (auto j : boost::irange<std::size_t>(1, b))
				{
					write(ost, " ", ar[i][j]);
				}
			}
		}
	}
	void write_line()
	{
		std::cout << std::endl;
	}
	template<class...Ts>void write_line(Ts const&... args)
	{
		detail::write(std::cout, args...);
		std::cout << std::endl;
	}
	void debug()
	{
		std::cerr << std::endl;
	}
	template<class...Ts>void debug(Ts const&... args)
	{
		detail::write(std::cerr, args...);
		std::cerr << std::endl;
	}
	template<class Range>void write_range(Range const& rng)
	{
		for (auto const& v : rng)
		{
			write_line(v);
		}
	}
	template<class Range>void debug_range(Range const& rng)
	{
		for (auto const& v : rng)
		{
			debug(v);
		}
	}
}

namespace adaptor = boost::adaptors;

typedef std::pair<std::int64_t, std::int64_t> int64pair;

namespace lib
{
	template<class T>class binary_indexed_tree
	{
		std::vector<T> ar;
		std::size_t size;
	public:
		binary_indexed_tree(std::size_t size_):ar(size_), size(size_)
		{

		}
		void add(T val, int index)
		{
			++index;
			while (index <= size)
			{
				ar[index - 1] += val;
				index += index & -index;
			}
		}
		auto get(int index)
		{
			++index;
			T ret{};
			while (index != 0)
			{
				ret += ar[index - 1];
				index -= index & -index;
			}
			return ret;
		}
	};
}

void Main()
{
	int N;
	std::int64_t M;
	std::cin >> N >> M;
	lib::binary_indexed_tree<int> counts(M + 1);
	std::vector<std::int64_t> sizes(M);
	std::vector<std::int64_t> vec(N);
	std::cin >> vec[0];
	--vec[0];
	std::int64_t now = 0;
	for (int i : boost::irange(0, N - 1))
	{
		std::cin >> vec[i + 1];
		--vec[i + 1];
		auto prev = vec[i];
		auto v = vec[i + 1];
		auto len = (v + M - prev) % M;
		auto s = (v + 1) % M;
		auto s2 = (s + M - (len - 1)) % M;
		sizes[s] = len;
		if (s2 < s)
		{
			counts.add(1, s2);
			counts.add(-1, s + 1);
		}
		else
		{
			counts.add(1, 0);
			counts.add(-1, s + 1);
			counts.add(1, s2);
		}
		now += std::min(len, v + 1);
	}
	auto min = now;
	for (int i : boost::irange<std::int64_t>(1, M))
	{
		now -= counts.get(i);
		now += sizes[i];
		min = std::min(min, now);
	}
	out::write_line(min);
}

int main()
{
	std::cin.tie(nullptr);
	std::cin.sync_with_stdio(false);
	std::cout << std::fixed;
	Main();
}

Submission Info

Submission Time
Task E - guruguru
User plasmaeffect
Language C++14 (GCC 5.4.1)
Score 0
Code Size 8807 Byte
Status WA
Exec Time 19 ms
Memory 2176 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 700
Status
AC × 2
AC × 7
WA × 15
Set Name Test Cases
Sample sample1.txt, sample2.txt
All half0.txt, half1.txt, half2.txt, half3.txt, half4.txt, min_m.txt, min_n.txt, mx0.txt, mx1.txt, mx2.txt, rnd0.txt, rnd1.txt, rnd2.txt, rnd3.txt, rnd4.txt, rnd5.txt, rnd6.txt, rnd7.txt, rnd8.txt, rnd9.txt, sample1.txt, sample2.txt
Case Name Status Exec Time Memory
half0.txt WA 6 ms 1152 KB
half1.txt WA 4 ms 1536 KB
half2.txt WA 9 ms 1536 KB
half3.txt WA 13 ms 1408 KB
half4.txt WA 14 ms 1920 KB
min_m.txt WA 1 ms 256 KB
min_n.txt AC 2 ms 1408 KB
mx0.txt WA 19 ms 2176 KB
mx1.txt WA 19 ms 2176 KB
mx2.txt WA 19 ms 2176 KB
rnd0.txt AC 1 ms 512 KB
rnd1.txt AC 1 ms 256 KB
rnd2.txt WA 2 ms 512 KB
rnd3.txt WA 4 ms 512 KB
rnd4.txt WA 12 ms 896 KB
rnd5.txt WA 2 ms 256 KB
rnd6.txt AC 1 ms 256 KB
rnd7.txt WA 2 ms 512 KB
rnd8.txt WA 2 ms 384 KB
rnd9.txt AC 1 ms 384 KB
sample1.txt AC 1 ms 256 KB
sample2.txt AC 1 ms 256 KB