Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)B. Take Your Places!

简介: Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)B. Take Your Places!

B. Take Your Places!

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output


William has an array of nn integers a1,a2,…,ana1,a2,…,an. In one move he can swap two neighboring items. Two items aiai and ajaj are considered neighboring if the condition |i−j|=1|i−j|=1 is satisfied.


William wants you to calculate the minimal number of swaps he would need to perform to make it so that the array does not contain two neighboring items with the same parity.


Input


Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). Description of the test cases follows.


The first line of each test case contains an integer nn (1≤n≤105)(1≤n≤105) which is the total number of items in William's array.


The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109) which are William's array.


It is guaranteed that the sum of nn over all test cases does not exceed 105105.


Output


For each test case output the minimal number of operations needed or −1−1 if it is impossible to get the array to a state when no neighboring numbers have the same parity.


Example


input


Copy

5

3

6 6 1

1

9

6

1 1 1 2 2 2

2

8 6

6

6 2 3 4 5 1


output

Copy

1

0

3

-1

2


Note


In the first test case the following sequence of operations would satisfy the requirements:


  1. swap(2, 3). Array after performing the operation: [6,1,6][6,1,6]


In the second test case the array initially does not contain two neighboring items of the same parity.


In the third test case the following sequence of operations would satisfy the requirements:


  1. swap(3, 4). Array after performing the operation: [1,1,2,1,2,2][1,1,2,1,2,2]
  2. swap(2, 3). Array after performing the operation: [1,2,1,1,2,2][1,2,1,1,2,2]
  3. swap(4, 5). Array after performing the operation: [1,2,1,2,1,2][1,2,1,2,1,2]


In the fourth test case it is impossible to satisfy the requirements.


In the fifth test case the following sequence of operations would satisfy the requirements:


swap(2, 3). Array after performing the operation: [6,3,2,4,5,1][6,3,2,4,5,1]

swap(4, 5). Array after performing the operation: [6,3,2,5,4,1][6,3,2,5,4,1]

题意分析,有这些情况


1奇数比偶数多一个数的话,反之同理,

2奇数偶数数量相等的时候取和的最小值  

3,否则就不能组成排列

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 3e5 + 10, mod = 1e9 + 7;
int n, m, t, k;
int s[N], dp[N], f[N];
char g[N];
string str;
vector<int> vec;
void solve() {
  vec.clear();
  cin >> n;
  int a = 0, b = 0, c = 0, d = 0;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    if (s[i] & 1) {
      a++;
      vec.push_back(i);
    } else
      b++;
  }
  if (abs(a - b) > 1) {
    puts("-1");
    return;
  }
  if (a == b) {
    int ans = 0, ans1 = 0;
    for (int i = 0; i < vec.size(); i++) {
      ans += abs(vec[i] - (2 * i + 1));
      ans1 += abs(vec[i] - (2 * i + 2));
    }
    printf("%d\n", min(ans, ans1));
    return;
  }
  if (a > b) {
    int ans = 0;
    for (int i = 0; i < vec.size(); i++)
      ans += abs(vec[i] - (2 * i + 1));
    printf("%d\n", ans);
    return;
  }
  int ans = 0;
  for (int i = 0; i < vec.size(); i++)
    ans += abs(vec[i] - (2 * i + 2));
  printf("%d\n", ans);
}
int main() {
  t = 1;
  cin >> t;
  while (t--)
    solve();
  return 0;
}


下面这个借鉴的,很简洁

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 5; 
int a ;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    vector<int> v;
    for (int i = 1; i <= n; i++) {
      cin >> a ;
      if (a  & 1)//奇数
        v.push_back(i);
    }
    int len = v.size();//奇数个数
    int m = n - len;//偶数个数
    if (labs(len - m) > 1) {//无法组成排列
      cout << -1 << endl;
      continue;
    }
    ll ans = 1e15 + 5, cnt = 0, p = 0;
    if (len - m == 1 || len - m == 0) { //如果奇数比偶数多1,或者相等;
      for (int i = 1; i <= n; i += 2) {
        cnt += labs(v[p++] - i);
      }
      ans = min(ans, cnt);
    }
    cnt = 0;
    p = 0;
    if (m - len == 1 || m - len == 0) {
      for (int i = 2; i <= n; i += 2) {
        cnt += labs(v[p++] - i);
      }
      ans = min(ans, cnt);
    }
    cout << ans << endl;
  }
}





