题目链接

https://codeforces.com/contest/1174/problem/B

题意

给一个数组,只能交换和为奇数的两个数,问最终能得到的字典序最小的序列。

题解

  • 内心OS:由题,只有交换奇偶的操作。(可略:开始觉得那就是奇数序列内部顺序不变,偶数序列内部顺序不变,然后优先队列。后来想了想不是这么回事...分明通过奇偶交换同奇偶内部顺序也可以变)。结论就是只要有数组中奇偶都存在就可以随便交换位置。同奇偶的交换借助另一个与它们元素不同的另一个元素即可互换位置。
  • 题解方法:当数组只有奇/偶,则输出原数组,否则输出排序后数组即可。

todo

好吧这并不是一份AC代码,居然在某个样例超时。可能是卡快排样例?但用List,shuffle再排序也无果。待解决。

代码

import java.util.Arrays;
import java.util.Scanner; public class OddReOrder {
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;++i) {
arr[i]=in.nextInt();
} boolean oddFlag=false;
boolean evenFlag=false;
for(int i=0;i<n;++i) {
if((arr[i]&1)==1){
oddFlag=true;
}
else {
evenFlag=true;
}
if(evenFlag&&oddFlag) {
break;
}
} if(evenFlag&&oddFlag) {
Arrays.sort(arr);
} for(int i=0;i<n;++i) {
System.out.print(arr[i]+" ");
}
}
}

[Codeforces1174B]Ehab Is an Odd Person的更多相关文章

  1. Codeforces Round #563 (Div. 2) B. Ehab Is an Odd Person

    链接:https://codeforces.com/contest/1174/problem/B 题意: You're given an array aa of length nn. You can ...

  2. CF1174B Ehab Is an Odd Person(排序+结论)

    做法 一个显然的结论就是如果至少有一个奇数和一个偶数,那么是可以随意调整的,也就是升序排序 否则不可以进行任何操作 Code #include<bits/stdc++.h> using n ...

  3. Codeforces 1174B Ehab Is an Odd Person

    题目链接:http://codeforces.com/problemset/problem/1174/B 题意:给定长度 n 的数组,任意俩个相加为奇数的数可以交换数组中的位置,让这个数组尽量从小到大 ...

  4. Codeforces Round #563 (Div. 2)B

    B.Ehab Is an Odd Person 题目链接:http://codeforces.com/contest/1174/problem/B 题目 You’re given an array a ...

  5. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  6. Codeforces Round #563 (Div. 2) A-D

    A. Ehab Fails to Be Thanos 这个A题很简单,就是排个序,然后看前面n个数和后面的n个数是不是相同,相同就输出-1 #include <cstdio> #inclu ...

  7. CF959C Mahmoud and Ehab and the wrong algorithm 构造

    Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is: Given an un ...

  8. [LeetCode] Odd Even Linked List 奇偶链表

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  9. Odd Even Linked List

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

随机推荐

  1. 阿里云体验实验室 教你如何《快速搭建LNMP环境》

    ## 体验平台简介 面向开发者和中小企业打造的一站式.全云端的开发平台,打开浏览器就可以开发.调试.上线,所测即所得,并结合无服务器的模式,重新定义云原生时代的研发工作方法论.旨在降低开发者上手成本和 ...

  2. JavaScript 基础三

    遍历对象的属性 for...in 语句用于对数组或者对象的属性进行循环操作. for (变量 in 对象名字) { 在此执行代码 } 这个变量是自定义 符合命名规范 但是一般我们 都写为 k 或则 k ...

  3. Gitlab-CI/CD 1

    Gitlab-Runner自动构建服务器搭建1 这里讲到的gitlab仓库指的是https://gitlab.com/,自建gitlab仓库也基本相同. 项目的构建打包过程相对比较消耗系统性能,所以g ...

  4. StructuredStreaming简单的例子(NewAPI)

    StructuredStreaming简单的例子(NewAPI)(wordCount) package com.briup.streaming.structed import org.apache.l ...

  5. 进阶6:连接查询 二、sql99语法

    #二.sql99语法/*语法: select 查询列表 from 表1 别名 [连接类型] join 表2 别名 on 连接条件 [where 筛选条件] [group by 分组] [having ...

  6. 牛客网PAT练习场-到底买不买

    题目地址:https://www.nowcoder.com/pat/6/problem/4065 题意:用数组统计好字符,最后进行相减,最后进行统计 /** * *作者:Ycute *时间:2019- ...

  7. 【干货!!】三句话搞懂 Redis 缓存穿透、击穿、雪崩

    前言 如何有效的理解并且区分 Reids 穿透.击穿和雪崩之间的区别,一直以来都挺困扰我的.特别是穿透和击穿,过一段时间就稀里糊涂的分不清了. 为了有效的帮助笔者自己,以及拥有同样烦恼的朋友们区分这三 ...

  8. ASP.NET Core3.1使用IdentityServer4中间件系列随笔(三):创建使用[ClientCredentials客户端凭证]授权模式的客户端

    配套源码:https://gitee.com/jardeng/IdentitySolution 上一篇<ASP.NET Core3.1使用IdentityServer4中间件系列随笔(二):创建 ...

  9. 淘宝ios端弹窗-2020年3月25日

  10. python IDE(集成开发环境)——PyCharm的安装

    选择PyCharm的原因: 1.很多大佬都推荐,说适合实战操作项目 2.另外是界面可视化不错,颜色很舒服(体验完之后的感觉) 3.PyCharm本身有很多操作快捷键很方便(后续可以记下来常用的,方便自 ...