{ $val = -$array[$pos]; if ($pos == 0) { $l = $length - 1; } else { $l = $pos - 1; } if ($pos == $length - 1) { $r = 0; } else { $r = $pos + 1; } $array[$l] -= $val; $array[$r] -= $val; $array[$pos] = -$array[$pos]; return $array; } $handle = fopen("php://stdin", "r"); $length = trim(fgets($handle)); $n = trim(fgets($handle)); $banks = explode(' ', $n); $sum = array_sum($banks); $magicMove = 0; while (!isPositive($banks)) { $min = min($banks); $whereMin = array_search($min, $banks, $length); doMagicMove($banks, $whereMin, $length); $magicMove++; } echo $magicMove; fclose($handle);
/** * Build query from filter data * @param Query $query Query object * @param array $data Filter data ($this->request->query) * @return Query $query Query object */ public function queryFromFilter(Query $query, array $data = []) { //"ID" field if (!empty($data['id']) && isPositive($data['id'])) { $query->where([sprintf('%s.id', $this->alias()) => $data['id']]); } //"Title" field if (!empty($data['title']) && strlen($data['title']) > 2) { $query->where([sprintf('%s.title LIKE', $this->alias()) => sprintf('%%%s%%', $data['title'])]); } //"Filename" field if (!empty($data['filename']) && strlen($data['filename']) > 2) { $query->where([sprintf('%s.filename LIKE', $this->alias()) => sprintf('%%%s%%', $data['filename'])]); } //"User" (author) field if (!empty($data['user']) && preg_match('/^[1-9]\\d*$/', $data['user'])) { $query->where([sprintf('%s.user_id', $this->alias()) => $data['user']]); } //"Category" field if (!empty($data['category']) && preg_match('/^[1-9]\\d*$/', $data['category'])) { $query->where([sprintf('%s.category_id', $this->alias()) => $data['category']]); } //"Active" field if (!empty($data['active'])) { $query->where([sprintf('%s.active', $this->alias()) => $data['active'] === 'yes']); } //"Priority" field if (!empty($data['priority']) && preg_match('/^[1-5]$/', $data['priority'])) { $query->where([sprintf('%s.priority', $this->alias()) => $data['priority']]); } //"Created" field if (!empty($data['created']) && preg_match('/^[1-9][0-9]{3}\\-[0-1][0-9]$/', $data['created'])) { $start = new Time(sprintf('%s-01', $data['created'])); $end = (new Time($start))->addMonth(1); $query->where([sprintf('%s.created >=', $this->alias()) => $start, sprintf('%s.created <', $this->alias()) => $end]); } return $query; }