code厨师问题:对 N 整数定的数组的 A 和若干 K 。在一个翻身所有爱的最大值选择,让我们把它叫做 MAX 。然后点击爱= MAX - 艾做是为了每一个 1< = I< = N 。了解如何将数组看起来像ķ关闭后。
输入
中的数字的 N 和 K 在输入的第一行给出。然后点击 N 整数中表示数组的第二行给出的 A
输出
输出的 N 在一行中的数字。它应该是阵列的 A 之后的 K 打开。
约束
1< = N< = 10 ^ 5
0℃= K&其中; = 10 ^ 9
爱不超过2 * 10 ^ 9是绝对值。
示例
输入:
4 1
5 -1 7 0
输出:
2 8 0 7
下面是我的code。它的工作原理完全正常在我的本地计算机(十二月C ++ 5.5.1操作系统Win8的)。我一直在想在这个的最后2天,但是当我把它上传于code厨师它显示的错误答案。请告诉我在这code我缺少什么?
的#include< stdio.h中>
得到long long int find_largest(得到long long int a [],长尺寸){
长整型最大= A [0],我;
对于(i = 1; I<大小;我++){
如果(最大< A [1])
最大= A [1];
}
最大的回报;
}
诠释的main(){
得到long long int A [100000],K,J,最大= 0,温度,prev_largest = 0;
N久,我;
焦炭eof_chk;
INT标志= 0;
而(1){
scanf函数(%LD%法学博士,与放大器; N,功放; K);
对于(I = 0; I&所述N;我+ +)
scanf函数(%LLD,和放大器; A [1]);
最大= find_largest(A,N);
//处理...
为(J = 1; J< = K,J ++){
最大= find_largest(A,N);
如果(prev_largest ==最大){
如果((K-j)条%2 == 0){
对于(I = 0; I&所述N;我++){
TEMP =最大 - A [1];
如果(临时> 20亿)
A [1] = - (20亿 - 温度+ 20亿+ 1);
其他
A [1] =气温;
}
打破;
}
别人休息;
}
其他{
对于(I = 0; I&所述N;我++){
TEMP =最大 - A [1];
如果(临时> 20亿)
A [1] = - (20亿 - 温度+ 20亿+ 1);
其他
A [1] =气温;
}
prev_largest =最大;
}
}
//开始进行格式化打印
对于(I = 0; I&所述N;我++){
如果(ⅰ==(N-1)){
的printf(%LLD,A [1]);
}
其他
的printf(%LLD,A [1]);
}
//结束格式化打印
如果(的getchar()== EOF){
打破;
}
其他
的printf(\ N);
}
返回0;
}
解决方案
问题说明
HTTP://www.$c$cchef.com/MAY14/problems/ RRSTONE只是说的初始的数组值的范围是
-2 * 10 ^ 9。2 * 10 ^ 9
,但不是说你应该每次迭代步骤后限制值,在该范围内。所以,你的主处理循环应该简单地是这样的:
为(J = 1; J< = K,J ++){
最大= find_largest(A,N);
对于(I = 0; I&所述N;我++){
A [1] =最大 - A [1];
}
}
然后
在最初的数组值的范围是 -2 * 10 ^ 9。2 * 10 ^ 9
。
在第一次迭代中所有数组值的范围是 0。4 * 10 ^ 9
。在
在所有后续的迭代中,数组值停留在范围 0。4 * 10 ^ 9
。
得到long long int
是你的编译器的64位量,可容纳值高达
9223372036854775807
。这足以让所有这些计算。
Code Chef Problem: Given array of N integers A and a number K. During a turn the maximal value over all Ai is chosen, let's call it MAX. Then Ai = MAX - Ai is done for every 1 <= i <= N. Find out how will the array look like after K turns.
Input
The numbers N and K are given in the first line of an input. Then N integers are given in the second line which denote the array A.
Output
Output N numbers on a single line. It should be the array A after K turns.
Constraints
1 <= N <= 10^5
0 <= K <= 10^9
Ai does not exceed 2 * 10^9 by it's absolute value.
Example
Input:
4 1
5 -1 7 0
Output:
2 8 0 7
Here is my code. It works perfectly fine in my local computer (Dec C++ 5.5.1 OS Win8). I have been thinking over this for the last 2 days but when I upload it on code chef it shows wrong answer. Please tell me what am I missing in this code ?
#include <stdio.h>
long long int find_largest(long long int A[], long size){
long int largest =A[0],i;
for(i=1;i<size;i++){
if(largest < A[i])
largest = A[i];
}
return largest;
}
int main(){
long long int A[100000],K,j,largest =0, temp,prev_largest = 0;
long N, i;
char eof_chk;
int flag = 0;
while(1){
scanf("%ld %lld",&N,&K);
for(i=0;i<N;i++)
scanf("%lld",&A[i]);
largest = find_largest(A,N);
// Processing...
for(j=1; j<=K; j++){
largest = find_largest(A,N);
if(prev_largest == largest){
if((K-j)%2 == 0){
for ( i = 0; i < N; i++ ){
temp = largest - A[i];
if(temp > 2000000000)
A[i] = -(2000000000 - temp + 2000000000 + 1);
else
A[i] = temp;
}
break;
}
else break;
}
else{
for ( i = 0; i < N; i++ ){
temp = largest - A[i];
if(temp > 2000000000)
A[i] = -(2000000000 - temp + 2000000000 + 1);
else
A[i] = temp;
}
prev_largest = largest;
}
}
// Start for formated printing
for(i=0;i<N;i++){
if(i == (N-1)){
printf("%lld",A[i]);
}
else
printf("%lld ",A[i]);
}
// End for formated printing
if(getchar() == EOF) {
break;
}
else
printf("\n");
}
return 0;
}
解决方案
The problem description
http://www.codechef.com/MAY14/problems/RRSTONEstates only that the initial array values are in the range
-2*10^9 .. 2*10^9
, but not that you should restrict the values to this range after each iteration step. So your "main processing loop" should simply look like this:
for(j=1; j<=K; j++){
largest = find_largest(A,N);
for ( i = 0; i < N; i++ ) {
A[i] = largest - A[i];
}
}
Then
The initial array values are in the range-2*10^9 .. 2*10^9
.
After the first iteration all array values are in the range 0 .. 4*10^9
.
In all following iterations, the array values stay in the range 0 .. 4*10^9
.
long long int
is an 64-bit quantity on your compiler and can hold values up to
9223372036854775807
. That is sufficient for all these calculations.