cf1556B B. Take Your Places!

题意:

有n个数,你可以将相邻两个数交换,使得奇偶性一样的数不相邻。问最少操作步数

题解:

最终排列无非是:奇,偶,奇…或者偶,奇,偶…
如果奇数>偶数,我们就按照第一个排,如果偶数>奇数,我们就按照第二个排。如果两者相等,两种排列都测一遍,取较小值

代码:

// Problem: B. Take Your Places!
// Contest: Codeforces - Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)
// URL: https://codeforces.com/contest/1556/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// Data:2021-08-31 23:46:12
// By Jozky#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{x= 0;char c= getchar();bool flag= 0;while (c < '0' || c > '9')flag|= (c == '-'), c= getchar();while (c >= '0' && c <= '9')x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();if (flag)x= -x;read(Ar...);
}
template <typename T> inline void write(T x)
{if (x < 0) {x= ~(x - 1);putchar('-');}if (x > 9)write(x / 10);putchar(x % 10 + '0');
}
void rd_test()
{#ifdef LOCALstartTime= clock();freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{#ifdef LOCALendTime= clock();printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn= 1e5 + 9;
int a[maxn];
int b[maxn];
int main()
{//rd_test();int t;read(t);while (t--) {int n;read(n);vector<int> q1; //奇数vector<int> q2; //偶数for (int i= 0; i < n; i++) {read(a[i]);if (a[i] % 2 == 1) //奇数q1.push_back(i);elseq2.push_back(i);}if (n == 1)printf("0\n");else if (abs((int)q1.size() - (int)q2.size()) > 1)printf("-1\n");else if ((int)q1.size() > (int)q2.size()) {int pos= 0;ll sum= 0;for (int i= 0; i < q1.size(); i++) {sum+= abs(q1[i] - pos);pos+= 2;}printf("%lld\n", sum);}else if ((int)q1.size() < (int)q2.size()) {int pos= 0;ll sum= 0;for (int i= 0; i < q2.size(); i++) {sum+= abs(q2[i] - pos);pos+= 2;}printf("%lld\n", sum);}else if ((int)q1.size() == (int)q2.size()) {int pos= 0;ll sum= 0;for (int i= 0; i < q1.size(); i++) {sum+= abs(q1[i] - pos);pos+= 2;}pos= 0;ll sum2= 0;for (int i= 0; i < q2.size(); i++) {sum2+= abs(q2[i] - pos);pos+= 2;}printf("%lld\n", min(sum, sum2));}}return 0;//Time_test();
}

cf1556B B. Take Your Places!相关推荐

  1. Given a list,rotate the list to right by k places, where k is nonegative.

    Given a list,rotate the list to right by k places, where k is nonegative. 给定一个列表,将列表向右旋转K个位置,其中K是非分隔 ...

  2. Xamarin.Forms Layout Challenges – Great Places(已全文翻译)

    原文地址:https://www.kymphillpotts.com/xamarin-forms-layout-challenges-great-places/ (作者Kym Phillpotts) ...

  3. places365_Google Places API网络服务示例

    places365 Google Places API can be used to find nearby places. In this tutorial, we'll be developing ...

  4. Unit5 Going places

    Recite: Conversation A: A: I'm so excited!We have two weeks off! What are you going to do? B: I'm no ...

  5. Places: A 10 million Image Database for Scene Recognition

    原文链接: http://places2.csail.mit.edu/PAMI_places.pdf 摘要 数百万项的数据集的兴起,让机器学习算法在物体和场景的识别等任务上可以接近人类语义分类的表现. ...

  6. Python中使用GoogleMaps API(包含Geocoding API,Places API Web Service )详细教程

    前言 业务需要根据经纬度获取该城市的邮政编码,需要通过调用GoogleMaps来搜索地点,来获取地点的详细信息.于是查阅了一些相关的资料,特意分享出来给大家参考参考. 涉及相关的资料: google官 ...

  7. F-02手工记账(凭证货币JPY)报错,消息号00011“小数位不允许”(Decimal places are not permitted)-OY04和金额内外部转换函数

    文章目录 一.场景概述 1.1 手工记账F-02 1.2 自动生成凭证 二.原因分析-OY04 2.1 实际场景原因分析 三.金额内外部转换函数 一.场景概述 SAP常用会计凭证生成方式:手工记账和通 ...

  8. Google Places API –练习

    Google Places API – practice Do you use google maps? I am sure that yes. Have you ever tried 'Search ...

  9. [译]本地排名因素研究(系列)之一 ---Google places(谷歌地方信息)优化

    注:原文地址 Local Ranking Factors Study 1 of 5 - Google Places Optimization 我们的目标 本文是一个5系列的bizible研究的第一部分 ...

最新文章

  1. 毕业论文查重心得体会—吐血奉献
  2. hdu1728 广搜
  3. 电影院票务管理系统数据库设计(1)
  4. nginx反向代理和rewrite进行解决跨域问题、去掉url中的一部分字符串,通过nginx正则生成新的url
  5. 百万年薪程序员必会的五种技术
  6. android版本相机权限,Android 11系统权限收紧,第三方APP仅支持调用原生相机
  7. C#/Net代码精简优化技巧(1)
  8. parseInt(string, radix)
  9. Linux系统编程:验证kernel内核缓存区大小-4096字节
  10. Linux多线程实践(10) --使用 C++11 编写 Linux 多线程程序
  11. php如何使用memcached,PHP如何使用Memcached_PHP
  12. SprutCAM v4.0.1.30 Expert Edition-ISO 1CD(完全版)
  13. LittleVGL-键盘控件
  14. 机械臂技术参数的意义
  15. html倒计时的原理,JS实现活动精确倒计时 - 轩枫阁
  16. 2021小透明的年度总结
  17. 无网络下imac重装系统
  18. 北京医院张烜教授课题组招聘科研博士后
  19. 阿里P8架构师谈(1):双11秒杀系统如何设计
  20. linux私房菜高级,别人的Linux私房菜(15)磁盘配额与高级文件系统管理

热门文章

  1. 这才是真正的蛙泳,还挺能蹦跶......
  2. 实用Python库,这几个你认识不?
  3. 年底求职难?起薪28万的数据岗位,人才缺口达150万,不限专业学历……
  4. linux实时进程优先级rt,Linux实时性- PREEMPT_RT实时抢占实现
  5. java中错误的源文件,关于Java源文件结构规则,说法错误的是()A.版权信息必须在java文件的开头B.package语句在imports...
  6. Android jdwp 自动断开,开启debug调试模式,进入不了,等一会就闪退了!!!!
  7. 查看mysql数据库历史_查看mysql数据库历史
  8. java取整公式,Java取整函数 四舍五入函数-Go语言中文社区
  9. C++ 学习之旅(7)——指针pointer
  10. [MyBatisPlus]代码生成器