题目链接

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. Mybatis中List传值

    Mybatis遇到List传值时常用方法. 1. List<E>,E为基本数据类型和String Mapper类文件: List<User> selectByIds(List& ...

  2. SpringCloud简记_part2

    Zookeeper服务注册与发现 1)Eureka停止更新了,你怎么办? https://github.com/Netflix/eureka/wiki 2)SpringCloud整合Zookeeper ...

  3. Java mysql数据库连接Demo1

    public class MysqlUtil { /** * 链接数据库 */ /** * 方法一: * 加载驱动的方法不止一种,但这种最常用 */ public static Connection ...

  4. Quartz:基本用法总结

    OpenSymphony所提供的Quartz是任务调度领域享誉盛名的开源框架.Spring提供了集成Quartz的功能,可以让开发人员以更面向Spring的方式创建基于Quartz的任务调度应用.任务 ...

  5. Go Channel 详解

    原文链接:Go Channel 详解 Channel类型 Channel类型的定义格式如下: ChannelType = ( "chan" | "chan" & ...

  6. 在服务器上使用python-gym出现的显示问题

    参考链接: http://www.luyixian.cn/news_show_392045.aspx https://www.cnblogs.com/cenariusxz/p/12666938.htm ...

  7. python分支结构

    if分支 一.单分支结构 # if 表达式:#     语句块# 后续语句 # 执行流程:如果表达式结果为真,则执行语句块.否则,直接执行后续语句 二.双分支结构 # 双分支语句# if 表达式:# ...

  8. 尝试写一写4gl与4fd

    4gl DATABASE ds   GLOBALS "../../config/top.global"   DEFINE g_curs_index           LIKE t ...

  9. iNeuOS工业互联平台,WEB组态(iNeuView)集成实时预警和报警柱状图

    目       录 1.      概述... 2 2.      平台演示... 2 3.      应用过程... 2 4.      实时数据展示效果... 3  1.      概述 对于我们 ...

  10. 超简单集成华为HMS ML Kit文本识别SDK,一键实现账单号自动录入

    前言   在之前的文章<超简单集成华为HMS Core MLKit通用卡证识别SDK,一键实现各种卡绑定>中我们给大家介绍了华为HMS ML Kit通用卡证识别技术是如何通过拍照自动识别卡 ...