相关文章
|
1天前
|
人工智能 自然语言处理 文字识别
阿里云百炼Qwen3.7-Max简介:能力、优势、支持订阅计划参考
Qwen3.7-Max是阿里云百炼面向智能体时代推出的新一代旗舰模型,对标GPT-5.5、Claude Opus 4.7等闭源旗舰。该模型支持百万级token上下文窗口,具备顶级推理能力、多模态搜索与视觉理解增强、流式输出低延迟响应等核心优势,覆盖编程、办公、长周期自主执行等复杂场景。同时支持OpenAI接口兼容,便于系统快速迁移。用户可通过Token Plan团队或节省计划等订阅方式灵活调用,适合企业级高要求场景使用。
7560 32
阿里云百炼Qwen3.7-Max简介:能力、优势、支持订阅计划参考
|
1天前
|
数据采集 人工智能 前端开发
让 Coding Agent 从黑盒到透明:阿里云 Agent 观测审计数据采集实践
AI Agent 规模化落地带来执行黑盒、行为难追溯、成本难度量三大难题。阿里云基于 OTel 标准,面向 Coding Agent、个人通用助理和框架型 Agent,推出 LoongSuite Pilot、插件及探针等无侵入采集方案,让 Agent 实现可看见、可分析、可审计、可治理。
644 144
|
1天前
|
人工智能 缓存 自然语言处理
阿里Qwen3.7-Max评测:Agent能力显著提升,耗时与调用成本大幅下降
阿里云百炼推出面向智能体的旗舰大模型Qwen3.7-Max,具备长周期自主执行能力,显著提升编程、办公自动化等复杂任务处理水平;支持MCP集成与多框架兼容,并以限时5折+100万Tokens免费试用大幅降低使用门槛,助力企业高效落地AI应用。在阿里云百炼平台快速体验:https://t.aliyun.com/U/fPVHqY
|
1天前
|
人工智能 安全 定位技术
CodeGraph深度解析 让Claude Code工具调用直降七成的核心原理与实操教程
如今以Claude Code为代表的AI编程智能体已经成为开发者日常编码、项目重构、漏洞修复的必备工具。但在长期使用过程中,几乎所有开发者都会遇到同一个明显痛点:AI虽然具备强大的代码生成与分析能力,却常常陷入盲目探索的循环中。
1265 2
|
1天前
|
人工智能 弹性计算 运维
阿里云发布堡垒机智能运维Agent,运维交互进入自然语言新时代
支持自然语言运维,提升效率与安全双保障。
1172 1
|
1天前
|
存储 定位技术 数据库
CodeGraph 如何让 Claude Code减少 7 成工具调用?
CodeGraph 为 Coding Agent 提供本地代码知识图谱,把函数、类、调用链和框架路由提前整理成“项目地图”,减少盲目搜索和文件读取。它不是新 Agent,而是上下文基础设施,让 Agent 更快找到正确代码路径,平均减少 7 成工具调用。
1316 4
|
1天前
|
人工智能 运维 JavaScript
阿里云Qoder CN(原通义灵码)全解析 产品形态、版本划分与技术适配说明
在AI辅助开发与智能办公工具持续普及的当下,阿里云旗下原通义灵码正式更名为Qoder CN,同时延伸出QoderWork CN、Qoder CN CLI、Qoder CN Mobile等多款配套产品,形成覆盖代码开发、日常办公、终端交互、移动端使用的完整工具矩阵。Qoder CN核心定位为AI智能编码助手,深度适配主流代码编辑器、集成开发环境以及终端场景;QoderWork CN则偏向桌面端综合办公辅助,二者面向不同使用场景,划分了多个版本档位,搭配差异化资源配额、功能权限与计费规则,同时兼容多款主流大模型。
399 4
|
1天前
|
JavaScript 定位技术 API
CodeGraph 爆火:编程 Agent 需要的不是更多上下文,而是一张提前画好的代码地图
CodeGraph 是一款爆火的本地代码智能工具,通过 tree-sitter 解析 AST 构建结构化知识图谱(存于 SQLite),为编程 Agent 提前生成“代码地图”。它显著降低 Agent 在中大型项目中的探索成本——实测工具调用减少71%、Token 降57%、速度提升46%,支持19+语言及主流框架路由识别,完全离线、无需 API Key。
354 1
CodeGraph 爆火:编程 Agent 需要的不是更多上下文,而是一张提前画好的代码地图
|
1天前
|
存储 安全 Java
AgentScope Java 2.0:打造分布式、企业级智能体底座
AgentScope 2.0 面向分布式部署、稳定运行、权限安全等企业级需求全面升级,打造支持多租户隔离与长期稳定运行的企业级智能体底座。
|
1天前
|
人工智能 运维 API
2026年阿里云百炼通义千问Qwen3.7-plus深度介绍 功能特性、使用优势及618大促订阅方案指南
大模型技术的普及,让AI能力逐步融入个人办公、内容创作、代码编写、企业运营、教育培训等各类场景。不同定位的模型对应不同使用需求,旗舰级模型性能强劲但使用成本偏高,轻量化模型价格低廉却难以胜任复杂任务,而介于两者之间的中端主力模型,凭借均衡的能力、亲民的定价、广泛的场景适配性,成为绝大多数个人用户、小型团队、中小企业的首选。
475 1