From d6b88f64de4d72e5112a6307c3229ccc58ada134 Mon Sep 17 00:00:00 2001 From: "Artur M. Wolff" Date: Mon, 30 Mar 2020 12:10:04 +0200 Subject: [PATCH] Add is_rotated --- README.md | 2 +- include/libcompetitive.hxx | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8725cb8..e564692 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # libcompetitive -A C++ header-only library with snippets useful for Competitive Programming. +A C++ header-only library with snippets useful (and not only) for Competitive Programming. ## Example usage diff --git a/include/libcompetitive.hxx b/include/libcompetitive.hxx index 6ac7d87..4980d8a 100644 --- a/include/libcompetitive.hxx +++ b/include/libcompetitive.hxx @@ -1,6 +1,7 @@ #pragma once #include +#include inline int parseint(void) { int c, n; @@ -10,3 +11,21 @@ inline int parseint(void) { return n; } + +template +inline bool is_rotated(std::vector const& v) { + int rises = 0; + int falls = 0; + for (size_t i = 0, s = v.size(); i < s; i++) { + if (v[i] < v[(i + 1) % s]) { + rises++; + } + if (v[i] > v[(i + 1) % s]) { + falls++; + } + if (rises > 1 && falls > 1) { + return false; + } + } + return true; +}