Codeforces 135A-Replacement(思维)
2014-09-30 13:13
381 查看
A. Replacement
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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.
Output
Print n space-separated integers — the minimum possible values of each array element after one replacement and the sorting are performed.
Sample test(s)
input
output
input
output
input
output
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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.
Output
Print n space-separated integers — the minimum possible values of each array element after one replacement and the sorting are performed.
Sample test(s)
input
5 1 2 3 4 5
output
1 1 2 3 4
input
5 2 3 4 5 6
output
1 2 3 4 5
input
3 2 2 2
output
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 135A-Replacement(有个小坑)
- Replacement(CodeForces 135A)
- codeforces 763B. Timofey and rectangles [思维]【智商】
- Codeforces 265 Escape from Stones 思维(极限)
- codeforces-660【思维】
- Codeforces 821D Okabe and City (拆点+思维建图+spfa)
- CodeForces 554B Ohana Cleans Up(思维)
- Codeforces 315E Sereja and Subsequences【思维+Dp】
- Codeforces 868C Qualification Rounds【思维】
- CodeForces 554B. Ohana Cleans Up【思维】
- codeforces 777D. Cloud of Hashtags (逆序思维)
- Codeforces 214C Game【思维+拓扑排序】
- Codeforces 115 B Lawnmower【思维】
- codeforces 552C Vanya and Scales 【思维】
- codeforces 719B:Anatoly and Cockroaches(思维)
- Codeforces 782B The Meeting Place Cannot Be Changed【二分+思维】
- Codeforces 876E National Property【思维建图+2-sat+输出可行解】好题~
- Codeforces 371B - Filya and Homework(思维)
- Codeforces 689D Friends and Subsequences【思维+二分+RMQ】套路题