Codeforces 135A-Replacement(思维)
2 seconds
256 megabytes
standard input
standard output
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109,
inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109,
inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
The first line contains a single integer n (1 ≤ n ≤ 105),
which represents how many numbers the array has. The next line contains nspace-separated integers — the array's description. All elements of the array lie
in the range from 1 to 109,
inclusive.
Print n space-separated integers — the minimum possible values of each array element after one replacement and the sorting are performed.
5
1 2 3 4 5
1 1 2 3 4
5
2 3 4 5 6
1 2 3 4 5
3
2 2 2
1 2 2
题意:给出一个长度为n的数组,要求替换掉数组中的一个数。(替换的含义是要用另外一个与次数不同的数来替换)全部的数的范围在[1,10^9],然后排序后的要求是使每一个位置上的数尽可能小,一种比較巧的做法是对于这个数组先排序(升序) 然后将最大的那个数换成1 假设最大的为1 则换成2(想想 为什么 )
然后在排序输出就能够了
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
#define LL long long
int a[100050];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d",a+i);
sort(a,a+n);
a[n-1]=a[n-1]==1? 2:1;
sort(a,a+n);
for(int i=0;i<n;i++)
if(i!=n-1)
printf("%d ",a[i]);
else
printf("%d\n",a[i]);
}
return 0;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
Codeforces 135A-Replacement(思维)的更多相关文章
- CodeForces 604C 【思维水题】`
题意: 给你01字符串的长度再给你一个串. 然后你可以在这个串中选择一个起点和一个终点使得这个连续区间内所有的位取反. 求: 经过处理后最多会得到多少次01变换. 例如:0101是4次,0001是2次 ...
- Minimum Integer CodeForces - 1101A (思维+公式)
You are given qq queries in the following form: Given three integers lili, riri and didi, find minim ...
- Codeforces 1038D - Slime - [思维题][DP]
题目链接:http://codeforces.com/problemset/problem/1038/D 题意: 给出 $n$ 个史莱姆,每个史莱姆有一个价值 $a[i]$,一个史莱姆可以吃掉相邻的史 ...
- AND Graph CodeForces - 987F(思维二进制dfs)
题意:给出n(0≤n≤22)和m,和m个数ai,1 ≤ m ≤ 2n ,0≤ai<2n ,把ai & aj == 0 的连边,求最后有几个连通块 解析:一个一个去找肯定爆,那么就要转换一 ...
- CodeForces - 631C ——(思维题)
Each month Blake gets the report containing main economic indicators of the company "Blake Tech ...
- Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)
Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...
- Diagonal Walking v.2 CodeForces - 1036B (思维,贪心)
Diagonal Walking v.2 CodeForces - 1036B Mikhail walks on a Cartesian plane. He starts at the point ( ...
- CodeForces - 1102A(思维题)
https://vjudge.net/problem/2135388/origin Describe You are given an integer sequence 1,2,-,n. You ha ...
- Stack Sorting CodeForces - 911E (思维+单调栈思想)
Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty) ...
随机推荐
- C#判断操作系统是32位还是64位(超简单)
由于项目需要在64位和32位系统运行,需要判断当前系统是32位还是64位. 网上很多方法,但是都感觉不是很简洁,最后发现可以使用int的长度来判断:看代码 /// <summary> ...
- Javascript中的__proto__、prototype、constructor
今天重温了下Javacript,给大家带来一篇Javascript博文,相信对于Javacript有一定了解的人都听过prototype原型这个概念,今天我们深度的分析下prototype与__pro ...
- zookeeper错误KeeperErrorCode = ConnectionLoss解决
原因: 一般是由于连接还未完成就执行zookeeper的get/create/exsit操作引起的. 解决方法: 利用"CountDownLatch 类 + zookeeper的watche ...
- 使用Intel HAXM 加速你的Android模拟器
Android 模拟器一直以运行速度慢著称, 本文介绍使用 Intel HAXM 技术为 Android 模拟器加速, 使模拟器运行度媲美真机, 彻底解决模拟器运行慢的问题. Intel HAXM ( ...
- Java OCR tesseract 图像智能字符识别技术
公司有需求啊,所以就得研究哈,最近公司需要读验证码,于是就研究起了图像识别,应该就是传说中的(OCR:光学字符识别OCR),下面把今天的收获整理一个给大家做个分享. 本人程序用的tesseract,官 ...
- 乐在其中设计模式(C#) - 原型模式(Prototype Pattern)
原文:乐在其中设计模式(C#) - 原型模式(Prototype Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 原型模式(Prototype Pattern) 作者:weba ...
- 编译命令行终端 swift
So, this is where swift lives, after you've installed XCode 6 Beta: /Applications/Xcode6-Beta.app/Co ...
- Windows10微软在线账户与本地账户的切换方法
Win10里面存在着两个账户,除了本地账户外,还有着一个微软在线账户,这个账户可以同步设置.日历等数据.不过对于大部分用户来说,本地账户已经足够我们使用了,那么这两个账户之间该如何切换呢? Win10 ...
- 【C语言探索之旅】 第二部分第八课:动态分配
内容简介 1.课程大纲 2.第二部分第八课: 动态分配 3.第二部分第九课预告: 实战“悬挂小人”游戏 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言 ...
- Android Studio如何引用jar包裹(不gradle)
这和eclipse最大的区别.与非常人的预期开始Android Studio我们很不高兴这一套. 它直接在地图上.首先通过File->Projcet structure打开project结构界面 ...