c - 查找k轮后的数组状态

标签 c arrays algorithm

<分区>

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

这是我的代码。它在我的本地计算机(Dec C++ 5.5.1 OS Win8)中运行良好。过去 2 天我一直在考虑这个问题,但是当我将它上传到 code chef 时,它显示错误答案。请告诉我这段代码中缺少什么?

#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;
}

最佳答案

问题描述

仅声明初始数组值在范围内 -2*10^9 .. 2*10^9,但并不是说您应该在每个迭代步骤后将值限制在此范围内。所以你的“主处理循环”应该看起来像这样:

for(j=1; j<=K; j++){
    largest = find_largest(A,N);
    for ( i = 0; i < N; i++ ) {
        A[i] = largest - A[i]; 
    }
} 

然后

  • 初始数组值在 -2*10^9 .. 2*10^9 范围内。
  • 第一次迭代后,所有数组值都在 0 .. 4*10^9 范围内。
  • 在所有后续迭代中,数组值保持在 0 .. 4*10^9 范围内。

long long int 在您的编译器上是一个 64 位数字,最多可以保存值 9223372036854775807。这足以进行所有这些计算。

关于c - 查找k轮后的数组状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23454090/

相关文章:

c - 填充动态 c 数组时遇到问题

objective-c - 将 NSArray 中的元素映射到它们的计数并获得最频繁的

c - fopen 一切 - 这可能吗?

c++ - 如何将 C 输出文件读入 Matlab

c - Gotoxy(int x,int y) 的使用

PHP 搜索然后插入

c - 如何为 const char* 数组赋值并打印到屏幕

c++ - float 组的中间模式平均计算

algorithm - 刺一个区间的最小点集

java - 将 java 代码转换为 python,IDE 建议?